#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s; //s是string类的对象
cout << s.length() << endl; // 0
s = "123456";
cout << s.length() << endl; // 6
char str[] = "hello";
s = str;
cout << s << endl; // hello
s += ' ';
cout << s << endl; // hello
s += "world"; // hello world
cout << s << endl;
s.append(", hello");
s.append(" computer!");
cout << s << endl; // hello world, hello computer!
s = "123456";
string::iterator it;
it = s.begin();
cout << *it << endl; // 1
cout << *(s.end() - 1) << endl; // 6
s.insert(it + 1, 'C');
cout << s << endl; //1C23456
s.erase(it + 1, it + 2); //注意:去掉的是[a, b)
cout << s << endl; //123456
cout << s[0] << endl; // 1
cout << s.empty() << endl; // 0
s.replace(3, 2, "bc");
cout << s << endl; // 123bc6
s.replace(3, 2, "45");
cout << s << endl; // 123456
s = "abcdef";
cout << s.find('a') << endl; // 0
cout << s.find("cd") << endl; // 2
cout << s.find("acm") << endl; // 一个很大的数
reverse(s.begin(), s.end()); //需要#include<algorithm>
cout << s << endl; //fedcba
return 0;
}
类string入门
最新推荐文章于 2024-05-15 10:00:00 发布