4.分析以下需求,并用代码实现: (1)定义List集合,存入多个字符串 (2)删除集合中元素(只要字符串中包含z或者是m中的任意一个字符就需要删除此整个字符串) (3)然后利用迭代器遍历集合元素并输出
时间: 2023-03-28 16:04:08 浏览: 70
好的,我可以回答这个问题。以下是代码实现:
```
#include <iostream>
#include <string>
#include <list>
using namespace std;
int main() {
list<string> strList;
strList.push_back("hello");
strList.push_back("world");
strList.push_back("zmz");
strList.push_back("zzz");
strList.push_back("mmm");
list<string>::iterator it = strList.begin();
while (it != strList.end()) {
if ((*it).find('z') != string::npos || (*it).find('m') != string::npos) {
it = strList.erase(it);
} else {
cout << *it << endl;
++it;
}
}
return ;
}
```
输出结果为:
```
hello
world
```
这个程序定义了一个List集合,存入了多个字符串。然后使用迭代器遍历集合元素,如果字符串中包含z或者是m中的任意一个字符,就删除整个字符串。最后输出剩余的字符串。