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


当前回答

char* result = strcpy((char*)malloc(str.length()+1), str.c_str());

其他回答

C++17

c++ 17(即将推出的标准)修改了basic_string模板的概要,添加了非const重载data():

图表*数据()noexcept; 返回:一个指针p,对于[0,size()]中的每个i, p + i == &运算符。


CharT const * from std::basic_string<CharT>

std::string const cstr = { "..." };
char const * p = cstr.data(); // or .c_str()

从std::basic_string<图表>

std::string str = { "..." };
char * p = str.data();

C++11

CharT const * from std::basic_string<CharT>

std::string str = { "..." };
str.c_str();

从std::basic_string<图表>

从c++ 11开始,标准说:

The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size(). const_reference operator[](size_type pos) const; reference operator[](size_type pos); Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type CharT with value CharT(); the referenced value shall not be modified. const charT* c_str() const noexcept;const charT* data() const noexcept; Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].

有多种方法可以获得非const字符指针。

1. 使用c++ 11的连续存储

std::string foo{"text"};
auto p = &*foo.begin();

Pro

简单而简短 快速(唯一不涉及复制的方法)

Cons

Final '\0'不会被改变/不一定是非const内存的一部分。

2. 使用std::向量<图>

std::string foo{"text"};
std::vector<char> fcv(foo.data(), foo.data()+foo.size()+1u);
auto p = fcv.data();

Pro

简单的 自动内存处理 动态

Cons

需要字符串复制

3.使用std::array<CharT, N>如果N是编译时间常数(并且足够小)

std::string foo{"text"};
std::array<char, 5u> fca;
std::copy(foo.data(), foo.data()+foo.size()+1u, fca.begin());

Pro

简单的 堆栈内存处理

Cons

静态 需要字符串复制

4. 原始内存分配与自动存储删除

std::string foo{ "text" };
auto p = std::make_unique<char[]>(foo.size()+1u);
std::copy(foo.data(), foo.data() + foo.size() + 1u, &p[0]);

Pro

内存占用小 自动删除 简单的

Cons

需要字符串复制 静态(动态使用需要大量代码) 特征比向量或数组少

5. 使用手动处理的原始内存分配

std::string foo{ "text" };
char * p = nullptr;
try
{
  p = new char[foo.size() + 1u];
  std::copy(foo.data(), foo.data() + foo.size() + 1u, p);
  // handle stuff with p
  delete[] p;
}
catch (...)
{
  if (p) { delete[] p; }
  throw;
}

Pro

最大的“控制”

Con

需要字符串复制 错误的最大责任/易感性 复杂的

char* result = strcpy((char*)malloc(str.length()+1), str.c_str());

试试这个

std::string s(reinterpret_cast<const char *>(Data), Size);

我正在使用一个API,其中有很多函数获得char*作为输入。

我创建了一个小类来处理这类问题,并且实现了RAII习惯用法。

class DeepString
{
        DeepString(const DeepString& other);
        DeepString& operator=(const DeepString& other);
        char* internal_; 
    
    public:
        explicit DeepString( const string& toCopy): 
            internal_(new char[toCopy.size()+1]) 
        {
            strcpy(internal_,toCopy.c_str());
        }
        ~DeepString() { delete[] internal_; }
        char* str() const { return internal_; }
        const char* c_str()  const { return internal_; }
};

你可以这样使用它:

void aFunctionAPI(char* input);

//  other stuff

aFunctionAPI("Foo"); //this call is not safe. if the function modified the 
                     //literal string the program will crash
std::string myFoo("Foo");
aFunctionAPI(myFoo.c_str()); //this is not compiling
aFunctionAPI(const_cast<char*>(myFoo.c_str())); //this is not safe std::string 
                                                //implement reference counting and 
                                                //it may change the value of other
                                                //strings as well.
DeepString myDeepFoo(myFoo);
aFunctionAPI(myFoo.str()); //this is fine

我将这个类称为DeepString,因为它正在创建一个现有字符串的深度且唯一的副本(DeepString不可复制)。

从c++ std字符串转换到c风格字符串现在真的很容易。

为此,我们有string::copy函数,它可以轻松地将std字符串转换为C风格字符串。参考

字符串::连续复制函数形参

Char字符串指针 字符串大小,b复制多少个字符 位置,从字符复制开始的位置

另一件重要的事,

此函数不会在操作结束时附加空字符。所以,我们需要手动放置它。

代码考试在下面

// char string
char chText[20];

// c++ string
string text =  "I am a Programmer";

// conversion from c++ string to char string
// this function does not append a null character at the end of operation
text.copy(chText, text.size(), 0);

// we need to put it manually
chText[text.size()] = '\0';

// below statement prints "I am a Programmer"
cout << chText << endl;

反之亦然,从C样式字符串转换到c++ std字符串要容易得多

有三种方法可以将C样式字符串转换为c++ std字符串

第一个是使用构造函数,

char chText[20] = "I am a Programmer";
// using constructor
string text(chText);

第二个是使用string::assign方法

// char string
char chText[20] = "I am a Programmer";

// c++ string
string text;

// convertion from char string to c++ string
// using assign function
text.assign(chText);

第三个是赋值操作符(=),其中字符串类使用操作符重载

// char string
char chText[20] = "I am a Programmer";

// c++ string
// convertion from char string to c++ string using assignment operator overloading
string text = chText;

第三个也可以写成如下-

// char string
char chText[20] = "I am a Programmer";

// c++ string
string text;


// convertion from char string to c++ string
text = chText;