第三题把时间主要花费在set的STL和怎么比较struct了怎么搞的上面了……忽然觉得有些不值得
不得不说做公开课作业有一个好处就是很锻炼英文阅读啊,嘤嘤经常晕晕的不知道你想让我做什么泥煤的TvT
Problem 3: Cartesian Products
这里set的STL的用法参考了几处,回头再总结一下,其实压根没用到什么比较实质的东西
比较函数参考来自于 http://www.cnblogs.com/lengbingshy/p/3491192.html
#include <iostream>
#include <string>
#include <set>
using namespace std;
struct pairT{
string first, second;
bool operator <(pairT other) const{
if(first == other.first)return second > other.second;
return first > other.first;
}//不是很理解这里的原理,还有待研究T口T,今天在这里死了一遍又一遍,果然自作聪明是不对的。
};
set<pairT> CartesianProduct(set<string> one, set<string> two){
set<string> :: iterator iOne;
set<string> :: iterator iTwo;
set<pairT> cartesian;
int i = 0;
for(iOne = one.begin(); iOne != one.end(); iOne++){
for(iTwo = two.begin(); iTwo != two.end(); ++iTwo){
i ++;
pairT a;
a.first = *iOne;
a.second = *iTwo;
cartesian.insert(a);
}
}
return cartesian;
}
int main(){
set<string> one;
set<string> two;
//下面这是给one和two增加内容
for(int i = 0; i < 3; i++){
char A = 'A' + i;
char a = 'A' + i;
string B = "";
string b = "";
B += A;
b += a;
one.insert(B);
two.insert(b);
}
set<pairT> cartesian = CartesianProduct(one, two);
cout << "一共有"<< cartesian.size() << "对"<< endl;
set<pairT> :: iterator itPair;
int time = 0;
for(itPair = cartesian.begin(); itPair != cartesian.end(); ++ itPair){
pairT p = *itPair ;
time ++;
cout << "cartesian中第" << time << "组" << p.first << " and " << p.second << endl;
}
return 0;
}