UOJ Logo jingrongze5001的博客

博客

新博客

2022-01-15 16:13:15 By jingrongze5001

string!!! 头文件:#include 1、定义和初始化 string s1;//默认初始化,s1是一个空字符串 string s2 = s1;//s2是s1的副本 string s3 = "hello world!"; string s4(10,'c');//s4的内容是:cccccccccc; 2、string上的一些操作 (1)string的读写 string s1,s2; cin >> s1 >> s2; cout << s1 << s2; //注意:不能用scanf和printf进行输入输出
(2)getline读取一行 string s1,s2; getline(cin, s1); cout << s1; (3)string的empty 和 size的操作 s1.empty():是一个bool值,字符串空就返回1,非空返回0; s1.size():返回字符串的长度,o(1)的复杂度

include

//#include using namespace std; int main() { string s1,s2 = "abc"; cout << s1.empty() << endl; cout << s2.empty() << endl; cout << s2.size() << endl; return 0; } (4)string的比较

 > >= < <= !=【字典序】

(5)两个string的相加 string s1 = "hello",s2 = "abc"; string s3 = s1 + s2; cout << s3; (6)字面值和string的相加 做加法运算时,字面值和字符都会转化成string对象! !!!确保+两侧的运算对象至少有一个是string!!! string s1 = "hello",s2 = "abc"; string s3 = s1 + "," + "world"; cout << s3;

3、处理string对象中的字符 (1)将string 对象当作字符数组来处理

include

//#include using namespace std; int main() { string s1 = "hello world"; for(int i = 0; i < s1.size(); i++) { cout << s1[i] << endl; } return 0; }

评论

暂无评论

发表评论

可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。