以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  [C++] 去除文件中重复的字符串  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=76431)


--  作者:xwmlz123
--  发布时间:8/19/2009 4:04:00 PM

--  [C++] 去除文件中重复的字符串
[C++] 去除文件中重复的字符串

2009-03-23 09:27:46  来源:  作者:  查看次数:
本程序假设文件中的字符串以空白(空格、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") );
}

公司地址:成都市高新区高棚大道3号东方希望大厦B座218-222 | 咨询电话:028-85176515 办公电话:028-85172396 028-85194325 | 800免费电话:800-886-3788(仅限电信) 网站: www.re-er.com.cn | E-mail:hello@re-er.com.cn


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
31.250ms