Character array
Character arrays in C + + can be used to represent a string, using char str[].
(1) The size of the array and the length of the string.
The size of the array must be greater than the length of the string, because the system automatically complements a ' + ' as the end flag of the string. Of course, for uninitialized also fill '.
#include <iostream> #include <string>using namespace Std;int main () {char str[11] = "I am happy"; The system will automatically fill in the ' \ s ' null character as the end flag, and no initialization is also '//char ' str[10] = "I am happy"; Error system automatically complements ' \ s ' At this time the character array length is not enough//char str[13] = "I am happy"; The following does not initialize the "happy\0\0\0if" for I am (str[10] = = ' + ') {cout << "Hello world!!" << Endl;} Cin >> str; input/output cout << str << endl;return 0;}
(2) Getline ()
The Getline function can read the text or line of the input stream, including the previously entered space, only until the carriage return line ends
#include <fstream> #include <iostream> #include <string>using namespace Std;int main () { ifstream In ("E:\\algorithmzack\\teststring\\input.txt"); if (!in) { cerr << "Some errors happened"; return-1; } String str; while (Getline (in, str))/// getline reads files by line from file Input.txt //while (Getline (CIN, str)) // read by row from input stream do not include line breaks { cout << str << endl; } return 0;}
(3) Comparison, connection, assignment, actual length with function strcmp, strcat, Strcpy,strlen
See blog:http://see.xidian.edu.cn/cpp/biancheng/view/158.html
Strings string
(1) String can be seen as a class library that needs to have include header file # include <string>.
Operations include: Connection (+=,append) Assignment (=, assign) comparison (>=,compare) lookup (Find)
Replace (replace) Delete (erase) Insert (insert) string (substring) swap (swap)
Attribute (length Sizec_str) forward and backward iterator (Interatorreverse_iterator)
The advantage of using Append,assign,compare is that the argument can be a character array
See blog:http://www.cnblogs.com/xfreedom/archive/2011/05/16/2048037.html for details
(2) A simple example
Reads a string from TXT into a space and de-sorts the output
#include <iostream> #include <fstream> #include <string> #include <vector> #include < Algorithm> #include <iterator>using namespace Std;int main () {Ifstream in ("e:\\algorithmzack\\teststring\\ Name.txt "); Fstreamstring str; Vector<string> VEC; Vectorwhile (Getline (in, str))//String{while (Str.find_first_of (")! =-1) {Vec.push_back (str.substr (0, Str.find_f Irst_of ("))); str = STR.SUBSTR (str.find_first_of (") +1);} Vec.push_back (str);} Sort (Vec.begin (), Vec.end ()); algorithm/* Note that the repeating element is removed but the VEC size has not changed, after which two spaces have been added. For int, however, the repeating element is placed behind */vector<string>::iterator it = unique (Vec.begin (), Vec.end ()); Algorithm returns the last element after the redo (Vec.begin (), it, ostream_iterator<string> (cout, "\ n")); Iterator ostream_iterator<string> iteratorcout << endl;copy (Vec.begin (), Vec.end (), ostream_iterator& Lt;string> (cout, "\ n"));/*for (Vector<string>::iterator iter = Vec.begin (); ITER! = Vec.end (); iter++) {cout << *iter << "";} cout << Endl;*/return 0;}
Note: This unique removes the repeating element from the vector<string>, but its size does not change, and the repeating element is replaced with a space. However, for the vector<int>, the repeating element is placed behind.
(3) Copy function
The copy function is contained in the header file #include<iterator> header file. There are three main uses that are commonly used
Copy (Iteratorinput it1, Iteratorinput it2, ITERATORONPUTIT3)//algorithm
1: Assigning values to vector<t> with arrays
2: Use CIN to assign values to vector<t>
3: Output of the vector<t>
Copy function prototype explanation see blog:http://blog.csdn.net/jerryjbiao/article/details/7376088
#include <iostream> #include <vector> #include <iterator> #include <algorithm> #include < String>using namespace Std;int main () {vector<int> vec;cout << "Hello world!!" << endl;//int a[] = {3, 2,1,1,2,3};//copy (A, a+6, Back_inserter (VEC));//vec.resize (6);//copy (A, a+6, Vec.begin ()); Copy (Istream_iterator <int> (CIN), istream_iterator<int> (), Back_inserter (VEC)), copy (Vec.begin (), Vec.end (), Ostream_iterator <int> (cout, "")); cout << Endl;sort (Vec.begin (), Vec.end ()); Vector<int>::iterator it = unique ( Vec.begin (), Vec.end ()), copy (Vec.begin (), it, ostream_iterator<int> (cout, "")); Output return 0;}
(4) A simple example
Filters all non-English characters at the beginning and end of a line. This is just to illustrate the difference between the Find function find_first_of function. Find is the position of the string s in the current string from the point of POS, and find_first_of is the position of the first n characters in the current string from the point of Pos.
#include <string> #include <iostream>using namespace Std;int main () {string strinfo = "//*----Hello world! ...----"; string strset =" ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ "; int first = Strinfo.find_first_of ( Strset), if (first = = String::npos) {cout << "not find any characters" << endl;return-1;} int last = strinfo.find_last_of (Strset), if (last = = String::npos) //string::npos =-1{cout << ' not ' find any Chara Cters "<< endl;return-1;} cout << strinfo.substr (First, Last-first + 1) << endl;string str = "Hello world!!!"; String str2 = "Hlo";//Note Find and find_first_of () differ greatly from int j = Str.find (str2); Finds the position of the string s in the current string starting from the pos int i = str.find_first_of (str2); Finds the position of the character in the first n-character array of s in the current string starting from Pos. cout << i << endl;return 0;}
See other blog:1:http://www.cplusplus.com/reference/string/string/:
2:http://blog.csdn.net/yangtrees/article/details/7577263
3:http://www.cnblogs.com/uniqueliu/archive/2011/07/28/2119328.html
4:http://blog.csdn.net/jerryjbiao/article/details/7376088
5:http://www.cnblogs.com/zkliuym/articles/909245.html
Character arrays and string strings in C + +