Real-combat conversion of String series--string and char*, const char * in C + + (data () or C_STR ())

Source: Internet
Author: User

In the project, we also have a lot of time to use the conversion between string and char*, here is a function we mentioned before C_str (), look at this prototype:

constchar *c_str();

The C_STR () function returns a pointer to the normal C string with the same contents as this string string.
You see, the return value is a const char*, which you need to be aware of.

1 String Turn const char*
Of course, the method described above is used C_STR ():

string"abcdeg";constchar *k = s1.c_str();cout<<k<endl;

There is another way:
Data (): Similar to C_str (), but the returned array is not terminated with a null character.

2 String Turn char*
You can use strcpy:

string"what fucking day"len = s.length();c =newchar[len+1];strcpy(c,s.c_str());

You can also use copy:

int main(){  std::string foo("quuuux");  char bar[7];  sizeof bar);  bar[6‘\0‘;  std::cout‘\n‘;}

3 Const char* Turn string
4 char* Turn string
It's simple:

char* c ="abc";string s(c);

================================================================
The difference between data and C_str is mentioned above, so what is the difference?
Both prototypes:

Const VALUE_TYPE *C_STR () const;
Const VALUE_TYPE *data () const;

Data simply returns the original data sequence, and there is no guarantee that Traits::eos (), or '% ', will be used to end the string. Of course, most implementations may have done so.
C_STR is a standard practice, the returned char* must point to a valid C-compatible string terminated with '/'.
Therefore, if a C-compatible string is required, C_STR is the standard practice, and data does not guarantee the consistency of all STL implementations.

You might ask, c_str () 's function contains data (), what else does it need to do with the data () function? Look at the source code:

constconst{   if  0)        return"";   terminate ();   return data ();}

The original process for C_STR () is to call terminate () first and then return data (). So if you have a higher efficiency requirement and your processing doesn't have to end in a way, you'd better choose data (). However, for the general C function, you need to use the const char* as the input parameter, you will be using the C_STR () function.
For the C_STR () data () function, the returned array is owned by the string itself, and its contents must never be modified. The reason for this is that many string implementations use a reference mechanism, that is, it is possible to have several strings using the same character storage space. And you can't use sizeof (string) to see its size. Detailed explanations and implementations see effective STL clause 15: Beware of the diversity of string implementations.
In addition, in your program, only use C_STR () or data () to get the string, each call, the next time the use will be invalidated, such as:

string strinfo("this is Winter");..." Hello!";foo(pstr);//错误!

What errors will you encounter? When you're lucky pstr may just point to "this is Winter hello!" String, if not lucky, will cause the program to have other problems, there are always some errors can not be met. It's not going to be the result that you expected anyway.

Real-combat conversion of String series--string and char*, const char * in C + + (data () or C_STR ())

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.