如何在 C++ 中从字符串流转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/662976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert from stringstream to string in C++?
提问by Nick Bolton
How do I convert from std::stringstream
to std::string
in C++?
我如何在 C++ 中转换std::stringstream
为std::string
?
Do I need to call a method on the string stream?
我需要在字符串流上调用一个方法吗?
回答by Tyler McHenry
???????
???????
yourStringStream.str()
回答by Emil H
Use the .str()-method:
使用.str() 方法:
Manages the contents of the underlying string object.
1) Returns a copy of the underlying string as if by calling
rdbuf()->str()
.2) Replaces the contents of the underlying string as if by calling
rdbuf()->str(new_str)
...Notes
The copy of the underlying string returned by str is a temporary object that will be destructed at the end of the expression, so directly calling
c_str()
on the result ofstr()
(for example inauto *ptr = out.str().c_str();
) results in a dangling pointer...
管理基础字符串对象的内容。
1) 返回底层字符串的副本,就像调用
rdbuf()->str()
.2) 替换底层字符串的内容,就像通过调用
rdbuf()->str(new_str)
...笔记
由STR返回的基础字符串的副本,将在表达式的结尾被破坏,所以直接调用临时对象
c_str()
上的结果str()
(例如,在auto *ptr = out.str().c_str();
以悬空指针)结果...
回答by hkBattousai
std::stringstream::str()
is the method you are looking for.
std::stringstream::str()
是您正在寻找的方法。
With std::stringstream
:
与std::stringstream
:
template <class T>
std::string YourClass::NumericToString(const T & NumericValue)
{
std::stringstream ss;
ss << NumericValue;
return ss.str();
}
std::stringstream
is a more generic tool. You can use the more specialized class std::ostringstream
for this specific job.
std::stringstream
是一个更通用的工具。您可以std::ostringstream
针对此特定作业使用更专业的类。
template <class T>
std::string YourClass::NumericToString(const T & NumericValue)
{
std::ostringstream oss;
oss << NumericValue;
return oss.str();
}
If you are working with std::wstring
type of strings, you must prefer std::wstringstream
or std::wostringstream
instead.
如果您正在使用std::wstring
字符串类型,则必须更喜欢std::wstringstream
或std::wostringstream
代替。
template <class T>
std::wstring YourClass::NumericToString(const T & NumericValue)
{
std::wostringstream woss;
woss << NumericValue;
return woss.str();
}
if you want the character type of your string could be run-time selectable, you should also make it a template variable.
如果您希望字符串的字符类型可以在运行时选择,您还应该将其设为模板变量。
template <class CharType, class NumType>
std::basic_string<CharType> YourClass::NumericToString(const NumType & NumericValue)
{
std::basic_ostringstream<CharType> oss;
oss << NumericValue;
return oss.str();
}
For all the methods above, you must include the following two header files.
对于上述所有方法,您必须包含以下两个头文件。
#include <string>
#include <sstream>
Note that, the argument NumericValue
in the examples above can also be passed as std::string
or std::wstring
to be used with the std::ostringstream
and std::wostringstream
instances respectively. It is not necessary for the NumericValue
to be a numeric value.
请注意,NumericValue
上述示例中的参数也可以分别作为和实例传递std::string
或std::wstring
与std::ostringstream
和std::wostringstream
实例一起使用。不必NumericValue
是数值。
回答by Timo Geusch
From memory, you call stringstream::str()
to get the std::string
value out.
从内存中,您调用stringstream::str()
以获取std::string
值。