如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:34:54  来源:igfitidea点击:

How do I convert from stringstream to string in C++?

c++stringstringstream

提问by Nick Bolton

How do I convert from std::stringstreamto std::stringin C++?

我如何在 C++ 中转换std::stringstreamstd::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 of str()(for example in auto *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::stringstreamis a more generic tool. You can use the more specialized class std::ostringstreamfor 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::wstringtype of strings, you must prefer std::wstringstreamor std::wostringstreaminstead.

如果您正在使用std::wstring字符串类型,则必须更喜欢std::wstringstreamstd::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 NumericValuein the examples above can also be passed as std::stringor std::wstringto be used with the std::ostringstreamand std::wostringstreaminstances respectively. It is not necessary for the NumericValueto be a numeric value.

请注意,NumericValue上述示例中的参数也可以分别作为和实例传递std::stringstd::wstringstd::ostringstreamstd::wostringstream实例一起使用。不必NumericValue是数值。

回答by Timo Geusch

From memory, you call stringstream::str()to get the std::stringvalue out.

从内存中,您调用stringstream::str()以获取std::string值。