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. 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
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;
}
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.
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.
"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";
-----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-----
> 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
"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
> 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.
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.
"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.
"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;
}
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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(...
|
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 ...
|
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;
...
|
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...
|
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...
|
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!";...
|
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...
|
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 ""
//...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |