std::string and integers

G

Glen Able

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.
 
G

Gwar

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;

}
 
A

Anders Persson

try
int i = 5;
std::string myString;
myString = "Number is " + i + " thankyou please";
// ANders
 
R

Rolf Magnus

Anders said:
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.
 
G

Glen Able

Gwar said:
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";
 
R

Rolf Magnus

Glen said:
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.
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Glen said:
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-----
 
J

John Harrison

Glen Able said:
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
 
G

Glen Able

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.
 
R

Rolf Magnus

Glen said:
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.
 
G

Glen Able

Rolf Magnus said:
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.
 
S

SaltPeter

Glen Able said:
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::eek:stringstream 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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top