如何将 std::string 转换为 C 风格的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10958437/
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 14:40:20  来源:igfitidea点击:

How to convert an std::string to C-style string

c++stringcstringstdstring

提问by user1413793

I am programming in C++.

我正在用 C++ 编程。

As basic as this question is I cannot seem to find an answer for it anywhere. So here is the problem:

与这个问题一样基本,我似乎无法在任何地方找到答案。所以这里的问题是:

I want to create a C-style string however I want to put an integer variable i into the string. So naturally, I used a stream:

我想创建一个 C 风格的字符串,但是我想将一个整数变量 i 放入字符串中。所以很自然地,我使用了一个流:

stringstream foo;
foo
                << "blah blah blah blah... i = " 
                << i
                << " blah blah... ";

However, I need to somehow get a C-style string to pass to a function (turns out foo.str() returns an std::string). So this is technically a three part question --

但是,我需要以某种方式获得一个 C 风格的字符串来传递给一个函数(结果 foo.str() 返回一个 std::string)。所以这在技术上是一个由三部分组成的问题——

1) How do I convert std::string to a C-style string?

1) 如何将 std::string 转换为 C 风格的字符串?

2) Is there a way to get a C-style string from a stringstream?

2) 有没有办法从字符串流中获取 C 风格的字符串?

3) Is there a way to use a C-style string directly (without using stringstreams) to construct a string with an integer variable in it?

3)有没有办法直接使用C风格的字符串(不使用stringstreams)来构造一个带有整数变量的字符串?

回答by K-ballo

1) How do I convert std::string to a C-style string?

1) 如何将 std::string 转换为 C 风格的字符串?

Simply call string.c_str()to get a char const*. If you need a mutable_C-style_ string then make a copy. The returned C stringwill be valid as long as you don't call any non-constfunction of string.

只需调用string.c_str()即可获取char const*. 如果您需要一个可变的_C-style_ 字符串,请复制一份。只要您不调用 的任何非常量函数,返回的C 字符串就是有效的。string

2) Is there a way to get a C-style string from a stringstream?

2) 有没有办法从字符串流中获取 C 风格的字符串?

There is, simply strstream.str().c_str(). The returned C stringwill be valid only until the end of the expression that contains it, that means that is valid to use it as a function argument but not to be stored in a variable for later access.

有,简直了strstream.str().c_str()。返回的C 字符串仅在包含它的表达式结束之前有效,这意味着将其用作函数参数是有效的,但不能存储在变量中以供以后访问。

3) Is there a way to use a C-style string directly (without using stringstreams) to construct a string with an integer variable in it?

3)有没有办法直接使用C风格的字符串(不使用stringstreams)来构造一个带有整数变量的字符串?

There is the C way, using sprintfand the like.

C方式,使用sprintf等。

回答by Jerry Coffin

You got halfway there. You need foo.str().c_str();

你已经成功了一半。你需要foo.str().c_str();

  1. your_string.c-str()-- but this doesn't really convert, it just gives you a pointer to a buffer that (temporarily) holds the same content as the string.
  2. Yes, above, foo.str().c_str().
  3. Yes, but you're generally better off avoiding such things. You'd basically be in the "real programmers can write C in any language" trap.
  1. your_string.c-str()-- 但这并没有真正转换,它只是为您提供一个指向缓冲区的指针,该缓冲区(暂时)保存与字符串相同的内容。
  2. 是的,上面,foo.str().c_str()
  3. 是的,但您通常最好避免此类事情。您基本上会陷入“真正的程序员可以用任何语言编写 C”的陷阱。

回答by CPlusSharp

  1. this is realy simple

    #include <afx.h>
    std::string s("Hello");
    CString cs(s.c_str());
    
  2. no

  3. yo can use sprintfto format a c-style string

  1. 这真的很简单

    #include <afx.h>
    std::string s("Hello");
    CString cs(s.c_str());
    
  2. 你可以使用sprintf来格式化一个 c 风格的字符串