#include <iostream>
#include <list>
#include <set>
#include <algorithm>
using namespace std;
template <typename T>
struct Anyset : public set<T> {
Anyset(){}
Anyset(const list<T>& l){list<T>::iterator i;
for (i=l.begin();i!=l.end();++i) insert(*i);}
Anyset Union(const Anyset& a)const{list<T> r;
set_union(begin(),end(),a.begin(),a.end(),
back_inserter(r)); return r;}
Anyset Intersect(const Anyset& a)const{list<T> r;
set_intersection(begin(),end(),a.begin(),a.end(),
back_inserter(r)); return r;}
Anyset SymDiff(const Anyset& a)const{list<T> r;
set_symmetric_difference(begin(),end(),a.begin(),a.end(),
back_inserter(r)); return r;}
};
template <typename T>
ostream& operator<<(ostream& o,const Anyset<T>& a)
{
Anyset<T>::iterator i;
for (i=a.begin();i!=a.end();++i) o << *i << " ";
return o;
}
struct Node {
int id;
Node(int i = 0) : id(i) {}
operator int()const{return id;}
};
typedef Anyset<int> Intset;
typedef Anyset<Node> Nodeset;
int main()
{
Intset i,j;
i.insert(7); i.insert(2); i.insert(3); i.insert(5);
j.insert(2); j.insert(3); j.insert(1);
cout << "Union " << i.Union(j) << endl;
cout << "Intersect " << i.Intersect(j) << endl;
cout << "SymDiff " << i.SymDiff(j) << endl;
Nodeset k,l;
k.insert(7); k.insert(2); k.insert(3); k.insert(5);
l.insert(2); l.insert(3); l.insert(1);
cout << "Union " << k.Union(l) << endl;
cout << "Intersect " << k.Intersect(l) << endl;
cout << "SymDiff " << k.SymDiff(l) << endl;
}
|
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <strstream>
using namespace std;
typedef map<string,vector<int> > MType;
MType MakeMap(istream& i,vector<string>& v)
{
MType r;
string s,t;
int l = 0;
while (getline(i,s)) {
++l;
istrstream is(s.c_str());
while (is >> t)
if (find(v.begin(),v.end(),t) != v.end())
r[t].push_back(l);
}
return r;
}
int main()
{
vector<string> v;
v.push_back("const");
v.push_back("cout");
v.push_back("double");
v.push_back("vector");
MType m = MakeMap(cin,v);
MType::iterator i;
for (i=m.begin();i!=m.end();++i) if (i->second.size() >= 10) {
vector<int>::iterator j;
cout << i->first;
for (j=i->second.begin();j!=i->second.end();++j)
cout << " " << *j;
cout << endl;
}
}
|