将 C 样式字符串转换为 C++标准::字符串

发布于 2024-10-14 03:06:27 字数 90 浏览 4 评论 0原文

将 C 样式字符串转换为 C++ std::string 的最佳方法是什么?过去我使用 stringstream 来完成它。有更好的办法吗?

What is the best way to convert a C-style string to a C++ std::string? In the past I've done it using stringstreams. Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

青衫负雪 2024-10-21 03:06:27

一般来说(无需声明新存储),您只需使用 1-arg 构造函数将 c 字符串更改为字符串右值:

string 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

您需要将引用设为常量引用:

void ProcessString(const std::string& username);
ProcessString(std::string("this is a test"));   // works.

In general (without declaring new storage) you can just use the 1-arg constructor to change the c-string into a string rvalue :

string xyz = std::string("this is a test") + 
             std::string(" for the next 60 seconds ") + 
             std::string("of the emergency broadcast system.");

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.

void ProcessString(std::string& username);
ProcessString(std::string("this is a test"));   // fails

You need to make the reference a const reference:

void ProcessString(const std::string& username);
ProcessString(std::string("this is a test"));   // works.
愁以何悠 2024-10-21 03:06:27

现在还有另一种用于 char 数组的方法。与 std::vector 的初始化类似,至少我记得是这样的。

char cBuf[256] = "Hello World";
std::cout << cBuf << '\n';
std::string str{std::begin(cBuf), std::end(cBuf)};
std::cout << str << '\n';

And yet another way for char arrays now. Similar to std::vector's initialization, at least that's how I remember it.

char cBuf[256] = "Hello World";
std::cout << cBuf << '\n';
std::string str{std::begin(cBuf), std::end(cBuf)};
std::cout << str << '\n';
清泪尽 2024-10-21 03:06:27

您可以直接从 C 字符串初始化 std::string

std::string s = "i am a c string";
std::string t = std::string("i am one too");

You can initialise a std::string directly from a c-string:

std::string s = "i am a c string";
std::string t = std::string("i am one too");
烟柳画桥 2024-10-21 03:06:27

如果您的意思是 char*std::string,则可以使用构造函数。

char* a;
std::string s(a);

或者,如果 string s 已经存在,只需编写以下内容:

s=std::string(a);

If you mean char* to std::string, you can use the constructor.

char* a;
std::string s(a);

Or if the string s already exist, simply write this:

s=std::string(a);
丢了幸福的猪 2024-10-21 03:06:27

C++ 字符串有一个构造函数,可让您直接从 C 样式字符串构造 std::string

const char* myStr = "This is a C string!";
std::string myCppString = myStr;

或者,也可以:

std::string myCppString = "This is a C string!";

正如@TrevorHickey 在注释中指出的那样,请小心确保您使用的指针正在初始化 std::string ,但不是空指针。如果是,上面的代码会导致未定义的行为。话又说回来,如果你有一个空指针,人们可能会说你根本就没有字符串。 :-)

C++ strings have a constructor that lets you construct a std::string directly from a C-style string:

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. :-)

红墙和绿瓦 2024-10-21 03:06:27

检查字符串类的不同构造函数:文档
您可能感兴趣:

//string(char* s)
std::string str(cstring);

以及:

//string(char* s, size_t n)
std::string str(cstring, len_str);

Check the different constructors of the string class: documentation
You maybe interested in:

//string(char* s)
std::string str(cstring);

And:

//string(char* s, size_t n)
std::string str(cstring, len_str);
一杯敬自由 2024-10-21 03:06:27

C++11:重载字符串文字运算符

std::string operator ""_s(const char * str, std::size_t len) {
    return std::string(str, len);
}

auto s1 = "abc\0\0def";     // C style string
auto s2 = "abc\0\0def"_s;   // C++ style std::string

C++14:使用 中的运算符std::string_literals 命名空间

using namespace std::string_literals;

auto s3 = "abc\0\0def"s;    // is a std::string

C++11: Overload a string literal operator

std::string operator ""_s(const char * str, std::size_t len) {
    return std::string(str, len);
}

auto s1 = "abc\0\0def";     // C style string
auto s2 = "abc\0\0def"_s;   // C++ style std::string

C++14: Use the operator from std::string_literals namespace

using namespace std::string_literals;

auto s3 = "abc\0\0def"s;    // is a std::string
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文