Road C ++ string class learning --string specific usage

introduction:

C ++ greatly enhanced support for strings, except C-style strings can be used, you can also use the built-in string type. string from the string class process a lot easier, can replace the character array or string pointer in the C language.
string is commonly used in a C ++ class, it is very important, we need to explain here alone.

definition

Use string class files need to include, the following example describes several ways to define a string variable (object):

#include <iostream>
#include <string>
using namespace std;

int main()
{
string s1;
    string s2 = "c plus plus";
    string s3 = s2;
    string s4 (5, 's');
    return 0;
}

Variable s1 just defined but is not initialized, the compiler will assign default values s1, default value is "", i.e., an empty string.
While the definition of variable s2 is initialized to the "c plus plus". C-style strings and different, not the end of the string marks the end of '\ 0'.
Variable s3 performed when initializing defined directly s2, s3 and therefore also the content "c plus plus".
S4 variables are initialized by the string 5 's' characters, i.e. "sssss".
As can be seen from the above code, string variables directly by the assignment operator = assignment. string variables can also be carried out with C-style string assignment, e.g., s2 is initialized with a string constant, and s3 is initialized by the variable s2.
C-style strings and different, when we need to know the length of the string, you can call length () function string class. As follows:

string s = "abcdefg";
int len = s.length();
cout<<len<<endl;

7 is output. Since there is no '\ 0' character at the end of the string, the length () returns true length of the string, rather than the length of the +1.

O string string

string class overloads the operator input and output, can be treated like a regular string variable as variables, which is input with >>, << for output. Consider the following code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s;
    cin>>s;  //输入字符串
    cout<<s<<endl;  //输出字符串
    return 0;
}
运行结果:
abc abc
abc

Although we have entered two spaces separated by a URL, but only one output, because the input operator >> default ignores spaces, spaces encountered is considered the input end, so abc last entered is not stored in the variable s.

Access the string of characters

string String may be the same as C-style string according subscripts to access each character therein. Subscript string starting from the beginning of the string is still 0. Consider the following code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s = "1234567890";
    for(int i=0,len=s.length(); i<len; i++){
        cout<<s[i]<<" ";
    }
    cout<<endl;
    s[5] = '5';
    cout<<s<<endl;
    return 0;
}
运行结果:
1 2 3 4 5 6 7 8 9 0
1234557890

This example defines a string variable s, and assigned "1234567890", followed by a for loop iterates each output character. Index means, in addition to access each character, each character may be modified, s [5] = '5'; 6, will modify the character '5', the last of s is "1234557890."

String splicing

With the string class, we can use + or + = string concatenation operator directly, very convenient, no longer need to use the C language strcat (), strcpy (), malloc () function and the like to splice the string , no longer have to worry about lack of space will overflow.
+ When used to splice the string, the operator may be on both sides of string is a string, the string may be a string and a C-style string, a string may be a character string and an array or a string and a single character string. Consider the following example:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1 = "first ";
    string s2 = "second ";
    char *s3 = "third ";
    char s4[] = "fourth ";
    char ch = '@';
    string s5 = s1 + s2;
    string s6 = s1 + s3;
    string s7 = s1 + s4;
    string s8 = s1 + ch;
    cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl;
    return 0;
}
运行结果:
first second
first third
first fourth
first @

Deletions string search string change

C ++ string class provided contains a number of useful member functions, which greatly facilitates the increase of the string, delete, change, query and other operations.
A string inserted
insert () function can be specified in string string string into another position, which is a prototype:
string & INSERT (POS size_t, const string & STR);
POS represents the position to be inserted, and is the subscript; STR represents the string to be inserted, it may be a string string, it may be C-style string.
Consider the following code:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s1, s2, s3;
    s1 = s2 = "1234567890";
    s3 = "aaa";
    s1.insert(5, s3);
    cout<< s1 <<endl;
    s2.insert(5, "bbb");
    cout<< s2 <<endl;
    return 0;
}
运行结果:
12345aaa67890
12345bbb67890

insert () function's first argument is likely to cross the border, if cross-border, a runtime exception will be generated.

II. Delete the string

erase () function to delete a substring of string. It is a prototype:
String & ERASE (size_t POS = 0, size_t len = NPoS);
POS represents the substring starting index to be deleted, len indicates the length of the substring to be deleted. If len is not specified, then delete all the characters from pos to the end of the string (At this point len = str.length - pos).
Consider the following code:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s1, s2, s3;
    s1 = s2 = s3 = "1234567890";
    s2.erase(5);
    s3.erase(5, 3);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    cout<< s3 <<endl;
    return 0;
}
运行结果:
1234567890
12345
1234590

Some readers worry that in the case of pos parameter is not out of bounds, len parameter may also cause the substring to delete cross-border. In practice this does not happen, erase () function will be taken from a minimum of two values as the length of the substring to be deleted:
value for len;
string length by subtracting the value of pos.
Some put it simply, the string can be deleted to delete up to the end of the string.

III. Extract a substring

substr () function is used to extract a substring from a string in a string, it is the prototype:
string substr (size_t POS = 0, len = NPoS size_t) const;
POS is to be extracted substring starting index, len is the length of the substring to be extracted.
Consider the following code:

#include <iostream>
#include <string>
using namespace std;
int main(){
  string s1 = "first second third";
    string s2;
    s2 = s1.substr(6, 6);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    return 0;
}
运行结果:
first second third
second

System substr () processing parameters and erase () like this:
if pos is out of range, an exception is thrown;
if len cross-border, will extract all the characters from pos to at the end of the string.

IV. String search

string class provides several functions associated with the search string, as shown below.
. 1) find () function
find () function is used to find the position of the substring occurring in the string string, wherein its two prototype:
size_t Find (const & string STR, size_t POS = 0) const;
size_t Find ( const char * s, size_t pos = 0) const;
the first parameter is the substring to be searched, it may be a string string, it may be C-style string. The second parameter to start looking for a position (subscript); if not specified, the characters from 0 to start looking.

Consider the following code:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.find(s2,5);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}
运行结果:
Found at index : 6

find () function returns the final time in the first substring starting index string. This embodiment of the lower subscript 6 is ultimately found s2 string. If you do not find the substring, it returns an infinite value of 4294967295.
2) rfind () function
rfind () and find () is very similar, the same is to find substrings in the string, except that find () function to find the next start the second parameter, and rfind () function is find the most to the second argument, the second argument if the specified index has not been found substring, an infinite value of 4294967295 is returned.
Consider the following example:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.rfind(s2,6);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}
运行结果:
Found at index : 6

3) find_first_of () function
find_first_of () function is used to find the location of the sub-strings and string together have the character first appeared in the string. Consider the following code:

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s1 = "first second second third";
    string s2 = "asecond";
    int index = s1.find_first_of(s2);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}
运行结果:
Found at index : 3

S1 and s2 in the present embodiment have in common is the character 's', the first occurrence of the character in the subscripts s1 3, 3 so that the search results returned.

Released eight original articles · won praise 5 · views 37

Guess you like

Origin blog.csdn.net/weixin_45525272/article/details/104284087
Recommended