字符串
一.字符串常用函数
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <string.h> 5 using namespace std; 6 7 int main() 8 { 9 string s(4,'x'); 10 cout << s << endl;//xxxx 11 12 s += "xx"; 13 14 string buffer = "E:\\1.txt"; 15 ifstream infile; 16 infile.open(buffer.c_str());//返回 17 18 //erase 19 string s1 = "This is a test string"; 20 s1.erase(10, 5); 21 cout << s1 << endl;//This is a string 22 23 //Insert 24 string s2 = "test "; 25 s1.insert(10, s2); 26 cout << s1 << endl;//This is a test string 27 28 //replace 29 s1 = "This is a string"; 30 s1.replace(10, 6, s2); 31 cout << s1 << endl;//This is a test 32 33 //swap 34 s1.swap(s2); 35 cout << s1 << endl; // test 36 37 s2[2] = 'a'; s2[3] = 't'; 38 cout << s2 << endl;//That is a test 39 40 //substr 41 s1 = s2.substr(10, 4); 42 cout << s1 << endl;//test 43 44 //find(子串,初始查找位置) 45 cout << s2.find("is", 0) << endl;//5 46 cout << s2.find("at", 0) << endl;//2 47 48 //rfind(子串,初始查找位置)从后往前找 49 cout << s2.rfind("a", 13) << endl;//8 50 cout << s2.rfind("t", 13) << endl;//13 51 52 //find_first_of,find_first_not_of 53 //find_last_of,find_last_not_of 54 s1 = "abcdef"; 55 cout << s1.find_first_of("xeyz") << endl;//4 56 cout << s1.find_first_not_of("xyba") << endl;//2 57 cout << s1.find_last_of("xeyz") << endl;//4 58 cout << s1.find_last_not_of("xyba") << endl;//5 59 60 //string.h strlen, strcmp, strcpy, atoi, strtok, strcat 61 cout << strlen(s1.c_str()) << endl;//6 abcdef 62 63 //c_str()是const char* 64 char s3[500]; 65 strcpy(s3, s1.c_str()); 66 cout << s3 << endl;//abcdef 67 68 char s4[500]; 69 strcpy(s4, s2.c_str()); 70 cout << strcmp(s3, s3) << endl;//0 71 cout << strcmp(s3, s4) << endl;//1 72 73 cout << strcat(s3, s4) << endl;//abcdefThat is a test 74 75 strcpy(s3, "123"); 76 cout << atoi(s3) << endl;//123 77 78 s1 = "123 456,789,你好"; 79 strcpy(s3, s1.c_str()); 80 char *p = strtok(s3, " "); 81 while((p = strtok(NULL, ","))) 82 { 83 cout << p << endl; 84 /* 85 123 86 456 87 789 88 你好 89 */ 90 } 91 return 0; 92 }
二.getline
用来读入一整行到string类型变量中去,若getline没有读入字符,它将返回false,可用于判断是否结束以终止程序。
cin在getline之前使用,getline无法工作,cin的回车getline会读入,需要cin.ignore()
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int main(int argc, char *argv[]) 6 { 7 8 string str; 9 cin >> str1; //Ed wood 10 cout << str1; //Ed, 自动忽略空格和之后内容 11 12 return 0; 13 }
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int main(int argc, char *argv[]) 6 { 7 8 string str; 9 getline(cin, str); //Ed wood 10 cout << str; //Ed wood 11 12 return 0; 13 }
1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 using namespace std; 5 6 int main(int argc, char *argv[]) 7 { 8 9 ifstream infile; 10 ofstream outfile; 11 12 string buffer; 13 14 infile.open("E:\\3.txt"); 15 outfile.open("E:\\2.txt", ios::app); 16 17 while(getline(infile, buffer)) 18 { 19 outfile << buffer << endl; 20 /* 21 Num = 1 22 Num = 2 23 Num = 3 24 Num = 4 25 Num = 5 26 1 27 2 28 3 29 4 30 5 31 32 */ 33 } 34 35 infile.close(); 36 outfile.close(); 37 38 return 0; 39 }
三.字符串++
1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 using namespace std; 5 6 int main(int argc, char *argv[]) 7 { 8 string s1 = "1", s2 = "2"; 9 char s3[] = "3";//char[2] 10 11 //左边必须string,+=同上 12 s1 = s3;//ok 13 s1 = s3;//ok 14 s1 = 'l';//ok 15 s3 = s1;//error 16 17 string s4 = ""; 18 19 s1 = s2 + s3;//ok 20 s1 = s2 + s4;//ok 21 s1 = 'w' + s3;//ok 22 s1 = "123" + "456";//error 23 24 return 0; 25 }
四.补充
1 #include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <string.h> 5 using namespace std; 6 7 int main() 8 { 9 //assign 10 string s1 = "cat"; 11 string s2; 12 s2.assign(s1); 13 cout << s2 << endl;//cat 14 s2.assign(s1, 0, 2); 15 cout << s2 << endl;//ca 16 17 //append 18 s1.append(s2); 19 cout << s1 << endl;//catca 20 s2.append(s1, 3, s1.size());//3至结束 21 cout << s2 << endl;//caca 22 23 //length,size 24 cout << s1.length() << endl;//5 25 cout << s1.size() << endl;//5 26 27 //strstr 28 //cout << strstr(s1.c_str(), s2.c_str()) << endl;//返回NULL 29 s1.append(s2); 30 cout << strstr(s1.c_str(), s2.c_str()) << endl;//返回s2在s1中首次出现的地址,cacaca 31 32 //at 33 string s3("hello"); 34 cout << s3[0] << " " << s3.at(0) << endl;//h h 35 //cout << s3.at(7) << endl;//at会做范围检查,超出范围会抛出outofrange异常,下标运算符不会 36 37 38 return 0; 39 }