如何将std::string转换为char*或const char*?
当前回答
char* result = strcpy((char*)malloc(str.length()+1), str.c_str());
其他回答
如果你只是想将std::string传递给一个需要const char *的函数,你可以使用.c_str():
std::string str;
const char * c = str.c_str();
如果你需要一个非const char *,调用.data():
std::string str;
char * c = str.data();
.data()是在c++ 17中添加的。在此之前,你可以使用&str[0]。
注意,如果std::string是const, .data()将返回const char *,就像.c_str()一样。
如果字符串被销毁或重新分配内存,指针将失效。
指针指向一个以空结束的字符串,终止符不计入str.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不可复制)。
看看这个:
string str1("stackoverflow");
const char * str2 = str1.c_str();
但是,请注意,这将返回一个const char *。
对于char *,使用strcpy将其复制到另一个char数组中。
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
需要字符串复制 错误的最大责任/易感性 复杂的
比方说, 字符串str =“堆栈”;
1)将字符串转换为char*
char* s_rw=&str[0];
上面的字符*(即。, s_rw)是可读可写的,并且指向基 需要转换为char*的字符串的地址
2)将字符串转换为const char*
const char* s_r=&str[0];
上面的const char*(即s_r)是可读的但不可写的,并且指向 字符串的基址。