Let's go straight to the subject, and here's the list of functions I'm going to explain today.
*
1 Find found series
2. Insert Inserts series
3. Extract substr
4. Delete Erase
5. Replace Replacement
*/
Now look at the first one: lookup function.
/*
function name description Find lookup
rfind Reverse lookup
find_first_of find any character in a substring, return to the first position
Find_ First_not_of find any character in a substring, return the first position
find_last_of find any character in the substring, and return to the last position
find_last_not_of finds no characters in a substring, returns the last position.
The above functions are overloaded 4 times, the following is an example of the Find_first_of function to illustrate their arguments, other functions and their parameters, that is, a total of 24 functions.
/* Size_type find_first_of (const basic_string& s, size_type pos = 0)
size_type find_first_of (const chart* S, Size_type Pos, size_type N)
size_type find_first_of (const chart* s, size_type pos = 0)
size_type find_first_o F (CharT c, size_type pos = 0)
* *
All lookup functions return a size_type type, which is generally the position of the found string, and returns String::npos if it is not found. It is important to note that all comparisons with String::npos must be made using string::size_type and not directly using types such as int or unsigned int. In fact, String::npos says-1, look at the header file:
Template <class _chart, class _traits, class _alloc>
const Basic_string<_chart, _traits, _alloc>::size_ Type
Basic_string<_chart, _traits, _alloc>::npos
= Basic_string<_chart, _traits, _alloc>::size_ Type)-1;
Here we'll give you an example of find, and we'll look at the bottom four first.
There is a need to filter all non-English characters at the beginning and end of a line. See how to do this with string:
#include <string>
#include <iostream>
using namespace std;
int main () {
string strinfo = " //*---Hello Word!......------";
String strset = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
INT-A-strinfo.find_first_of (strset);
if (A/= String::npos) {
cout << "not find any characters" << Endl;
return-1;
}
int last = strinfo.find_last_of (strset);
if (last = = String::npos) {
cout << "not find any characters" << Endl;
return-1;
}
cout << Strinfo.substr (Last-first + 1) << Endl;
return 0;
}
/* Output is as follows: Hello Word
* *
Here, write all the letters in English. In order to find the character set, first find the position of the first letter, then find the position of the last English letter, then use the substr to the middle part of the output results. But I think there are just a few places where this function can go. For example, there are some characters in the middle of Hello and word, and you can only write your own function to find it. So the specific problem of specific treatment, in thinking about the problem to consider good, this function although provided to you, so that you more convenient operation of the code, but also want to comprehensive, functional functions and you face the problem is consistent. Or this function can solve your problem, can have other more efficient or more convenient way.
OK, here's a look at the Find function, which is a forward match, that is, finding the first matching character or string.
I'll look at string and char from two directions.
#include <iostream>
#include <string>
#include <algorithm>//find header file
using namespace STD;
int main ()
{
string str1 = ' 123456 ';
The first is a string that finds string
str2 = "34";//can find
string str3 = "364";//Cannot find
int a = Str1.find (str2);
int b = Str1.find (STR3);
cout << a << Endl;
cout << b << Endl;
/*
output is:
2
-1///
second is to find char
char ch1 = ' 3 ' in string;//can find
char CH2 = ' 9 ';
int aa = Str1.find (CH1);
int bb = Str1.find (CH2);
cout << aa << Endl;
cout << BB << endl;
/* Output is:
2
-1 */return
0;
}
Here might say, if you give a character array. For example, look for character char ch= ' 5 ' or char[5]= ' 4567 ' from char a[10]= "123456789".
Http://www.cplusplus.com/reference/cstring/strstr/?kw=strstr This is the explanation of C plus plus.
Let's take a look at the RFind function, and note that this function is a reverse match, that is to find the last match.
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int main ()
{
string str1= "456456";
String str2= "456";
String str3= "654";
int A=str1.rfind (STR2);
int B=str1.rfind (STR3);
cout<<a<<endl;//Output 3
cout<<b<<endl;//output-1 return
0;
}
Second: inserting a function
string& Insert (size_t pos, const string& str);
string& Insert (size_t pos, const string& STR, size_t subpos, size_t sublen = NPOs);
string& Insert (size_t POS, const char* s);
string& Insert (size_t POS, const char* S, size_t N);
string& Insert (size_t pos, size_t N, char c);
This is a common overload.
#include <iostream>
#include <string>
int main ()
{
std::string str= "to be question";
std::string str2= "the";
std::string str3= "or not to be";
Std::string::iterator it;
Used in the same order as described above:
str.insert (6,STR2); The To IS (the) question inserts str2
Str.insert (6,str3,3,4) in the 6th position of STR; To the question in the 6th position of STR, insert a fragment of STR3, which is the 4 characters from the beginning of the subscript 3
Str.insert ("That's cool", 8); To being not (which) the question
Str.insert ("to be"); To being not
the question Str.insert 15,3, ': '. To being not to be (:::) which is the question
std::cout << str << ' \ n ';
return 0;
}
The third one: extract
String substr (size_t pos = 0, size_t len = npos) const;
Simple, just look at the example of C plus plus
String::substr
#include <iostream>
#include <string>
int main ()
{
std::string Str= "We generalities, but we live in details."
std::string str2 = str.substr (3,5); "Do You"
std::size_t pos = Str.find ("Live"); Position of "live" in str
std::string STR3 = Str.substr (POS); Get from ' live ' to the ' end
std::cout << str2 << ' << str3 << ' \ n ';//output: ' I ' live in det Ails.
return 0;
}
Fourth: delete function
string& Erase (size_t pos = 0, size_t len = NPOs)
iterator erase (iterator p)
iterator erase (iterator , iterator last)
Or look at the example of C plus plus.
String::erase
#include <iostream>
#include <string>
int main ()
{
std::string str ("This is a example sentence.");
Std::cout << str << ' \ n ';
"This is a example sentence."
Str.erase (8);
Std::cout << str << ' \ n ';
"This is a sentence."
Std::cout << str << ' \ n ';
"This is a sentence."
Str.erase (Str.begin () + 5, Str.end ()-9);
Std::cout << str << ' \ n ';
"This sentence."
return 0;
}
Fifth: replacing replace
#include <iostream> #include <string> #include <vector> #include <algorithm>//header file using
namespace Std; int main () {/* function prototype: More prototypes, write only two 1.template <class forwarditerator commonly used, class t> void replace (ForwardIterator first
, ForwardIterator last,const t& old_value, const t& new_value);
2.basic_string& replace (size_type pos1,size_type n1,const basic_string& str);
*/String str = "Take a right turn"; Str.replace (7, 5, "left")//replaced by address from &str[7] to &str[12] to left cout << str << Endl;//take a left turn/
/Note that you can also use Find () to identify the place to replace in replace () string S1 = "old";
String s2 = "mature";
String S3 = "The Old Man and the Sea";
int pos = S3.find (S1);
if (POS!= string::npos) s3.replace (POS, s1.size (), S2);
The code above replaces the old with mature//And then lifts an instance int myints[] = {10, 20, 30, 30, 20, 10, 10, 20}; Vector<int> Myvector (myints, myints + 8); (Myvector.begin (), Myvector.end (), 20, 99); 10 9All 20 of 9//myvector are replaced by cout << "myvector contains:";
for (Std::vector<int>::iterator it = Myvector.begin (); it!= myvector.end (); ++it) cout << ' << *it;
cout << ' \ n ';
/* Output: Myvector contains:10/return 0;
}