string
获取一整行
#include<bits/stdc++.h>
using namespace std;
stack<int> t;
int main(){
int n;
cin >> n;
string s;
getchar();
getline(cin,s);
cout << n << endl;
cout << s << endl;
return 0;
}
查找
#include<bits/stdc++.h>
using namespace std;
stack<int> t;
int main(){
string s;
s = "hello world"; //增删改查
//查找find
int k = s.find("lk");
if(k!=-1){
cout << "YES\n";
}else{
cout << "NO\n";
}
if(s.find("llo")!=string::npos){//string::npos : s.npos
cout << "YES\n";
}else{
cout << "NO\n";
}
s = "hello world";
if(s.find("llo",5)!=string::npos){//从下标5开始找
cout << "YES\n";
}else{
cout << "NO\n";
}
return 0;
}
删除
#include<bits/stdc++.h>
using namespace std;
stack<int> t;
int main(){
string s;
s = "hello world"; //增删改查
//删除
s.erase(2,3);
cout << s << endl;
return 0;
}
更换
#include<bits/stdc++.h>
using namespace std;
stack<int> t;
int main(){
string s;
s = "hello world"; //增删改查
//修改,更替
s.replace(1,4,"i");
cout << s;
return 0;
}
插入
#include<bits/stdc++.h>
using namespace std;
stack<int> t;
int main(){
string s;
s = "hello world"; //增删改查
//插入insert
s.insert(3,"abc");//在下标3的前面插入“abc”
cout << s;
return 0;
}
截取
#include<bits/stdc++.h>
using namespace std;
stack<int> t;
int main(){
string s;
s = "hello world"; //增删改查
//截取
string ss = s.substr(2,6) ;
cout << s << endl;
cout << ss << endl;
return 0;
}