向量的题解
2024-06-10 15:28:37
发布于:上海
9阅读
0回复
0点赞
#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
int main(){
string colors[4] = {"red","yellow","blue","green"};
vector<string> colorlist(colors,colors+4);
while (true){
string fun;
cin >> fun;
if (fun=="quit") break;
else if (fun=="添加"){
string item;
cin >> item;
colorlist.push_back(item);
}
else if (fun=="删除"){
string item;
cin >> item;
vector<string>::iterator it=colorlist.begin();
while (it!=colorlist.end()){
if (*it==item) colorlist.erase(it);
else it++;
}
}
else if (fun=="修改"){
string old,item;
cin >> old >> item;
for (int i=0;i<colorlist.size();i++) if (colorlist[i]==old) colorlist[i]=item;
}
else if (fun=="排序"){
string way;
cin >> way;
if (way=="递增") sort(colorlist.begin(),colorlist.end(),greater<string>());
else if (way=="递减") sort(colorlist.begin(),colorlist.end());
}
}
vector<string>::iterator it=colorlist.begin();
while (it!=colorlist.end()){
cout << *it << " ";
it++;
}
cout << endl;
return 0;
}
这里空空如也
有帮助,赞一个