J
jl_post
Hi,
A few months back I remember reading through C++ newsgroups trying
to find a way to quickly convert a number to a C++ std::string. I
often see code like:
// Create a string that holds a number:
int num = 7; // for example
char tmp[24];
sprintf(tmp, "%d", num);
const std::string str = tmp;
but I wanted to see if there was a better way. I generally would use
this:
// Create a string that holds a number:
int num = 7;
std:stringstream outStream; // #include <sstream>
outStream << num;
const std::string str = outStream.str();
and this works great, but just like the first example, it declares and
uses a temporary variable/object. What I'd have liked to see was some
sort of method in std::string that handled the conversion in one line,
like this:
// NOTE: not real code!
int num = 7;
const std::string str = std::string::number(num);
While I didn't find what I was looking for, I did find an
interesting post, where someone posted this nice, simple function:
// Converts many things to a std::string:
#include <sstream>
template <typename T>
std::string toStdString(const T &thing)
{
std:stringstream os;
os << thing;
return os.str();
}
Using this function, I can now write code like this:
int num = 7;
const std::string str = toStdString(num);
This code is very clear, but it also has another benefit: Not only
can I easily convert integers and floats to a std::string, but I can
convert pretty much anything that I can send to std::cout.
But then I thought of something: Why even define a templated
function? I could write code like this:
int num = 7;
std::string str = (std:stringstream() << num).str();
It may not be as elegant as the toStdString() solution, but at least
it won't require me to define (or include a header file of) the
toStdString() function in every program I write. In addition, there's
no need to introduce a temporary variable to the current scope.
However, when I tried to compile this code, I got the following
error message:
testfile.cpp: In function `int main(int, char**)':
testfile.cpp:23: error: 'struct std::basic_ostream<char,
std::char_traits<char> >' has no member named 'str'
I can't figure out why I'm getting this error message. I mean, I'm
declaring a std:stringstream object, using the "<<" operator on it
(which, I believe, returns the very same std:stringstream object),
and then I call the ::str() method on it. std:stringstream HAS to
have ::str() defined, otherwise the toStdString() function above
wouldn't compile.
What puzzles me is that I've successfully compiled similar things
with std::strings, like this:
// Prints "Hello, world!":
const char* word1 = "Hello";
const char* word2 = "world";
printf("%s", (word1 + std::string(", ") + word2 + "!\n").c_str());
and have had no problems with it. (Basically, word1+std::string(", ")
+word2+"!\n" returns a std::string, and the ::c_str() method is called
on it, passing its internal data to printf() to use before the
std::string goes out of scope.)
I would think that what I'm trying to do with std:stringstream is
really no different, so it puzzles me why (std:stringstream() <<
num).str() generates a compiler error. Does anybody know why this
generates a compiler error?
Thanks in advance for any help.
-- Jean-Luc
A few months back I remember reading through C++ newsgroups trying
to find a way to quickly convert a number to a C++ std::string. I
often see code like:
// Create a string that holds a number:
int num = 7; // for example
char tmp[24];
sprintf(tmp, "%d", num);
const std::string str = tmp;
but I wanted to see if there was a better way. I generally would use
this:
// Create a string that holds a number:
int num = 7;
std:stringstream outStream; // #include <sstream>
outStream << num;
const std::string str = outStream.str();
and this works great, but just like the first example, it declares and
uses a temporary variable/object. What I'd have liked to see was some
sort of method in std::string that handled the conversion in one line,
like this:
// NOTE: not real code!
int num = 7;
const std::string str = std::string::number(num);
While I didn't find what I was looking for, I did find an
interesting post, where someone posted this nice, simple function:
// Converts many things to a std::string:
#include <sstream>
template <typename T>
std::string toStdString(const T &thing)
{
std:stringstream os;
os << thing;
return os.str();
}
Using this function, I can now write code like this:
int num = 7;
const std::string str = toStdString(num);
This code is very clear, but it also has another benefit: Not only
can I easily convert integers and floats to a std::string, but I can
convert pretty much anything that I can send to std::cout.
But then I thought of something: Why even define a templated
function? I could write code like this:
int num = 7;
std::string str = (std:stringstream() << num).str();
It may not be as elegant as the toStdString() solution, but at least
it won't require me to define (or include a header file of) the
toStdString() function in every program I write. In addition, there's
no need to introduce a temporary variable to the current scope.
However, when I tried to compile this code, I got the following
error message:
testfile.cpp: In function `int main(int, char**)':
testfile.cpp:23: error: 'struct std::basic_ostream<char,
std::char_traits<char> >' has no member named 'str'
I can't figure out why I'm getting this error message. I mean, I'm
declaring a std:stringstream object, using the "<<" operator on it
(which, I believe, returns the very same std:stringstream object),
and then I call the ::str() method on it. std:stringstream HAS to
have ::str() defined, otherwise the toStdString() function above
wouldn't compile.
What puzzles me is that I've successfully compiled similar things
with std::strings, like this:
// Prints "Hello, world!":
const char* word1 = "Hello";
const char* word2 = "world";
printf("%s", (word1 + std::string(", ") + word2 + "!\n").c_str());
and have had no problems with it. (Basically, word1+std::string(", ")
+word2+"!\n" returns a std::string, and the ::c_str() method is called
on it, passing its internal data to printf() to use before the
std::string goes out of scope.)
I would think that what I'm trying to do with std:stringstream is
really no different, so it puzzles me why (std:stringstream() <<
num).str() generates a compiler error. Does anybody know why this
generates a compiler error?
Thanks in advance for any help.
-- Jean-Luc