【C++ Primer Plus习题】16.8
国中之林 2024-10-07 10:35:01 阅读 80
大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
main.cpp
<code>#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
set<string> Mat_set, Pat_set, Guest_set;
cout << "Enter Mat's friends(q to quit): ";
string name;
while (getline(cin,name)&&name!="q")
{
Mat_set.insert(name);
cout << "Enter Mat's friends(q to quit): ";
}
cout << "\nMat's friends are: \n";
for (auto pd = Mat_set.begin(); pd != Mat_set.end(); pd++)
{
cout << *pd << " ";
}
cout << "\nEnter Pat's friends(q to quit): ";
while (getline(cin, name) && name != "q")
{
Pat_set.insert(name);
cout << "Enter Pat's friends(q to quit): ";
}
cout << "\nPat's friends are: \n";
for (auto pd = Pat_set.begin(); pd != Pat_set.end(); pd++)
{
cout << *pd << " ";
}
Guest_set.insert(Mat_set.begin(), Mat_set.end());
Guest_set.insert(Pat_set.begin(), Pat_set.end());
cout << "\n All friends are: \n";
for (auto pd = Guest_set.begin(); pd != Guest_set.end(); pd++)
{
cout << *pd << " ";
}
return 0;
}
运行结果:
考查点:
set关联容器无重复迭代器
2024年9月18日21:40:43
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。