编写程序处理一个 string 类型的 list 容器。在该容器9.27:中寻找一个特殊值,如果找到,则将它删除掉。用 deque容器重写上述程序。
//list容器
#include<iostream>
#include<list>
using namespace std;
int main(void)
{
?? ?string str[] = { "hello","world","!" }, str1;
?? ?std::list<string> strs;
?? ?for (size_t i = 0; i != 3; i++)
?? ??? ?strs.push_back(str[i]);
?? ?cout << "請輸入需要刪除的值" << endl<< "hello, world, !"<<endl;
?? ?cin >> str1;
?? ?for (std::list<string>::iterator it = strs.begin(); it != strs.end();)
?? ?{
?? ??? ?if (str1 == *it)
?? ??? ??? ?strs.erase(it++);
?? ??? ?else
?? ??? ??? ?it++;
?? ?}
?? ?cout << "剩下的值" << endl;
?? ?for (std::list<string>::iterator it = strs.begin(); it != strs.end(); it++)
?? ??? ?cout << *it;
?? ?return 0;
}
//deque容器
#include<deque>
#include<iostream>
using namespace std;
int main(void)
{
?? ?string str[] = { "hello","world","!" }, str1;
?? ?std::deque<string> strs;
?? ?for (size_t i = 0; i != 3; i++)
?? ??? ?strs.push_back(str[i]);
?? ?cout << "請輸入需要刪除的值" << endl << "hello, world, !" << endl;
?? ?cin >> str1;
?? ?for (std::deque<string>::iterator it = strs.begin(); it != strs.end();?? ?it++)
?? ?{
?? ??? ?if (str1 == *it)
?? ??? ?{
?? ??? ??? ?it=strs.erase(it);//返回下個it坐標即it++賦值給it;
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ??? ?
?? ?}
?? ?cout << "剩下的值" << endl;
?? ?for (std::deque<string>::iterator it = strs.begin(); it != strs.end(); it++)
?? ??? ?cout << *it;
?? ?return 0;
}
總結(jié)
以上是生活随笔為你收集整理的编写程序处理一个 string 类型的 list 容器。在该容器9.27:中寻找一个特殊值,如果找到,则将它删除掉。用 deque容器重写上述程序。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果8p与xr选哪个
- 下一篇: 编写程序计算 sentence 中有多少