本程序假设文件中的字符串以空白(空格、tab 和换行)隔开。如果文件内容为:
|
string vvv http string vvv stdcpp.cn stdcpp cn stdcpp.cn string cvs hvp |
则输出为
cn cvs http hvp stdcpp stdcpp.cn string vvv |
// 作者:antigloss at http://stdcpp.cn
// 最后修改时间:07-10-18 17:50
// 功能:去除文件中重复的字符串,并将结果按字符串大小顺序输出
#include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
int main()
{
string line;
set<string> uniq_set;
ifstream infile("pathname");
istream_iterator<string> inbeg(infile), inend;
copy( inbeg, inend, inserter(uniq_set, uniq_set.begin()) );
infile.close();
ofstream outfile("pathname");
copy( uniq_set.begin(), uniq_set.end(), ostream_iterator<string>(outfile, "\n") );
}
本文版权归 蚂蚁的 C/C++ 标准编程 以及 作者 Antigloss 共同所有,转载请注明原作者和出处。谢谢。