比较两组字符串区别
两组字符串,1万组
如何快速比较这两组的区别。
并且把区别全部打印出来?
例如:
A组:aaa bbb ddd eee ....
B组:aaa ccc ddd fff ....
A组和B组都有的:aaa ddd ...
A组有B组没有的: bbb eee ...
B组有A组没有的:ccc fff ...
[最优解释]
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
vector<string> v1, v2;
vector<string> vone, vtwo, vall;
vector<string>::const_iterator it;
v1.push_back(string("aaa"));
v1.push_back(string("bbb"));
v1.push_back(string("ddd"));
v1.push_back(string("eee"));
v2.push_back(string("aaa"));
v2.push_back(string("ccc"));
v2.push_back(string("ddd"));
v2.push_back(string("fff"));
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),
back_inserter(vall));
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),
back_inserter(vone));
set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(),
back_inserter(vtwo));
cout << "A组和B组都有的: ";
for (it = vall.begin(); it != vall.end(); ++it)
cout << *it << ' ';
cout << endl;
cout << "A组有B组没有的: ";
for (it = vone.begin(); it != vone.end(); ++it)
cout << *it << ' ';
cout << endl;
cout << "B组有A组没有的: ";
for (it = vtwo.begin(); it != vtwo.end(); ++it)
cout << *it << ' ';
cout << endl;
return 0;
}
std::vector<std::string> A = {"aaa", "bbb", "ccc"};
std::vector<std::string> B = {"bbb", "ccc", "ddd"};
//std::sort A和B
//A组和B组都有的
std::vector<std::string> AB_intersect;
set_symmetric_difference(A, B, AB_intersect);
//A组有B组没有的
std::vector<std::string> AB_differ;
set_difference(A, B, AB_differ); //自己简化一下
//B组有A组没有的
std::vector<std::string> BA_differ;
set_difference(B, A, BA_differ); //同上
std::vector<std::string> AB_intersect;
set_intersection(A, B, AB_intersect);
cout << *it << ' ';
cout << endl;
for (it = vone.begin(); it != vone.end(); ++it)
cout << *it << ' ';
cout << endl;
for (it = vtwo.begin(); it != vtwo.end(); ++it)
cout << *it << ' ';
cout << endl;
return 0;
}
[/code]
[其他解释]
来晚了,只能顶11楼。
[其他解释]
鉴于很多人说C比C++快,楼主不妨试试下面这个:
//输出PROG中有但LIST中没有的文本行,即集合PROG-LIST
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#define MAXCHARS 512
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
char PROG[256]="PROG";//程序Program需要的文件列表
char LIST[256]="LIST";//dir /b /s生成的实际文件列表List
FILE *fp,*fl;
int i,c,n,L,hh;
int ignore_case=0;
char ln[MAXCHARS];
int icompare(const void *arg1,const void *arg2) {
return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
if (argc>1) strcpy(PROG,argv[1]);//命令行参数1覆盖PROG
if (argc>2) strcpy(LIST,argv[2]);//命令行参数2覆盖LIST
if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
if ((fl=fopen(LIST,"rt"))==NULL) {
fprintf(stderr,"Can not open %s\n",LIST);
fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
return 1;
}
if ((fp=fopen(PROG,"rt"))==NULL) {
fclose(fl);
fprintf(stderr,"Can not open %s\n",PROG);
fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
return 2;
}
buf=(char *)malloc(MAXLINES*MAXCHARS);
if (NULL==buf) {
fclose(fl);
fclose(fp);
fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
return 4;
}
n=0;
hh=0;
i=0;
while (1) {
if (fgets(ln,MAXCHARS,fl)==NULL) break;//
hh++;
L=strlen(ln)-1;
if ('\n'!=ln[L]) {//超长行忽略后面内容
fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",LIST,hh,MAXCHARS);
while (1) {
c=fgetc(fl);
if ('\n'==c