posts - 137,comments - 0,views - 40818

Mat和Pat希望邀请他们的朋友来参加派对。他们要编写一个程序完成下面的任务。

让Mat输入他朋友的姓名列表。姓名存储在一个容器中,然后按排列后的顺序显示出来。
让Pat输入她朋友的姓名列表。姓名存储在另一个容器中,然后按排列后的顺序显示出来。
创建第三个容器,将两个列表合并,删除重复的部分,并显示这个容器的内容。
main.cpp:

复制代码
#include <iostream>
using namespace std;
void input(vector<string>& ret) {
  string str;
  cout << "please input your friends:(按'q'结束)";
  while (cin >> str && str != "q") {
    ret.push_back(str);
  }
  sort(ret.begin(), ret.end());
  for (auto a : ret) {
    cout << a << " ";
  }
}
void imergeVector(vector<string>& ret1, vector<string>& ret2) {
  vector<string> ret3;
  ret3.reserve(ret1.size() + ret2.size());//预留足够的空间
  ret3.insert(ret3.end(), ret1.begin(), ret1.end());
  ret3.insert(ret3.end(), ret2.begin(), ret2.end());

  sort(ret3.begin(), ret3.end());//在使用unique前要先排序
  auto aut = unique(ret3.begin(), ret3.end());//去掉重复值
  for (auto it = ret3.begin(); it != aut; ++it) {
    cout << *it << " ";
  }
  cout << endl;
}
int main(void) {
  vector<string> pat, mat;
  cout << "【pat】:" << endl;
  input(pat);
  cout << "\n-----------------\n";
  cout << "【mat】:" << endl;
  input(mat);
  cout << "\n-----------------\n";
  imergeVector(pat, mat);
  return 0;
}
复制代码

posted on   wshidaboss  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示