C++ 如何将 std::string 转换为 const char*?

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

How to convert std::string to const char*?

c++

提问by Richard Knop

Possible Duplicate:
Convert std::string to const char* or char*

可能的重复:
将 std::string 转换为 const char* 或 char*

void Foo::bar(const std::string& foobar) {
    // ...
    const char* foobar2 = (char*)foobar;
    // ...
}

That does not work and I get an error during compilation about invalid casting.

这不起作用,我在编译期间收到有关无效转换的错误。

Is there some other way to convert std::stringto const char*?

有没有其他方法可以转换std::stringconst char*

回答by

Use foobar.c_str().

使用 foobar.c_str().

You might find this link useful: http://www.cppreference.com/wiki/string/start

您可能会发现此链接很有用:http: //www.cppreference.com/wiki/string/start

回答by wkl

std::string::c_str()gets you a const char*pointer to a character array that represents the string (null-terminated).

std::string::c_str()为您提供一个const char*指向表示字符串(以空字符结尾)的字符数组的指针。

You should not manipulate the data this pointer points to, so if you need to do that, copy the data.

您不应该操作此指针指向的数据,因此如果您需要这样做,请复制数据。

Double edit - doing it in a more C++ fashion

双重编辑 - 以更 C++ 的方式进行

Since it is nicer to avoid the use of raw pointers and arrays where possible, you can also get the data into an std::vector<char>

由于在可能的情况下避免使用原始指针和数组更好,因此您还可以将数据放入 std::vector<char>

#include <string>
#include <vector>

int main()
{
    std::string str = "Hello";
    std::vector<char> cvec(str.begin(), str.end()); 

    // do stuff
}

edit this is more like C since it uses raw pointers and explicitly allocates mem

编辑这更像 C,因为它使用原始指针并显式分配内存

#include <string>
#include <cstring>

int main()
{
    std::string str = "Hello";
    char *cptr = new char[str.size()+1]; // +1 to account for 
class Something {
    const char* name;
public:
    Something(const std::string& pname) {
        this->name = pname.c_str(); /* wrong! the pointer will go wrong as the object from the parameter ceases to exist */
    }
};
byte std::strncpy(cptr, str.c_str(), str.size()); // do stuff... delete [] cptr; }

回答by Kos

You're going to get a lot of kinda incorrectanswers about str.c_str()here. :) While c_str()is indeed useful, please keep in mind that this will not actually convert the string into a char*, but rather return the contents of the string as a const char*. And this is a big difference!

你会在这里得到很多有点不正确的答案str.c_str()。:) 虽然c_str()确实很有用,但请记住,这实际上不会将字符串转换为 achar*,而是将字符串的内容作为 a 返回const char*这是一个很大的不同!

What's important here is that the pointer you obtain from c_str()is valid only as long as the given stringobject exists. So this would be terribly wrong:

这里重要的是,您从中获取的指针c_str()仅在给定string对象存在时才有效。所以这将是非常错误的

char* convert(const std::string& str) {
    char* result = new char[str.length()+1];
    strcpy(result,str.c_str());
    return result;
}

So if you want to convert, as in: create a new value which will be independent of the original std::string, then you'll want to do something like this:

因此,如果您想进行转换,例如:创建一个独立于原始值的新值std::string,那么您将需要执行以下操作:

const char* foobar2 = foobar.c_str();

But still c_str()will be quite enough for you in most cases. Just try to think in terms of objects' time of life.

c_str()在大多数情况下对你来说仍然足够了。试着从对象的生命周期来思考。

回答by fabrizioM

##代码##

Notice the const. Otherwise you have to copy it to a char buffer.

注意const。否则,您必须将其复制到字符缓冲区。