Formating STL strings.
In JAVA you can format a string by using the operators + += = to concatenate other types such as floats, ints etc... i.e:
String myString;
myString = "Hello you owe me " + 5.50 + " dollars!";
Is there something similar for the STL string?
If I wanted a reference I could have used the msdn also
Is there a way to do the below?
The following will not compile....
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2784: ''class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const _E *,const class std::basic_string<_E,_Tr,_A> &)'' : could not deduce template argument for ''const *'' from ''class std::bas
ic_string,class std::allocator >''
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2784: ''class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const class std::basic_string<_E,_Tr,_A> &,const class std::basic_string<_E,_Tr,_A> &)'' : could not deduce template argument for
''const class std::basic_string<_E,_Tr,_A> &'' from ''class std::basic_string,class std::allocator >''
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2784: ''class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)'' : could not deduce template argument for '''' from ''clas
s std::basic_string,class std::allocator >''
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2676: binary ''+'' : ''class std::basic_string,class std::allocator >'' does not define this operator or a conversion to a type acceptable to the predefined
operator
Error executing cl.exe.
Test.exe - 7 error(s), 0 warning(s)
Is there a way to do the below?
The following will not compile....
#include <iostream>#include <string>using namespace std;void main(){ string aString; aString = "Hello World."; aString = aString + " Dood" + 5 + "blah!"; cout << aString << endl;}
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2784: ''class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const _E *,const class std::basic_string<_E,_Tr,_A> &)'' : could not deduce template argument for ''const *'' from ''class std::bas
ic_string,class std::allocator >''
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2784: ''class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const class std::basic_string<_E,_Tr,_A> &,const class std::basic_string<_E,_Tr,_A> &)'' : could not deduce template argument for
''const class std::basic_string<_E,_Tr,_A> &'' from ''class std::basic_string,class std::allocator >''
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2784: ''class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)'' : could not deduce template argument for '''' from ''clas
s std::basic_string,class std::allocator >''
D:\Programming\Projects\CPP\Test\main.cpp(12) : error C2676: binary ''+'' : ''class std::basic_string,class std::allocator >'' does not define this operator or a conversion to a type acceptable to the predefined
operator
Error executing cl.exe.
Test.exe - 7 error(s), 0 warning(s)
You can''t do like that. You have to do it one at a time
But you are limited to strings and chars. You have to use ostringstream to format other data types.
The reason why Java can do that is because it converts everything to a string object before applying +. This is one of the reason why Java is slower.
string s;s += "hello";s += " 5.50 ";s += "world";
But you are limited to strings and chars. You have to use ostringstream to format other data types.
The reason why Java can do that is because it converts everything to a string object before applying +. This is one of the reason why Java is slower.
You could make C++ just as slow as Java!
Also, if you want formatted input into strings (as opposed to just string concatenation), look at ostringstream.
string aString ("Hello World.");aString += string (" Dood") + string ("5") + string ("blah!");
Also, if you want formatted input into strings (as opposed to just string concatenation), look at ostringstream.
That is slightly different because the 5 is a string, not a integer. Only the ostringstream and Java can format non char types.
No I want to format integer and floats into a string just like JAVA so I guess it would be the ostringstream?
Yes.
For converting one-off values, you could do it like this:
But if you find yourself using 3 or 4 calls to ToString in a single statement or expression, you''d find it better to explicitly create the ostringstream and format everything in there once, then get the string out at the end with str().
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
For converting one-off values, you could do it like this:
template<class T>string ToString(T value){ ostringstream ss; ss << T; return ss.str();}
But if you find yourself using 3 or 4 calls to ToString in a single statement or expression, you''d find it better to explicitly create the ostringstream and format everything in there once, then get the string out at the end with str().
[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement