如何将 char* 指针转换为 C++细绳?

发布于 2024-08-30 00:59:02 字数 195 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

瞎闹 2024-09-06 00:59:02
  1. 获取相当于的 C 字符串
    C++ string 对象使用 c_str
    函数。
  2. 定位第一次出现的
    string 对象中的 char 使用
    find_first_of 函数。

示例:

string s = "abc";

// call to strlen expects char *
cout<<strlen(s.c_str());  // prints 3

// on failure find_first_of return string::npos
if(s.find_first_of('a') != string::npos)
    cout<<s<<" has an a"<<endl;
else
    cout<<s<<" has no a"<<endl;

注意:我给出的 strlen 只是一个采用 char* 的函数示例。

  1. To get the C string equivalent of
    the C++ string object use c_str
    function.
  2. To locate the first occurence of a
    char in a string object use
    find_first_of function.

Example:

string s = "abc";

// call to strlen expects char *
cout<<strlen(s.c_str());  // prints 3

// on failure find_first_of return string::npos
if(s.find_first_of('a') != string::npos)
    cout<<s<<" has an a"<<endl;
else
    cout<<s<<" has no a"<<endl;

Note: I gave the strlen just an example of a function that takes char*.

执笔绘流年 2024-09-06 00:59:02

令人惊讶的是,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.

睫毛溺水了 2024-09-06 00:59:02

您无法从 string 获取 char*

string 不允许您自由访问其内部缓冲区。
您可以获得的最接近的是 const char* 如果您希望它以 null 终止,则使用 .c_str().data() 如果不希望它终止不必以 null 终止。

然后,您可以将这些函数返回的指针强制转换为 char*,但您需要自行承担风险。话虽这么说,只要您确保不更改字符串,这是一个相对安全的转换。如果您更改了它,那么从 c_str() 获得的指针可能不再有效。

这段代码:

string str("Hello World!");
char* sp = (char*)str.c_str();
sp[5] = 'K';

可能没问题
然而,这

string str("Hello World!");
char* sp = (char*)str.c_str();
str = "Chaged string";
sp[5] = 'K';

绝对是不行的。

You can't get a char* from a string

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 from c_str() may no longer be valid.

This code:

string str("Hello World!");
char* sp = (char*)str.c_str();
sp[5] = 'K';

is probably ok
However this:

string str("Hello World!");
char* sp = (char*)str.c_str();
str = "Chaged string";
sp[5] = 'K';

is most definitely not ok.

巴黎夜雨 2024-09-06 00:59:02

如果您只想将字符串文字分配给 pw,则可以像

char *pw = "Hello world";

如果您有一个 C++ std::string 对象 一样,将其值分配给 pw

strong>,你可以像char *pw = some_string.c_str()

,但是,pw 指向的值仅在 some_string 的生命周期内有效。

这里有更多:
如何在 C++ 中将字符串分配给 char *pw< /a>


祝你好运!!

If you just want to assign a string literal to pw, you can do it like

char *pw = "Hello world";

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.

More here :
How to assign a string to char *pw in c++

GoodLUCK!!

终弃我 2024-09-06 00:59:02
std::string yourString("just an example");
char* charPtr = new char[yourString.size()+1];
strcpy(charPtr, yourString.c_str());
std::string yourString("just an example");
char* charPtr = new char[yourString.size()+1];
strcpy(charPtr, yourString.c_str());
梦里兽 2024-09-06 00:59:02

如果字符串中存在 str,请使用 str.c_str() 方法获取其中的 char* 。

If str in your string use str.c_str() method to get the char* inside it.

溇涏 2024-09-06 00:59:02

也许这个例子会对你有所帮助

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Replace the vowels in this sentence by asterisks.");
  size_t found;

  found=str.find_first_of("aeiou");
  while (found!=string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  cout << str << endl;

  return 0;
}

Perhaps this exmaple will help you

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Replace the vowels in this sentence by asterisks.");
  size_t found;

  found=str.find_first_of("aeiou");
  while (found!=string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  cout << str << endl;

  return 0;
}
入怼 2024-09-06 00:59:02

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() and data(). 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 function find()of class sttd::basic_string instead of strchr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文