// TimerTest.cpp : Using STL functions
//并集,交集,差集,根据bound获取特定子集
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int N = 6;
string s1[N] = {"hell","cc","acc","bb","dd","kk"};
string s2[N] = {"ohell","ucc","yacc","nbb","mdd","ckk"};
set<string> A(s1,s1+N);
set<string> B(s2,s2+N);
ostream_iterator<string,char> out(cout," ");
cout<<"Set A:";
copy(A.begin(),A.end(),out);
cout<<endl;
cout<<"Set B:";
copy(B.begin(),B.end(),out);
cout<<endl;
cout<<"Union of A and B:";
set_union(A.begin(),A.end(),B.begin(),B.end(),out);
cout<<endl;
cout<<"Intersection of A and B:";
set_intersection(A.begin(),A.end(),B.begin(),B.end(),out);
cout<<endl;
cout<<"Difference of A and B:";
set_difference(A.begin(),A.end(),B.begin(),B.end(),out);
cout<<endl;
set<string> C;
cout<<"Set C:\n";
set_union(A.begin(),A.end(),B.begin(),B.end(),insert_iterator<set<string>>(C,C.begin()));
copy(C.begin(),C.end(),out);
string s3("grungy");
C.insert(s3);
cout<<"Set C after insert s3:\n";
copy(C.begin(),C.end(),out);
cout<<endl;
cout<<"Show a range:\n";
copy(C.lower_bound("bb"),C.upper_bound("acc"),out);
cout<<endl;
system("pause");
}
// TimerTest.cpp : Using STL functions
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
typedef int KeyType;
typedef pair<const KeyType,string> Pair;
typedef multimap<const KeyType,string> MapCode;
/**
程序入口
简单 multimap 应用。
*/
int _tmain(int argc, _TCHAR* argv[])
{
MapCode codes;
codes.insert(Pair(415,"San Francison"));
codes.insert(Pair(510,"kingdom_0"));
codes.insert(Pair(521,"lynn"));
codes.insert(Pair(521,"forever love."));
codes.insert(Pair(415,"hehe."));
cout<<"Number of cities with the area code 415:"
<<codes.count(415)<<endl;
cout<<"Numbver of cities with the area code 510:"
<<codes.count(510)<<endl;
cout<<"Number of cities with the area code 521:"
<<codes.count(521)<<endl;
cout<<"Area code City \n";
MapCode::iterator it;
for(it = codes.begin(); it != codes.end(); it++)
{
cout<<"Key:"<<it->first<<"Value:"<<it->second<<endl;
}
pair<MapCode::iterator,MapCode::iterator> range=codes.equal_range(521);
cout<<"Cities with area code 521:\n";
for(it = range.first; it != range.second; it++)
cout<<(*it).second<<endl;
system("pause");
}
// TimerTest.cpp : Using STL functions
// 函数符
#include "stdafx.h"
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
template<class T> //functor class defines operator()
class TooBig
{
private:
T cutoff;
public:
TooBig(const T &t):cutoff(t){}
bool operator()(const T &v){return v>cutoff;}
};
/**
程序入口
*/
int _tmain(int argc, _TCHAR* argv[])
{
TooBig<int> f100(100);//limit = 100
list<int> yedayada;
list<int> etcetera;
int vals[10] = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};
yedayada.insert(yedayada.begin(),vals,vals+10);
etcetera.insert(etcetera.begin(),vals,vals+10);
ostream_iterator<int,char> out(cout," ");
cout<<"Original lists:\n";
copy(yedayada.begin(),yedayada.end(),out);
cout<<endl;
copy(etcetera.begin(),etcetera.end(),out);
cout<<endl;
yedayada.remove_if(f100);
//cout<<"Remove "
copy(yedayada.begin(),yedayada.end(),out);
cout<<endl;
etcetera.remove_if(TooBig<int>(200));
copy(etcetera.begin(),etcetera.end(),out);
cout<<endl;
//transform:数据通过处理,将集合放到一个新容器中
transform(yedayada.begin(), yedayada.end(), out, sqrt);
system("pause");
}
// TimerTest.cpp : Using STL functions
// transform demo
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
/**
程序入口
*/
void Show(double);
const int LIM = 5;
int _tmain(int argc, _TCHAR* argv[])
{
double arr1[LIM] = {36, 39, 42, 45, 48};
double arr2[LIM] = {25, 27, 29, 31, 33};
vector<double> gr8(arr1,arr1+LIM);
vector<double> m8(arr2,arr2+LIM);
cout.setf(ios_base::fixed);
cout.precision(1);
cout<<"gr8: \t";
for_each(gr8.begin(),gr8.end(),Show);
cout<<endl;
cout<<"m8: \t";
for_each(m8.begin(),m8.end(),Show);
cout<<endl;
vector<double> sum(LIM);
transform(gr8.begin(),gr8.end(),m8.begin(),sum.begin(),plus<double>());
cout<<"sum: \t";
for_each(sum.begin(),sum.end(),Show);
cout<<endl;
vector<double> prod(LIM);
transform(gr8.begin(),gr8.end(),prod.begin(),bind1st(multiplies<double>(),2.5));
cout<<"prod: \t";
for_each(prod.begin(),prod.end(),Show);
cout<<endl;
system("pause");
}
void Show(double v)
{
cout.width(6);
cout<<v<<' ';
}