STL容器语法( 二 )


测试:
(8)计算容器中的元素个数(vec.size())
(9)判断容器是否为空(vec.empty()):容器为空返回1;
例题:题目描述
For aarray A={a0,a1,...} of ,aof the:
(x): addx at the end of A
(p):printap
():the lastof A
A is a 0- array and it is empty in thestate.
输入描述 The input is given in the.
Each queryis given by

STL容器语法

文章插图
0 x
or
1 p
or
where the first0, 1 and 2,and.
andwill not be given for an empty array. 输出描述
For each , print ap in a line.
输入
80 10 20 320 41 01 11 2
输出
124
提示
1≤q≤200,000
0≤p< the size of A
?1,000,000,000≤x≤1,000,000,000
AC Code:
#include#includeusing namespace std;#define int long longvector a;signed main(){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);int i,j,k,n,x,y;cin>>n;while(n--){cin>>x;if(x==0){cin>>y;a.push_back(y);}if(x==1){int p;cin>>p;cout<
2. :的创建方式与实用功能
#includeusing namespace std;int main(){//string的创建方式:string str1="str1";cout<> s;transform(s.begin(),s.end(),s.begin(),::toupper);cout << s;//字符串转大写.string s;cin >> s;transform(s.begin(), s.end(), s.begin(), ::tolower);cout << s;//字符串转小写.遍历:string s;cin >> s;for(auto j:s)cout << j;string s;cin >> s;for(auto it=s.begin();it!=s.end();it++)cout << (*it);string s;cin >> s;for(int i=0;i> s;for(int i=0;s[i];i++)cout << s[i];字符串转数字:(自动去除前导零)stoi(s);string str2 = "ILoveyou";cout<
常用的一些基本操作:赋值删改查 3.1 C++赋值方式---通过函数赋值:
#includeusing namespace std;int main(){string s1("IMissyou");string s2;//string s2(s1)s2.assign(s1);cout<
运行结果:
3.2(1)比较
#includeusing namespace std;int main(){string first = "123";string second = "13";//逐位按照ASCII码比较cout << (first > second) << endl;cout << (first != second) << endl;cout << (first < second) << endl;return 0;}
运行结果:
(2)调用比较成员函数 ()
#includeusing namespace std;int main(){string first = "123";string second = "13";cout << first.compare(second) << endl;//firstsecond : 返回一个正数//first=second : 返回一个零//strcmpreturn 0;}
运行结果:
3.3字符串连接操作:
#includeusing namespace std;int main(){string strFirst = "ILoveyou,";string strSecond = "IMissyou";//在c++中用‘+’号连接即可strFirst = strFirst + strSecond;cout << strFirst << endl;//通过函数方式连接 append()strFirst.append(strSecond);cout << strFirst << endl;//截取部分内容strFirst.append(strSecond,1,4);cout << strFirst << endl;//用多个相同的字符连接strFirst.append(4,'0');cout << strFirst << endl;//连接常量字符串strFirst.append("IMissyou");cout << strFirst << endl;return 0;}
运行结果:
(3.4)C++ 的查找 find :从左往右找子串或者字符出现的位置,若没有找到,则返回的是 ::npos (-1) rfind : 从右往左找子串或者字符出现的位置
#includeusing namespace std;int main(){string findStr("ILoveyou,IMissyou");if(findStr.find('o') != string::npos){//字符cout << findStr.find('o') << endl;//子串cout << findStr.find("Love") << endl;}if(findStr.rfind('o')!= string::npos){cout