string类

image.png
The string structure is also a dynamic array that can be used almost like a vector. string类 类型也是一个动态数组,用起来很像 vector ,
经常用的s.substr(pos, length) int pos = s.find('c'),还有+用来拼接两个 string

  1. string a = "hatti";
  2. string b = a+a;
  3. cout << b << "\n"; // hattihatti
  4. b[5] = 'v';
  5. cout << b << "\n"; // hattivatti
  6. string c = b.substr(3,4);
  7. cout << c << "\n"; // tiva
  8. string c = b.substr(3);
  9. cout << c << "\n"; // 这个是什么呢,请实践一下
  10. int pos;
  11. pos = b.find('a');
  12. cout << pos << '\n'; //实践一下
  13. pos = b.find('x');
  14. cout << pos << '\n'; //实践一下
  15. // c_str()
  16. string s;
  17. cin >> s;
  18. char * cstr = new char [s.length()+1];
  19. strcpy(cstr, s.c_str());
  20. printf("%s\n", cstr);
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char s1[110];
  4. string s2;
  5. int main(){
  6. cin >> s2;
  7. strcpy(s1, s2.c_str());
  8. printf("%s\n", s1);
  9. return 0;
  10. }
  1. //Note: 我们需要用到不同的头文件
  2. #include <vector>
  3. #include <set>
  4. #include <queue>
  5. //总之,可以用万能头
  6. //无特殊情况,后面的练习过程中,你可以一直使用万能头了
  7. //前提是,你已经了解了不同头文件的作用。比赛的时候认真看比赛要求是否ban掉万能头
  8. //在一些Unix评测环境,万能头不一定好用,不稳定
  9. #include <bits/stdc++.h>