将 C 风格的字符串转换为 C++ std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4764897/
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
Converting a C-style string to a C++ std::string
提问by Ian Burris
What is the best way to convert a C-style string to a C++ std::string
? In the past I've done it using stringstream
s. Is there a better way?
将 C 样式字符串转换为 C++ 的最佳方法是什么std::string
?过去我使用stringstream
s完成了它。有没有更好的办法?
回答by templatetypedef
C++ strings have a constructor that lets you construct a std::string
directly from a C-style string:
C++ 字符串有一个构造函数,可以让你std::string
直接从 C 风格的字符串构造一个:
const char* myStr = "This is a C string!";
std::string myCppString = myStr;
Or, alternatively:
或者,或者:
std::string myCppString = "This is a C string!";
As @TrevorHickey notes in the comments, be careful to make sure that the pointer you're initializing the std::string
with isn't a null pointer. If it is, the above code leads to undefined behavior. Then again, if you have a null pointer, one could argue that you don't even have a string at all. :-)
正如@TrevorHickey 在评论中指出的那样,请注意确保您正在初始化的std::string
指针不是空指针。如果是,上面的代码会导致未定义的行为。再说一次,如果您有一个空指针,人们可能会争辩说您甚至根本没有字符串。:-)
回答by Santiago Alessandri
Check the different constructors of the string class: documentationYou maybe interested in:
检查字符串类的不同构造函数:文档您可能感兴趣:
//string(char* s)
std::string str(cstring);
And:
和:
//string(char* s, size_t n)
std::string str(cstring, len_str);
回答by Shreevardhan
C++11
: Overload a string literal operator
C++11
: 重载字符串文字运算符
std::string operator ""_s(const char * str, std::size_t len) {
return std::string(str, len);
}
auto s1 = "abcusing namespace std::string_literals;
auto s3 = "abcchar* a;
std::string s(a);
s=std::string(a);
def"s; // is a std::string
std::string s = "i am a c string";
std::string t = std::string("i am one too");
def"; // C style string
auto s2 = "abcstring xyz = std::string("this is a test") +
std::string(" for the next 60 seconds ") +
std::string("of the emergency broadcast system.");
void ProcessString(std::string& username);
ProcessString(std::string("this is a test")); // fails
def"_s; // C++ style std::string
C++14
: Use the operator from std::string_literals
namespace
C++14
: 使用std::string_literals
命名空间中的操作符
void ProcessString(const std::string& username);
ProcessString(std::string("this is a test")); // works.
回答by Manas
If you mean char*
to std::string
, you can use the constructor.
如果你char*
想std::string
,你可以使用构造函数。
Or if the string s
already exist, simply write this:
或者如果string s
已经存在,只需写下:
回答by trojanfoe
You can initialise a std::string
directly from a c-string:
您可以std::string
直接从 c 字符串初始化 a :
回答by systemBuilder
In general (without declaring new storage) you can just use the 1-arg constructor to change the c-string into a string rvalue :
通常(不声明新存储)您可以只使用 1-arg 构造函数将 c-string 更改为字符串 rvalue :
##代码##However, this does not work when constructing the string to pass it by reference to a function (a problem I just ran into), e.g.
但是,这在构造字符串以通过引用函数传递它时不起作用(我刚刚遇到的问题),例如
##代码##You need to make the reference a const reference:
您需要将引用设为常量引用:
##代码##