C + + string of those pits, C + + string function supplement (type cross, split, merge, slimming), C + + string Memory essence (simple and straightforward test)

Source: Internet
Author: User
Tags sprintf

1. Size_type find_first_of (const basic_string &STR, Size_type index = 0);

Finds the first character in a string that matches a character in Str and returns its position. Search starts from index and returns String::npos if not found

2. string& Replace (size_t pos, size_t len, const string& str);

Starting at the POS position of the current string, len-length paragraph, replacing Chengcheng str

3. int compare (const string& str)

The result is 0, which means that the strings are equal, equivalent to the = between the strings

4. The difference between data () and C_STR ()

Data () is the return character array, which may or may not be at the tail.

C_str () is the return C-compatible string, and the tail must have ' s '

5. The difference between at () and []

[] not checked out of bounds, no exception thrown, high efficiency

at () Check out of bounds, throw exceptions, high security

6.size_t copy (char* s, size_t len, size_t pos = 0) const;

Copies the contents of the strign to an external character array, instead of copying the outer string to String7.size () and length () size () to return a string, which is the number of bytes, not the number of characters. String ignores what encoding is used for internal characters, and it is treated as a collection of bytes. Size () and length () function and meaning, the former is to cooperate with the STL standard add interface. Http://www.cnblogs.com/guoxiaoqian/p/4275959.html
intStringutil::intfromstring (stringdata) {    //NOTE Atoi is a non-standard C function    returnatoi (Data.c_str ());}stringStringutil::stringfromint (intdata) {    Chartmp[ One]; memset (TMP,0, One); sprintf (TMP,"%10d", data); return string(TMP);}DoubleStringutil::d oublefromstring (stringdata) {    Doubletmp; SSCANF (Data.c_str (),"%LF",&tmp); returntmp;}stringStringutil::stringfromdouble (Doubledata) {    Chartmp[ +]; memset (TMP,0, +); sprintf (TMP,"%20.3LF", data); return string(TMP);}floatStringutil::floatfromstring (std::stringdata) {    floattmp; SSCANF (Data.c_str (),"%f",&tmp); returntmp;} STD::stringStringutil::stringfromfloat (floatdata) {    Chartmp[ +]; memset (TMP,0, +); sprintf (TMP,"%20.3f", data); return string(TMP);}BOOLStringutil::boolfromstring (std::stringdata) {    if(Data.compare ("true") ==0)        return true; Else        return false;} STD::stringStringutil::stringfrombool (BOOLdata) {    if(data)return string("true"); Else        return string("false");} Vector&LT;STD::string> Stringutil::splitstringtoarray (std::stringSOURCE, std::stringseperator) {Vector<string>result; if(!Source.empty ()) {        string:: Size_type begin =0; string:: Size_type end =0; unsignedintSepsize =seperator.size ();  while(end = Source.find_first_of (Seperator,begin))! =string:: NPOs) {            stringItem = SOURCE.SUBSTR (begin,end-begin);            Result.push_back (item); Begin=end +sepsize; }        //Last item, note that the final element is an empty string if the last is a separator        if(Begin <=source.size ()) {            stringItem = SOURCE.SUBSTR (Begin,source.size ()-begin);        Result.push_back (item); }    }    returnresult;} STD::stringStringutil::linkarraytostring (VECTOR&LT;STD::string> Array, std::stringseperator) {    stringresult; if(Array.size () >0) {unsignedintLimit = Array.size ()-1;  for(unsignedintI=0;i< limit;++i) {result+=Array[i]; Result+=seperator; } result+=Array[limit]; }    returnresult;} Map&LT;STD::string, std::string> Stringutil::splitstringtomap (std::stringSOURCE, std::stringPRIMARYSEP, std::stringsecondarysep) {Vector<string> array =Stringutil::splitstringtoarray (SOURCE,PRIMARYSEP); Vector<string>Tmparray; Map<string,string>result;  for(unsignedinti =0; i< array.size (); + +i) {Tmparray=Stringutil::splitstringtoarray (ARRAY[I],SECONDARYSEP); if(Tmparray.size () >=2) {result[tmparray[0]] = tmparray[1]; }    }    returnresult;} STD::stringStringutil::linkmaptostring (MAP&LT;STD::string, std::string> Tmpmap, std::stringPRIMARYSEP, std::stringsecondarysep) {Vector<string>Tmparray; stringTmpstr; Map<string,string>::iterator it =Tmpmap.begin ();  for(; It!=tmpmap.end (); + +it) {Tmpstr= it->first+secondarysep+it->second;    Tmparray.push_back (TMPSTR); }    returnstringutil::linkarraytostring (TMPARRAY,PRIMARYSEP);} STD::stringStringutil::trimfront (std::stringdata) {unsignedinti =0;  for(; I<data.size () &&data.at (i) = =' ';++i) {}if(i<data.size ())returnData.substr (I,data.size ()-i); Else        return string("");} STD::stringStringutil::trimback (std::stringdata) {    inti = data.size ()-1;  for(; i>=0&&data.at (i) = =' ';--i) {}if(i>=0)        returnDATA.SUBSTR (0, i+1); Else        return string("");} STD::stringStringutil::trim (std::stringdata) {    stringTMP =Stringutil::trimfront (data); returnStringutil::trimback (TMP);}

Http://www.cnblogs.com/guoxiaoqian/p/4113339.html

Although the source code of string has not been studied, it can be determined that the memory space of string is opened up on the heap, it is responsible for freeing up space without our relationship.

We use a dynamically assigned string pointer to initialize a string object retstr, it will do a copy process, the string into the Retstr own memory space, and then Retstr has nothing to do with RET, so we have to remember to release RET:

1 char* ret = (char*) malloc (LEN_STR); 2 memset (RET,0,LEN_STR); 3//operate ret  ... 4 string retstr (ret); 5 free (ret);

Http://www.cnblogs.com/guoxiaoqian/p/3944805.html

C + + string of those pits, C + + string function supplement (type cross, split, merge, slimming), C + + string Memory essence (simple and straightforward test)

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.