c ++中c_str函数的用途是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7416445/
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
What is use of c_str function In c++
提问by Amit Singh Tomar
I have just started reading C++ and found c++ having rich functions for string manipulation which C does not have. I am reading these function and came across c_str()
and from what I understand is c_str
convert a string which may be null terminated or may not be to a null terminated string .Is it true?
我刚刚开始阅读 C++,发现 C++ 具有 C 没有的丰富的字符串操作功能。我正在阅读这些函数,c_str()
并根据我的理解c_str
将字符串转换为空终止或可能不是空终止的字符串。这是真的吗?
Can anyone suggest me some example so that i can understand the use of c_strfunction??
谁能给我一些例子,以便我可以理解c_str函数的使用??
回答by Jon
c_str
returns a const char*
that points to a null-terminated string (i.e. a C-style string). It is useful when you want to pass the "contents"1 of an std::string
to a function that expects to work with a C-style string.
c_str
返回const char*
指向以空字符结尾的字符串(即 C 样式字符串)的 。当您想将 an 的“内容”1 传递std::string
给希望使用 C 样式字符串的函数时,它很有用。
For example, consider this code:
例如,考虑以下代码:
std::string str("Hello world!");
int pos1 = str.find_first_of('w');
int pos2 = strchr(str.c_str(), 'w') - str.c_str();
if (pos1 == pos2) {
printf("Both ways give the same result.\n");
}
Notes:
笔记:
1 This is not entirely true because an std::string
(unlike a C string) can contain the \0
character. If it does, the code that receives the return value of c_str()
will be fooled into thinking that the string is shorter than it really is, since it will interpret \0
as the end of the string.
1 这并不完全正确,因为一个std::string
(与 C 字符串不同)可以包含该\0
字符。如果是这样,接收 的返回值的代码c_str()
将被愚弄,认为字符串比实际短,因为它将解释\0
为字符串的结尾。
回答by hkBattousai
In C++, you define your strings as
在 C++ 中,您将字符串定义为
std::string MyString;
std::string MyString;
instead of
代替
char MyString[20];
.
char MyString[20];
.
While writing C++ code, you encounter some C functions which require C string as parameter.
Like below:
在编写 C++ 代码时,您会遇到一些需要 C 字符串作为参数的 C 函数。
像下面这样:
void IAmACFunction(int abc, float bcd, const char * cstring);
void IAmACFunction(int abc, float bcd, const char * cstring);
Now there is a problem. You are working with C++ and you are using std::string
string variables. But this C function is asking for a C string. How do you convert your std::string
to a standard C string?
现在有一个问题。您正在使用 C++ 并且您正在使用std::string
字符串变量。但是这个 C 函数需要一个 C 字符串。你如何将你的转换std::string
为标准的 C 字符串?
Like this:
像这样:
std::string MyString;
// ...
MyString = "Hello world!";
// ...
IAmACFunction(5, 2.45f, MyString.c_str());
This is what c_str()
is for.
这c_str()
就是为了。
Note that, for std::wstring
strings, c_str()
returns a const w_char *
.
请注意,对于std::wstring
字符串,c_str()
返回一个const w_char *
.
回答by Daniel Mo?mondor
Most OLD c++ and c functions, when deal with strings, use const char*
.
With STL and std::string
, string.c_str()
is introduced to be able to convert from std::string
to const char*
.
大多数旧的 c++ 和 c 函数,在处理字符串时,使用const char*
.
使用 STL 和std::string
,string.c_str()
引入可以从std::string
到const char*
.
That means that if you promise not to change the buffer, you'll be able to use read only string contents. PROMISE = constchar*
这意味着如果您承诺不更改缓冲区,您将能够使用只读字符串内容。承诺 =常量字符*
回答by pmr
It's used to make std::string
interoperable with C code that requires a null terminated char*
.
它用于std::string
与需要空终止的 C 代码进行互操作char*
。
回答by CadentOrange
c_str() converts a C++ string into a C-style string which is essentially a null terminated array of bytes. You use it when you want to pass a C++ string into a function that expects a C-style string (e.g. a lot of the Win32 API, POSIX style functions, etc).
c_str() 将 C++ 字符串转换为 C 风格的字符串,它本质上是一个以空字符结尾的字节数组。当您想将 C++ 字符串传递给需要 C 样式字符串的函数时(例如,许多 Win32 API、POSIX 样式函数等),您可以使用它。
回答by Linkon Ruhul
In C/C++ programming there are two types of strings: the C strings and the standard strings. With the <string>
header, we can use the standard strings. On the other hand, the C strings are just an array of normal chars. So, in order to convert a standard string to a C string, we use the c_str()
function.
在 C/C++ 编程中有两种类型的字符串:C 字符串和标准字符串。通过<string>
标题,我们可以使用标准字符串。另一方面,C 字符串只是一个普通字符数组。因此,为了将标准字符串转换为 C 字符串,我们使用了该c_str()
函数。
for example
例如
// a string to a C-style string conversion//
const char *cstr1 = str1.c_str();
cout<<"Operation: *cstr1 = str1.c_str()"<<endl;
cout<<"The C-style string c_str1 is: "<<cstr1<<endl;
cout<<"\nOperation: strlen(cstr1)"<<endl;
cout<<"The length of C-style string str1 = "<<strlen(cstr1)<<endl;
And the output will be,
输出将是,
Operation: *cstr1 = str1.c_str()
The C-style string c_str1 is: Testing the c_str
Operation: strlen(cstr1)
The length of C-style string str1 = 17
回答by Li haonan
Oh must add my own pick here, you will use this when you encode/decode some string obj you transfer between two programs.
哦,必须在这里添加我自己的选择,当您编码/解码在两个程序之间传输的一些字符串 obj 时,您将使用它。
Lets say you use base64encode some array in python, and then you want to decode that into c++. Once you have the string you decode from base64decode in c++. In order to get it back to array of float, all you need to do here is
假设您在 Python 中使用 base64encode 对某个数组进行编码,然后您想将其解码为 C++。一旦你有了从 c++ 中的 base64decode 解码的字符串。为了让它回到浮点数组,你需要在这里做的就是
float arr[1024];
memcpy(arr, ur_string.c_str(), sizeof(float) * 1024);
This is pretty common use I suppose.
我想这是很常见的用法。