C + + Primer (Fifth edition) learning Note _5_ Standard Template Library string (2)
10. Searching for elements or substrings of a string object
Use the Find () method to find the first character element in a string (char, in single quotation marks) or substring (in double quotation marks), or, if found, to return the subscript value (counting from 0), and if not, return a large number of string:npos (i.e.: 4294967295 )。
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) { string s; s = "Cat dog cat"; Find the first character ' C ', return subscript value cout << s.find (' C ') << Endl; Finds the first substring "C", returns the subscript value cout << s.find ("C") << Endl; Find the first substring "cat", return subscript value cout << s.find ("Cat") << Endl; Finds the first substring "dog", returns the subscript value cout << s.find ("dog") << Endl; Find the first substring "pig", then return to 4294967295 cout << s.find ("Pig") << Endl; if (S.find ("pig") = = String::npos) cout << "not found" << Endl; return 0;}
Operation Result:
0
0
0
4
4294967295
Not found
11. Comparison of String objects
A string object can be compared to other strings using the Compare () method. Returns 1 if it is larger than the other, or 1 if it is smaller than the other, or 0 if it is the same as the other.
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) { string s; s = "Cat dog cat"; Use the Compare symbol ">", "<", "= =" cout << (s = = "Cat Dog cat") << Endl; s larger than "ABC" string, returns 1 cout << s.compare ("abc") << Endl; Exception: S is larger than the "Cat dog C" string, returns 2, finds that the return value is 2 and the remaining value is inconsistent returns 2. cout << s.compare ("Cat dog C") << Endl; s smaller than "dog", returns-1 cout << s.compare ("dog") << Endl; S is equal to "cat dog Cat" and returns 0 cout << s.compare ("Cat Dog Cat") << Endl; return 0;}
Operation Result:
1
1
2
-1
0
12. Reverse sort string object with reverse
Use the reverse () method to reverse-order the elements (characters) in the interval pointed to by the string object iterator.
#include <iostream> #include <string> #include <algorithm>using namespace std;int main (int argc, char* Argv[]) { string s; s = "123456789"; Reverse (S.begin (), S.end ()); cout << s << endl; return 0;}
Operation Result:
987654321
13. String object as vector element
A string object can be an element of vector vectors, a usage similar to a string array.
#include <iostream> #include <string> #include <algorithm>using namespace std;int main (int argc, char* Argv[]) { vector<string> str; Str.push_back ("Jack"); Str.push_back ("Mike"); Str.push_back ("Tom"); for (int i = 0; i < str.size (); i++) cout << str[i] << Endl; cout << str[0][0] << Endl; cout << str[1][0] << Endl; cout << str[2].size () << Endl; return 0;}
Operation Result:
Jack
Mike
Tom
J
M
3
14. Digital processing of String type
It is often necessary to separate each bit of the number that is read into, and if the method of taking redundancy takes too long, then the data read is treated as a string, which saves time.
#include <iostream> #include <string>using namespace std;int main (int argc, char* argv[]) { string s; s = "1234059"; int sum = 0; for (int i = 0; i < s.length (); i++) { if (s[i] = = ' 0 ') sum + = 0; else if (s[i] = = ' 1 ') sum + = 1; else if (s[i] = = ' 2 ') sum + = 2; else if (s[i] = = ' 3 ') sum + = 3; else if (s[i] = = ' 4 ') sum + = 4; else if (s[i] = = ' 5 ') sum + = 5; else if (s[i] = = ' 6 ') sum + = 6; else if (s[i] = = ' 7 ') sum + = 7; else if (s[i] = = ' 8 ') sum + = 8; else if (s[i] = = ' 9 ') sum + = 9; } cout << sum << endl; return 0;}
Operation Result:
24
15. String objects interoperate with character arrays
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) { string s; Char ss[100]; Enter the string into the character array scanf ("%s", &SS); Character array assignment string Object s = SS; To output a string object with PRINGF, use the C_str () method printf (S.c_str ()). cout << Endl; Output character array printf with printf ("%s", SS); cout << Endl; return 0;}
Operation Result:
abc123
abc123
abc123
16. String object and sscanf function
In the C language, the SSCANF function works well, and it can separate substrings, even numbers, in the way you want them.
#include <iostream> #include <stdio.h> #include <string>using namespace std;int main (int argc, char* Argv[]) { string s1, S2, S3; Char sa[100], sb[100], sc[100]; Separates the string into a number, the delimiter is a space sscanf ("ABC 123 PC", "%s%s%s", SA, SB, SC); S1 = sa; s2 = SB; S3 = SC; cout << s1 << "<< S2 <<" << S3 << Endl; Separates a string into a number, the delimiter is a space //When used with a number, like scanf, it needs to pass the pointer address int A, b, C; SSCANF ("1 2 3", "%d%d%d", &a, &b, &c); cout << a << "<< b <<" "<< c << Endl; When a string is separated into a number, the delimiter is "," and "$" //when used with a number, like scanf, it is to pass the pointer address int x, y, Z; SSCANF ("4,5$6", "%d,%d$%d", &x, &y, &z); cout << x << "<< y <<" "<< z << endl; return 0;}
Operation Result:
ABC 123 PC
1 2 3
4 5 6
17. String objects and values are converted to each other
#include <iostream> #include <sstream> #include <string> #include <stdio.h>using namespace std ;//The second method of converting a value to a string: C + + method string ConvertToString (Double x) { ostringstream o; if (o << x) return o.str (); return "error";} Double convertfromstring (const string &s) { Istringstream I (s); Double X; if (i >> x) return x; return 0.0;} int main (int argc, char* argv[]) { //converts the numeric value to string the first method: C method char b[10]; string A; sprintf (b, "%d", 1975); A = b; cout << a << Endl; The second method of converting numeric values to string: C + + method, string cc = ConvertToString (1976); cout << cc << Endl; The first method of converting a string to a numeric value: the C + + method string dd = "2006"; int p = convertfromstring (dd) + 2; cout << p << Endl; return 0;}
Operation Result:
1975
1976
2008
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer (Fifth edition) learning Note _5_ Standard Template Library string (2)