D
Diego Martins
Since C++ (and STL) have many ways to do string concatenation, I want
to hear (read) from you how you do to concatenate strings with other
strings and other types. The approaches I know are:
-- ostringstream
this does the job well, but:
* all types involved have to support operator<<
* we will lose some readibility in the code because we will always
have to create a temp object to do the concatenation
int n;
float x;
....
ostringstream temp;
temp << "we bought " << n << " items at " << x << " price";
notebook.add(temp.str());
// three liner
-- string:perator+
most advantage is we can built all the string using a one liner and
passing the result to string const &, but:
* we have to ensure all members are of string type
* to achieve that, we can use boost::lexical_cast or custom functions
to convert our types/classes to string
int n;
float x;
....
notebook.add("we bought " + toString(n) + " items at " + toString(x) +
" price");
// one liner!!
(but is there actually a gain doing that?)
and we have to implement toString(). which way is better?
lexical_cast? ostringstream? overloading (or specializing) for each
involved types? (ints and floats can be converted faster using some
hacks)
-- ostringstream wrapped in a (family) of functions
this technique combines the stream versatility with the oneliner
concatenation thing, but without RVO, I am not sure we will gain
anything:
int n;
float x;
....
notebook.add(concat("we bought ",n," items at ",x," price"));
// one liner!!
template<typename A, typename B, typename C, typename D>
string concat(A a, B b, C c, D d) {
ostringstream temp;
temp << a << b << c << d;
return temp.str();
}
this works, but one does not need to be a genius to figure out we have
to define a family of concat functions to handle the different number
of parameters
any ideas?
thanks!!!
Diego
HP
to hear (read) from you how you do to concatenate strings with other
strings and other types. The approaches I know are:
-- ostringstream
this does the job well, but:
* all types involved have to support operator<<
* we will lose some readibility in the code because we will always
have to create a temp object to do the concatenation
int n;
float x;
....
ostringstream temp;
temp << "we bought " << n << " items at " << x << " price";
notebook.add(temp.str());
// three liner
-- string:perator+
most advantage is we can built all the string using a one liner and
passing the result to string const &, but:
* we have to ensure all members are of string type
* to achieve that, we can use boost::lexical_cast or custom functions
to convert our types/classes to string
int n;
float x;
....
notebook.add("we bought " + toString(n) + " items at " + toString(x) +
" price");
// one liner!!
(but is there actually a gain doing that?)
and we have to implement toString(). which way is better?
lexical_cast? ostringstream? overloading (or specializing) for each
involved types? (ints and floats can be converted faster using some
hacks)
-- ostringstream wrapped in a (family) of functions
this technique combines the stream versatility with the oneliner
concatenation thing, but without RVO, I am not sure we will gain
anything:
int n;
float x;
....
notebook.add(concat("we bought ",n," items at ",x," price"));
// one liner!!
template<typename A, typename B, typename C, typename D>
string concat(A a, B b, C c, D d) {
ostringstream temp;
temp << a << b << c << d;
return temp.str();
}
this works, but one does not need to be a genius to figure out we have
to define a family of concat functions to handle the different number
of parameters
any ideas?
thanks!!!
Diego
HP