School recruit knowledge summary C ++ (two) string

The question now is really a multi-string, c ++ string library is very powerful, but has a function which is not very understanding, so this summarize string library functions.

A, string and other types of conversion

  1. C- conversion between the string and style.

string str = "abc";
const char * =str.c_str()
View Code

  Note that, the return type is const, conversion if necessary, a new need c ++ out to convert to a very const_cast amount of the pointer. the socket transmission data, frequently used function.

  

 const char * s = "hello,world";
char d[] = "hello,world";
string str(s);
string str1(d);
cout<<str.length()<<str1.length();
cout<< sizeof(d)<<endl;

  The need to pay attention C- style strings and string different places. C- plurality of style '\ 0';

  2. and int, double, long, float conversion.

  Beginning:. Atoi (const char *) atol () atof () function These parameters are cost char *.

  Another advantage of header file #include <sstream>. This space is used to deal with a good point. Type conversion processing, almost meaning.

  c ++ 11 added:. stoi (string) stol (string) is a string.

  Conversion in the opposite direction: to_string () function. Commonly used.

 

Two, string length of the member functions.

  

string str ="afdfd";
str.length();
str.size();

 

Three, string find member function.

  1. Find substring The substring index returns.

  

string str = "hello,world";
cout<<str.find("w")<<endl;
cout<<str.rfind("w")<<endl;
cout<<str.find("l")<<endl;
cout<<str.rfind("l")<<endl;
View Code

 

  2. substring taken, according to the following standard, returns the substring.

string  str = "hello,world";
cout<<str.substr(2,5)<<endl;
View Code

  Note that this sustr not return a reference that is a return of the right value, not the change itself str.

Four, string modification (not iterator, although the string is the STL, iterators can also be used, but not deep understanding of the iterator, in order to prevent mistakes, temporarily do not.)

  Note that these changes are a member function returns a reference that is directly changed str, returns a reference, value means that only left to do.  

  1.erase

  

    << str.erase COUT ( . 9 ) << endl; // POS until the last 
    COUT str.erase << ( 0 , . 3 ) << endl; // POS n-

 

  2.insert

cout<<str.insert(1,"strat")<<endl;
cout<<str.insert(1,"strat",2)<<endl;//和replace相同

 

  3.replace

cout<<str.replace(0,1,"huya");// pos n temp
cout << str.replace (0,1, "huya", 2) << endl; // pos n temp n1 n1 representative of the number of characters in the string of this alternative
cout << str.replace (0, str.length (), "hahahhaadfdfdaf") << endl; // guess, what I would like for this experiment
cout<<str.replace(0,str.length()+100,"hahahhaadfdfdaf")<<endl;
cout<<str.length()<<endl;
 

 

  4.append、push_back.

  

<< str.append cout ( " End " ) << endl; 
    str.push_back ( ' L ' ); // Note that you can only add character, and not return references, can not be directly output. 
    cout << str << endl;

 

Five, string comparison

 1. direct comparison of two string. > = <= ==

 2.compare function. Character by character comparison returns greater than 1, equal to 0 returns, less than -1; from    

  

    string str2 = "abcdef";
    string str3 = "abecdefg";
    string str4 = "adfc";
    string str5 = "abc,";
    cout<<str2.compare(str3)<<endl;
    cout<<str2.compare(str4)<<endl;
    cout<<str2.compare(str5)<<endl;

 

Guess you like

Origin www.cnblogs.com/meikon/p/11519167.html