基本语法
1.string之间的比较大小
原则:找第一个不同的比较得结果,对大小写敏感,A<a大写变小写'A'+32='a' #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int main(){ vector<string> words={"a","Aee", "b", "app", "appl", "ap", "apply", "apple"}; sort(words.begin(),words.end()); for(auto a:words) cout<<a<<" "; cout<<char('A'+32); return 0; } Aee a ap app appl apple apply b a
2.string常用函数
1.insert()插入
#include <iostream> #include <string> using namespace std; int main(){ string str = "meihao"; string sstr = str.insert(0,2,'a'); cout<<sstr<<endl; //aameihao string str = "meihao"; string sstr = str.insert(0,"hello~"); cout<<sstr<<endl; //hello~meihao string str = "meihao"; string sstr = str.insert(1,"hello~",3); cout<<sstr<<endl; //mheleihao string str1 = "meihao"; string str2 = "hello~"; string sstr = str2.insert(6,str1,3,3); cout<<sstr<<endl; //hello~hao return 0; }
2.str.erase()删除
erase() 函数可以删除 string 中的一个子字符串。它的一种原型为: string& erase (size_t pos = 0, size_t len = npos); pos 表示要删除的子字符串的起始下标,len 表示要删除子字符串的长度。如果不指明 len 的话, 那么直接删除从 pos 到字符串结束处的所有字符(此时 len = str.length - pos)。 #include <iostream> #include <string> using namespace std; int main(){ string s1, s2, s3; s1 = s2 = s3 = "1234567890"; s2.erase(5); 从下标5开始把以后的都删除 s3.erase(5, 3); 从下标5开始删除3个 cout<< s1 <<endl; cout<< s2 <<endl; cout<< s3 <<endl; return 0; } 运行结果: 1234567890 12345 1234590
3.str.substr()提取
substr() 函数用于从 string 字符串中提取子字符串,它的原型为: string substr (size_t pos = 0, size_t len = npos) const; pos 为要提取的子字符串的起始下标,len 为要提取的子字符串的长度。 #include <iostream> #include <string> using namespace std; int main(){ string s1 = "first second third"; string s2; s2 = s1.substr(6, 6); 从下标6开始提取6个字符 cout<< s1 <<endl; cout<< s2 <<endl; return 0; } 运行结果: first second third second
4.1查找str.find()
第一个参数为待查找的子字符串,它可以是 string 字符串,也可以是C风格的字符串。 第二个参数为开始查找的位置(下标);如果不指明,则从第0个字符开始查找。 最终返回的是子字符串第一次出现在字符串中的起始下标 #include <iostream> #include <string> using namespace std; int main(){ string s1 = "first second third"; string s2 = "second"; int index = s1.find(s2,5); 从下标5开始向右查找 if(index < s1.length()) cout<<"Found at index : "<< index <<endl; else cout<<"Not found"<<endl; return 0; } 输出是6
4.2查找str.find()
rfind() 函数则最多查找到第二个参数处,如果到了第二个参数所指定的下标还没有找到子字符串 则返回一个无穷大值4294967295。 最终返回的是子字符串第一次出现在字符串中的起始下标 #include <iostream> #include <string> using namespace std; int main(){ string s1 = "first second third"; string s2 = "second"; int index = s1.rfind(s2,6); 从左开始向右查找到下标为6结束而且查找的是整个S2 if(index < s1.length()) s2 = "s0cond"则查找不到 cout<<"Found at index : "<< index <<endl; else cout<<"Not found"<<endl; return 0; } 输出是6
4.3查找find_first_of() 函数
find_first_of() 函数用于查找子字符串和字符串共同具有的字符在字符串中首次出现的位置。 #include <iostream> #include <string> using namespace std; int main(){ string s1 = "first second second third"; string s2 = "asecond"; int index = s1.find_first_of(s2); if(index < s1.length()) cout<<"Found at index : "<< index <<endl; else cout<<"Not found"<<endl; return 0; } 运行结果: Found at index : 3 本例中 s1 和 s2 共同具有的字符是 ’s’,该字符在 s1 中首次出现的下标是3,故查找结果返回3。
3.char str[]数组
1.char数组赋值
#include <iostream> #include <string> #include <cstring> using namespace std; int main(){ // 方式一:strcpystrcpy(str,"asdf") // char str[10]; //str[10]="sdsd"; //报错不符合语法 strcpy(str,"asdf"); // 先定义数组指明长度再使用strcpy函数复制sizeof(str)=10 strlen(str)=4 // 方式二:定义的时候直接用字符串赋值,可以不指明长度 // char str1[10] = "asdf"; // 定义的时候直接用字符串赋值sizeof(str1)=10 strlen(str1)=4 // 方式三:对数组中字符逐个赋值,可以不指明长度 // char str2[] = {'a','s','d','f'}; // 对数组中字符逐个赋值 sizeof(str2)=4 strlen(str2)不指定长度结果不定 char str3[] = "asdf"; // “”表示尾部加上'\0' sizeof(str3)=5 strlen(str3)=4 char str4[50] = {'a','s','d','f'}; //对数组中字符逐个赋值 sizeof(str4)=10 strlen(str4)=4结果确定 cout<<sizeof(str)<<sizeof(str1)<< " "<<sizeof(str2)<< " "<< sizeof(str3)<< " "<< sizeof(str4) <<endl; cout<<strlen(str)<<strlen(str1)<< " "<<strlen(str2)<< " "<< strlen(str3)<< " "<< strlen(str4) <<endl; cout<<str2; //输出不一定是asdf不确定 cout<<str4; //输出一直都是asdf return 0; }
4.cin.getline与getline
#include <iostream> //cin.getline(char_nums,10) 用于char数组 #include <string> //getline(cin,str1) 用于string类 using namespace std; int main() { // char char_nums[100]; // cin.getline(char_nums,10); // cout<<char_nums<<endl; string str1; getline(cin,str1); cout<<str1<<endl; }
2022/1/25
于 2022-01-25 21:52:03 首次发布