473,394 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

std::string and integers

Obviously the following doesn't work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

So can anyone give me some idea what's the nicest way to this sort of thing?
I'm sure it's not using sprintf.

with thanks,
G.A.
Jul 22 '05 #1
13 3491
"Glen Able" <sm*************@hotMEmail.com> writes:
Obviously the following doesn't work: int i = 5;
std::string myString = "Number is " + i + " thankyou please"; So can anyone give me some idea what's the nicest way to this sort of thing?
I'm sure it's not using sprintf.


You can use cout-style formatting to print to a string. See the FAQ
http://www.parashift.com/c++-faq-lit....html#faq-38.1
Jul 22 '05 #2


On Thu, 8 Jul 2004, Glen Able wrote:
Obviously the following doesn't work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

So can anyone give me some idea what's the nicest way to this sort of thing?
I'm sure it's not using sprintf.

with thanks,
G.A.

// one way:

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
int i = 5;
string myString1 = "The number is ";
string myString2 = " thank you, please";

ostringstream os;

os << myString1 << i << myString2;
string msgString = os.str();

cout << msgString << endl;

}
Jul 22 '05 #3
try
int i = 5;
std::string myString;
myString = "Number is " + i + " thankyou please";
// ANders
On Thu, 08 Jul 2004 09:28:58 +0100, Glen Able wrote:
Obviously the following doesn't work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

So can anyone give me some idea what's the nicest way to this sort of thing?
I'm sure it's not using sprintf.

with thanks,
G.A.


Jul 22 '05 #4
Anders Persson wrote:
try
int i = 5;
std::string myString;
myString = "Number is " + i + " thankyou please";
// ANders


This won't compile, since it's illegal in C++ to add two pointers
together.

myString = "Number is " + i;

would compile, but it would for sure not do what was intended.

Jul 22 '05 #5

"Gwar" <xe**@xor.qua> wrote in message
news:20*******************@synergy.transbay.net...
> Obviously the following doesn't work:
int i = 5;
std::string myString = "Number is " + i + " thankyou please";

int i = 5;
string myString1 = "The number is ";
string myString2 = " thank you, please";

ostringstream os;

os << myString1 << i << myString2;
string msgString = os.str();

cout << msgString << endl;


Eek! Why isn't there anything nice, like a std::string ctor that takes an
int, so I could then do
"blah " + std::string(i) + " blah";


Jul 22 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Glen Able wrote:
"Gwar" <xe**@xor.qua> wrote in message
news:20*******************@synergy.transbay.net...
Obviously the following doesn't work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

int i = 5;
string myString1 = "The number is ";
string myString2 = " thank you, please";

ostringstream os;

os << myString1 << i << myString2;
string msgString = os.str();

cout << msgString << endl;

Eek! Why isn't there anything nice, like a std::string ctor that takes an
int, so I could then do
"blah " + std::string(i) + " blah";

There is, its called lexical cast & is part of the Boost library. look
at http://boost.org/libs/conversion/lexical_cast.htm

The code looks like the following:

string mystring;
mystring = "The number is " + lexical_cast<short>(5) + " thank you, please";

Evan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFA7T4Zoo/Prlj9GScRAi+wAJ9R3yYSkpBSmORTEoGarWyCvTxAhQCeNm0L
trw57xPoS3zrpilJjmpQc28=
=e9Og
-----END PGP SIGNATURE-----
Jul 22 '05 #7
> There is, its called lexical cast & is part of the Boost library. look
at http://boost.org/libs/conversion/lexical_cast.htm

The code looks like the following:

string mystring;
mystring = "The number is " + lexical_cast<short>(5) + " thank you, please";


Errm

lexical_cast<string>(5)

john
Jul 22 '05 #8

"Glen Able" <sm*************@hotMEmail.com> wrote in message
news:cc*******************@news.demon.co.uk...

"Gwar" <xe**@xor.qua> wrote in message
news:20*******************@synergy.transbay.net...
> Obviously the following doesn't work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

int i = 5;
string myString1 = "The number is ";
string myString2 = " thank you, please";

ostringstream os;

os << myString1 << i << myString2;
string msgString = os.str();

cout << msgString << endl;


Eek! Why isn't there anything nice, like a std::string ctor that takes an
int, so I could then do
"blah " + std::string(i) + " blah";


Why would that be a good idea exactly?

If you want an operator to concatenate strings with integers there is
nothing stopping you writing it.

string operator+(const string& lhs, int rhs)
{
...
}

string operator+(int lhs, const string& rhs)
{
...
}

Don't blame me if the results don't match your expectations though.

john
Jul 22 '05 #9
> If you want an operator to concatenate strings with integers there is
nothing stopping you writing it.

string operator+(const string& lhs, int rhs)
{
...
}
Ooh, didn't know you could do global operator overloads, ta.
In my excitement I just tried defining float operator+(float f1, float f2),
but the compiler's on to me :)
Don't blame me if the results don't match your expectations though.

john


Do you have some specific problem in mind, John?

ta,
G.A.
Jul 22 '05 #10
Glen Able wrote:
If you want an operator to concatenate strings with integers there is
nothing stopping you writing it.

string operator+(const string& lhs, int rhs)
{
...
}


Ooh, didn't know you could do global operator overloads, ta.
In my excitement I just tried defining float operator+(float f1, float
f2), but the compiler's on to me :)


You can't overload operators with only built-in types as parameters.

Jul 22 '05 #11
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cc*************@news.t-online.com...
Glen Able wrote:
If you want an operator to concatenate strings with integers there is
nothing stopping you writing it.

string operator+(const string& lhs, int rhs)
{
...
}


Ooh, didn't know you could do global operator overloads, ta.
In my excitement I just tried defining float operator+(float f1, float
f2), but the compiler's on to me :)


You can't overload operators with only built-in types as parameters.


Evidently :)

Shame though. Would have been nice on a few occasions to help me trap dodgy
floating point operations. I did once even try globally redefining 'float'
as 'MyFloat' and supplying all the relevant operators, but ISTR there were
some unresolvable problems which would've meant fixing the usage in numerous
places. Actually, let me start a new thread on that...

cheers,
G.A.
Jul 22 '05 #12

"Glen Able" <sm*************@hotMEmail.com> wrote in message
news:cc*******************@news.demon.co.uk...
Obviously the following doesn't work:

int i = 5;
std::string myString = "Number is " + i + " thankyou please";

So can anyone give me some idea what's the nicest way to this sort of thing? I'm sure it's not using sprintf.

with thanks,
G.A.


Why not consider a templated function? This way you can ostream other types,
not just an int.

// TtoStr.h
#include <string>
#include <sstream>

template<class T> std::string TtoStr(const T& r_t)
{
std::ostringstream ossbuffer;
ossbuffer << r_t;
return ossbuffer.str();
}

// main
#include <iostream>
#include "TtoString.h"

int main()
{
int i = 5;
double d = 2.0;

std::string s("int i = ");
s += TtoStr(i);
s += " and double d = ";
s += TtoStr(d);

std::cout << s << std::endl;

return 0;
}
Jul 22 '05 #13
Glen Able wrote:
Eek! Why isn't there anything nice, like a std::string ctor that
takes an int,


There is. It will create a string of the specified size.
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
5
by: Christopher Benson-Manica | last post by:
Are the C functions atoi() and strtol() (preferred, I'm aware) the only way to convert std::string's to integers, or are there other alternatives I'm not aware of? -- Christopher Benson-Manica ...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
7
by: Marcus Kwok | last post by:
std::string::npos is described in _TC++PL:SE_ (Section 20.3.4) as the "all characters" marker. I tried to use it this way, but my program crashes: #include <iostream> #include <string> int...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.