Skip to main content
GameDev.net gamedev.net
🔒 Locked

Formating STL strings.

Started by ANSI2000 Jan 3, 2002 at 8:28 AM 6 replies 400+ views
Original Post
ANSI2000
ANSI2000
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?
NuffSaid
NuffSaid
The same is available to the STL string class. here''s the documentation
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
ANSI2000
ANSI2000
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....

  
#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)
Void
Void
You can''t do like that. You have to do it one at a time

  
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.
Stoffel
Stoffel
You could make C++ just as slow as Java!
  
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.
Void
Void
That is slightly different because the 5 is a string, not a integer. Only the ostringstream and Java can format non char types.
ANSI2000
ANSI2000
No I want to format integer and floats into a string just like JAVA so I guess it would be the ostringstream?
Kylotan
Kylotan
Yes.

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 ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.