如何将 char* 指针转换为 C++细绳?
我有一个 C++ 字符串
。我需要将此字符串传递给接受 char*
参数的函数(例如 - strchr()
)。
a) 我如何获得该指针?
b) 是否有一些与 strschr()
等效的函数适用于 C++ 字符串
?
I have a C++ string
. I need to pass this string to a function accepting a char*
parameter (for example - strchr()
).
a) How do I get that pointer?
b) Is there some function equivalent to strschr()
that works for C++ strings
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
C
字符串C++
string
对象使用c_str
函数。
string
对象中的char
使用find_first_of
函数。示例:
注意:我给出的
strlen
只是一个采用char*
的函数示例。C
string equivalent ofthe
C++
string
object usec_str
function.
char
in astring
object usefind_first_of
function.Example:
Note: I gave the
strlen
just an example of a function that takeschar*
.令人惊讶的是,std:;string 的功能比 C 风格字符串要多得多。您可能需要 find_first_of() 方法。一般来说,如果您发现自己在 C++ std::strings 上使用 strxxx() 函数,那么您几乎肯定做错了什么。
与 C++ 标准库的大部分内容一样,字符串类是一个复杂的野兽。为了充分利用它,您确实需要一本好的参考书。我推荐 C++ 标准库,作者:Nicolai Josuttis。
Surprisingly, std:;string has far, far more capabilities than C-style strings. You probably want the find_first_of() method. In general, if you find yourself using the strxxx() functions on C++ std::strings, you are almost certainly doing something wrong.
Like much of the C++ Standard Library, the string class is a complex beast. To make the most of it, you really need a good reference book. I recommend The C++ Standard Library, by Nicolai Josuttis.
您无法从
string
获取char*
string
不允许您自由访问其内部缓冲区。您可以获得的最接近的是
const char*
如果您希望它以 null 终止,则使用.c_str()
或.data()
如果不希望它终止不必以 null 终止。然后,您可以将这些函数返回的指针强制转换为 char*,但您需要自行承担风险。话虽这么说,只要您确保不更改字符串,这是一个相对安全的转换。如果您更改了它,那么从
c_str()
获得的指针可能不再有效。这段代码:
可能没问题
然而,这
绝对是不行的。
You can't get a
char*
from astring
string
does not allow you free access to its internal buffer.The closest you can get is a
const char*
using.c_str()
if you want it null terminated or.data()
if it doesn't have to be null terminated.You can then cast the pointer returned by these functions to
char*
but you do this on your own risk. That being said this is a relatively safe cast to make as long as you make sure you're not changing the string. If you changed it then the pointer you got fromc_str()
may no longer be valid.This code:
is probably ok
However this:
is most definitely not ok.
如果您只想将字符串文字分配给 pw,则可以像
如果您有一个 C++ std::string 对象 一样,将其值分配给 pw
strong>,你可以像
char *pw = some_string.c_str()
,但是,pw 指向的值仅在 some_string 的生命周期内有效。
祝你好运!!
If you just want to assign a string literal to pw, you can do it like
If you have a C++ std::string object, the value of which you want to assign to pw, you can do it like
char *pw = some_string.c_str()
However, the value that pw points to will only be valid for the life time of some_string.
GoodLUCK!!
如果字符串中存在 str,请使用 str.c_str() 方法获取其中的 char* 。
If str in your string use
str.c_str()
method to get the char* inside it.也许这个例子会对你有所帮助
Perhaps this exmaple will help you
C++ 标准提供了两个 std::basic_string 类的成员函数,它们返回指向字符串第一个元素的指针。它们是
c_str()
和data()
。但两者都返回 const char *,因此您不能将它们与具有 char * 类型参数的函数一起使用。对于函数strchr,它的第一个参数是const char *。因此,您可以将 c_str() 和 data() 与此函数一起使用。不过,最好使用 sttd::basic_string 类的成员函数
find()
而不是strchr。
The C++ Standard provides two member functions of claass std::basic_string that return pointer to the first element of the string. They are
c_str()
anddata()
. But the both return const char *, So you may not use them with a function that has parameter of type char *.As for function strchr then its first parameter is
const char *
. So you may use c_str() and data() with this function. However it is much better to use member functionfind()
of class sttd::basic_string instead ofstrchr.