C++ Primer(第5版) 练习 10.9
练习 10.9 实现你自己的elimDups。测试你的程序,分别在读取输入后、调用unique后以及调用erase后打印vector的内容。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
/*************************************************************************> File Name: ex10.9.cpp> Author: > Mail: > Created Time: Fri 01 Mar 2024 08:25:02 AM CST************************************************************************/#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;void elimDups(vector<string> &words){sort(words.begin(), words.end());auto end = unique(words.begin(), words.end());words.erase(end, words.end());
}int main(){vector<string> str;string temp;cout<<"Enter strings: ";while(cin>>temp){str.push_back(temp);if(cin.get() == '\n'){break;}}cout<<"Original strings: ";for(const auto s : str){cout<<s<<" ";}cout<<endl;elimDups(str);cout<<"New strings: ";for(const auto s : str){cout<<s<<" ";}cout<<endl;return 0;
}