Additional string operations

In addition to co-operation with sequential containers, stringtype also offers a number of additional operations.

Other methods of construction string

char noNull[] = { 'H','i' };
string s1(cp);      //拷贝cp中的值,直到遇到空字符
string s2(noNull,2);    //从noNull拷贝两个字符
string s3(noNull);  //未定义,noNull不是以空字符结束
string s4(cp+6,5);  //从cp[6]开始,拷贝5个字符
string s5(s1,6,5);  //从s1[6]开始,拷贝5个字符
string s6(s1,6);    //从s1[6]开始,直到s1末尾
string s7(s1,6,20); //从s1[6]开始,直到s1末尾
string s8(s1, 16);  //抛出一个out_of_range异常

When const char*created string, the array must point to point is null-terminated, the null character copy operation stops. But if passed to the constructor only one count, it does not have the array is null-terminated. Two kinds of undefined situations:

  • Do not pass the calculated values ​​and the array is not null-terminated.
  • Calcd given, but the count is greater than the size of the array.

From stringcopying, it can be provided with an optional start position and a count value must be less than or equal to a start position of a given stringsize, if larger than a given stringsize, thrown out_of_rangeexception. If you pass a count value from a given location to start copying so much character. No matter how many characters required copies, up to the standard library copy to stringthe end.

substr

substrA return string, which is the original stringpart or all of the non-copy, can be passed to substran optional start position and the count value.

string s("Hello World");
string s2 = s.substr(0, 5);
string s3 = s.substr(6);
string s4 = s.substr(6, 11);
string s5 = s.substr(12);   //抛出 out_of_range 异常

If the starting position over stringthe size, the substrfunction will throw out_of_rangean exception.

If the count is greater than plus start position from the stringobtained size, substradjusts the count value, only copy to stringthe end.

Other methods of changing the string

In addition to receiving the iterator version insert, eraseoutside, stringit is also provided under the accepted version of the subject. Subscript indicates the start position deleted or position before the given value to insert into.

s.insert(s.size(),5,'!');   //在s的末尾插入5个感叹号
s.erase(s.size()-5,5);      //从s删除最后5个字符

The standard library stringtype is also provided to accept C-style array of characters insertand assignversions. For example, an array of characters will be null character insertto or assignto one string.

const char* cp = "Stately,plump Buck";
string s;
s.assign(cp,7); 
s.insert(s.size(),cp+7);

Required assignnumber of characters assigned to be less than or equal cpcharacter size of the array pointed to (excluding the null character at the end).

insert The specified character is inserted before the specified position to.

You can also be specified from other stringcharacter or substring into the current stringor the current imparted string.

string s = "some string";
string s2 = "some other string ";
s.insert(0, s2);    //在s中位置0之前插入s2的拷贝   
//在s[0]之前插入s2中s2[0]开始的s2.size()个字符
s.insert(0,s2,0,s2.size());

append and replace function

stringClass defines two additional member functions: appendand replace. These two functions can change the stringcontent.

appendOperation is stringa shorthand form of insertion end:

string s("C++ Primer"), s2 = s;
s.insert(s.size(), " 4th Ed.");
s2.append(" 4th Ed.");

replaceOperation is invoked eraseand inserta form of:

string s("C++ Primer 4th Ed."), s2 = s;
s.erase(11, 3);
s.insert(11, "5th");
s2.replace(11, 3, "5th");


Change various string of overloaded function

  • assignAlways replace stringall content.
  • appendAlways add a new character to stringthe end.
  • replace You can delete the specified range of elements available in two ways:
    • Range can be specified by a location and a length.
    • It can be specified by an iterator range.
  • insertIt allows two ways to specify the insertion point: subscripting or iterator before the specified index or a new element is inserted into the iterator.

string search operation

stringClass provides six different search elements functions, each has four overloads. Each search operation returns a string::size_typevalue that represents the position of the index match occurs, if the search fails, called the return string::nposof staticmembers.

Standard library nposis defined as a const string::size_typetype, and initializes the value -1. Since this means that the initial value nposequal to any stringmaximum possible size. Since the search function returns string::size_typea value, the type is a unsignedtype, therefore, with a intor with other types of symbols class holds these functions return value is not a good idea.

findThe simplest function to complete the search. It looks for the specified string parameter, if found, the index of the first matching position is returned, otherwise npos.

string name("AnnaBelle");
auto pos1 = name.find("Anna");  //0

To find a location in the given string matches any one character:

string numbers("0123456789"), name("r2d2");
auto pos = name.find_first_of(numbers);     //1

Search for the first parameter is not in the amount of characters:

string numbers("0123456789");
string dept("03714p3");
auto pos = dept.find_first_not_of(numbers);  //5

Specify where to start search

Operation can be passed to find an alternative starting position, this optional parameter specifies the location from which to start the search, by default, this location is set to 0.

A common programming model is the optional parameter in the string loop to search all of the string that appears:

string numbers("0123456789"), name("r2d2");
string::size_type pos = 0;
while((pos = name.find_first_of(numbers,pos))!= string::npos)
{
    cout << "found number at index:" << pos
    << " element is " << name[pos] << endl;
    ++pos;
}

The second step from the beginning, from the posstart point to search for the character, and this character is a number, and therefore find_first_ofwill be repeated return pos.

Reverse Search

The library also provides a search operation from right to left:

  • rfind Member function searches the last match, that the child appears rightmost position of the string.
  • find_last_ofSearch and given stringin any of the last character in a character to match.
  • find_last_not_ofFinally, do not appear in a search given stringof characters.
string river("Mississippi");
auto first_pos = river.find("is");  //1
auto last_pos = river.rfind("is");  //4

compare function

stringProviding a set of comparefunctions that the C function strcmpfunction is similar, according to s is equal to, greater than, less than the specified character string, s.comparereturns 0, or negative integer.

Numeric conversion

Data and standard library stringconversion between.

If you stringcan not be converted to a numeric value, these functions throw an invalid_argumentexception.

If the converted value can not be represented by any type, it throws an out_of_rangeexception.

Guess you like

Origin www.cnblogs.com/xiaojianliu/p/12497211.html