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

Source: Internet
Author: User

In 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, the same as this string string.
You see, the return value is a const char*, which you need to pay attention to here.

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 one more way:
Data (): Similar to C_str (). However, the returned array is not terminated with a null character.

2 String Turn char*
Ability to 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 series. There is no guarantee that the end of the string will be done with Traits::eos (), or '/. ' Of course, most implementations may have done so.
C_STR is the standard practice. The returned char* must point to a valid C-compatible string terminated with '/'.


So, assuming that C-compatible strings are required, C_STR is the standard practice, and data does not guarantee the consistency of all STL implementations.

You might ask. The function of C_STR () includes data (), what does it need to do with the data () function? Look at the source code:

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

The original C_STR () process is: Call Terminate () first. The data () is then returned.

So assume that you have a higher efficiency requirement, and that your processing does not necessarily end in a way. You'd better choose data (). However, for the General C function, the const char* is required as the input parameter. You will use the C_STR () function.
For the C_STR () data () function, the returned array is owned by the string itself and must not be altered by its contents.

The reason for this is that many of the string implementations use a reference mechanism, that is to say. It is possible that several strings use the same character storage space. And you cannot use sizeof (string) to see its size. Specific explanations and implementations see effective STL clause 15: Beware of the diversity of string implementations.
Also in your program, use C_STR () or data () to get the string, once per call, only when needed. The next time you use it, it will fail, such as:

string strinfo("this is Winter");...//最好的方式是:foo(strinfo.c_str());//也能够这么用:const char* pstr=strinfo.c_str();foo(pstr);//不要再使用了pstr了, 以下的操作已经使pstr无效了。

" Hello!";foo(pstr);//错误!

What errors will you encounter? When you're lucky pstr may just be pointing to "This is Winter hello!" The string, assuming not fortunate. Can cause other problems in the program, there will always be some errors that cannot 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 ())

Related Article

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.