How to convert a char* pointer into a C++ string?

2019-03-19 07:27发布

问题:

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?

回答1:

  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*.



回答2:

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.



回答3:

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.



回答4:

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!!



回答5:

std::string yourString("just an example");
char* charPtr = new char[yourString.size()+1];
strcpy(charPtr, yourString.c_str());


回答6:

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



回答7:

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;
}


回答8:

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.