tts

string操作

输入输出
string s;
cin >> s;
cout << s << endl;
cin.getline(s, 5); //cin.getline(接收字符串的变量,接收字符个数,结束字符)
getline(cin,s);
查找
int pos = s.find(str);
int pos = s.find(str, index);
if (pos == -1)
if (pos == string::npos)
s.find_first_of(str)
s.find_last_of(str)
int index = s.find(c,index)
插入删除
s.insert(pos, str); //在pos之前的位置
s.erase(pos,n); //从pos开始n个字符
合并
c = a + b;
替换
s = s.replace(s.begin(), s.end(), str);
子串
s = s.substr(pos, n); //从pos开始n个字符
比较
s1 > s2;
s1 != s2;
s1 == s2;
基本操作
s.size(); //大小
s.empty(); //是否空字符串

Vector操作

基本函数
vector<int> s;
s.empty();
s.size();
s.clear();
插入
s.push_back(1);
查找
vector< int >::iterator iter = s.find(s.begin(), s.end(), num);
int pos = s.find(s.begin(), s.end(), num) - s.begin();
排序与反转
sort(s.begin(),s.end());
reverse(s.begin(),s.end());
访问
s[1];
删除
s.erase(s.begin() + pos);
lower_bound与upper_bound
int pos =  lower_bound(s.begin(),s.end(), x) - s.begin(); //第一个大于等于该元素
int pos =  upper_bound(s.begin(),s.end(), x) - s.begin(); //第一个大于该元素
去重
sort(v.begin(), v.end());
//1 2 3 3 4 5 5
vector<int>::iterator pos = unique(v.begin(), v.end());
v.erase(pos, v.end());
posted @ 2025-04-19 00:07  B041016  阅读(18)  评论(0)    收藏  举报