C ++ string detailed usage

string is not STL containers (know that when I was surprised), but it has many similarities with the STL container operations, do not worry about the length of the problem, but also a wide variety of packaging methods, very easy to use.


Use the library

#include  <string> 

statement

The most basic:

string str; 

This statement is an empty string

There are a variety of other methods:

  • string s; // creates an empty string s
  • string s (str); // copy constructor generated replica of str
  • string s (str, stridx); // inner string str "starting position stridx" as the initial value of the string portion
  • string s (str, stridx, strlen); // inner string str "begins and a length of at most stridx strlen" as the initial value of the string portion
  • string s (cstr); // the C string (NULL terminated) as the initial value s.
  • string s (chars, chars_len); // front chars_len character string C as the initial value of the string s.
  • string s (num, 'c'); // generates a string containing a character c num
  • string s ( "value"); string s = "value"; // initializes s to copy a string literal
  • string s (begin, end); // character interval to begin / end (not included end) as the initial value in the string s
  • s ~ string ();. // destroy all the characters, the release of memory

I wrote an example, easy to understand:

#include<bits/stdc++.h>

using namespace std; 


int main(){  
    int n,i;  
    char cstr[10]="cstr";
    string str1="qwertyui";
    string str2(str1);
    string str3(str1,1);
    string str4(str1,1,2);
    string str5(cstr);
    string str6(cstr,2);
    string str7(5,'a');
    string str8("zxcv");
    string str9(str1.begin(),str1.end());
    cout<<str1<<endl<<str2<<endl<<str3<<endl<<str4<<endl<<str5<<endl<<str6<<endl<<str7<<endl<<str8<<endl<<str9<<endl;
    str1.~string();

}

Results are as follows:

qwertyui
qwertyui
wertyui
we
cstr
cs
aaaaa
zxcv
qwertyui

Compared with the C array of characters

Online said a lot, I feel the most immediate difference, if there s s string of three characters, the traditional C [3] is '\ 0' character, but C ++ is the only string to s [2] this character only.

FIG different specific uses:

We can see the string is more convenient in terms of the assignment, as well as stitching comparisons.

Operation string object

String s;
 . 1 ) s.empty ();   // s Returns the empty string to true 
2 ) s.size ();   // returns the number of characters in s Type should be: :: String size_type 
. 3 ) s [n-];   // starting from 0 corresponds to indexed access 
. 4 ) s1 + s2;   // the s1 and s2 is connected to a new string and returns the new string 
. 5 ) s1 = s2;   // replace the copy of s1 to s2 
. 6 ) V1 V2 == ;   // comparison, equal returns to true 
. 7 ) `=, <, <=,>,> =` customary operation of any of a capital letter lowercase less than any!

When the string literal string objects and mixing the connecting operation, about + operator must have at least one operand of type string:

String S1 ( "Hello");
 String S3 = S1 + "World";   // legal operation 
String S4 = "Hello" + "World";   // Payload: adding two string literal

Insert Character

Maybe you need to insert somewhere in the middle of the string string, this time you can use the insert () function, which requires you to specify an index position of placement, the string will be inserted on the back of this index.
s.insert (0, "My name");
s.insert (. 1, STR);
This form of insert () function does not support incoming single character, then the character must be written in a single string. In order to insert a single character, insert () function provides two single character insertion operation overloaded functions:
INSERT (size_type index, size_type NUM, Chart C) and insert (iterator pos, size_type num, chart c).
Size_type which is an unsigned integer, iterator is a char *, so, so you call the insert function does not work:
insert (0, 1, 'J'); this time will be converted to the first argument, which one do?
So you have to write so: insert ((string :: size_type) 0, 1, 'j')!
The second form points out the form using an iterator placement of characters.

Substring extraction s.substr ()

s.substr (); // Returns the entire contents of s 
s.substr ( 11 ); // the index 11 subsequent substring 
s.substr ( . 5 , . 6 ); // starting at index 56 characters

string to int conversion

int n=1;
    str1="100";
    stringstream stream;
    stream<<str1;
    stream>>n;
    cout<<n<<endl;

reference:

https://blog.csdn.net/manonghouyiming/article/details/79827040

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11333009.html