变异算法: 修改容器中的元素是主要的特点
1,复制:
copy(),copy_backward()
template
OutIt copy(Init first, Init last , OutIt x);
template
BidIt2 copy_backward(BidIt1 first,BidIt1 last,BidIt2 x);
示例:
#include
#include
#include
using namespace std;
int main()
{
int a[5] = {1,2,3,4,5};
int b[5];
vector v;
copy(a,a+5,b);
copy(a,a+5,back_inserter(v));
cout << "Org arrar a : " ;
copy(a,a+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "Array b : " ;
copy(b,b+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "Vector : ";
copy(v.begin(),v.end(),ostream_iterator(cout,"\t"));
return 0;
}
2,交换
swap(),swap_ranges(),iter_swap()
template
void swap(T &a,T&b);
template
FwdIt2 swap_ranges(FwdIt1 first,FwdIt1 last,FwdIt2 x);
template
void iter_swap(FwdIt1 x,FwdIt2 y);
示例:
#include
#include
#include
#include
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << "Org data : a = " << a << "\tb= " << b << endl;
swap(a,b);
cout << "After swap data : a = " << a << "\tb = " << b << endl << endl;
int a2[5] = {1,2,3,4,5};
int b2[5] = {6,7,8,9,10};
cout << "Org a2[5] = ";
copy(a2,a2+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org b2[5] = ";
copy(b2,b2+5,ostream_iterator(cout,"\t"));
cout << endl;
swap_ranges(a2,a2+5,b2);
cout << "After swap : a2[5] = ";
copy(a2,a2+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "After swap : b2[5] = ";
copy(b2,b2+5,ostream_iterator(cout,"\t"));
cout << endl;
int a3[5] = {10,20,30,40,50};
int b3[5] = {15,25,35,45,55};
vector v1(a3,a3+5);
vector v2(b3,b3+5);
cout << "Org vector1 = ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org vector2 = ";
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
swap(v1,v2);
cout << "After swap vector1 = ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "After swap vector2 = ";
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
3,变换
transfrom();
template
OutIt transform(Init first,Init last,OutIt x,Unop uop);
template
OutIt transform(Init1 first1,Init1 last1,Init2 first1,OutIt x,Binop bop)
示例:
1,>
#include
#include
#include
#include
using namespace std;
int fun1(int value)
{
return value * 2;
}
int fun2(int value1,int value2)
{
return value1 + value2;
}
int main()
{
int a[5] = {1,2,3,4,5};
vector v1(a,a+5);
vector v2(5);
cout << "Org vector v1 = ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "v1 * 2 -- v1 = ";
transform(v1.begin(),v1.end(),v1.begin(),fun1);
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "v1 * 2 -- v2 = ";
transform(v1.begin(),v1.end(),v2.begin(),fun1);
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
int a2[5] = {1,2,3,4,5};
int b2[5] = {6,7,8,9,10};
int c2[5];
cout << "a2[5] = ";
copy(a2,a2+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "b2[5] = ";
copy(b2,b2+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "a2+b2 -- c2 = ";
transform(a2,a2+5,b2,c2,fun2);
copy(c2,c2+5,ostream_iterator(cout,"\t"));
return 0;
}
2,>
#pragma warning(disable:4786)
#include
#include
#include
#include
#include
#include
#include
using namespace std;
template
class Encrypt
{
;
};
template<>
class Encrypt
{
public:
string operator() (const string &src)
{
string s = src;
for(string::iterator it = s.begin(); it != s.end();it ++)
{
*it = *it + 1;
}
return s;
}
};
int main()
{
string strText;
vector v;
vector vResult;
ifstream in("a.txt");
while(!in.eof())
{
getline(in,strText,'\n');
v.push_back(strText);
}
in.close();
transform(v.begin(),v.end(),back_inserter(vResult),Encrypt());
copy(vResult.begin(),vResult.end(),ostream_iterator(cout,"\n"));
return 0;
}
4,替换:
replace(), replace_if(), replace_copy(),replace_copy_if()
template
void replace(FwdIt first,FwdIt last,const T & vold,const T &vnew);
template
void replace_if(FwdIt first,FwdIt last,Pred pr,const T &val);
template
OutIt replace_copy(Init first,Init last,OutIt x,const T &vold,const T &vnew);
template
OutIt replace_copy_if(Init first,Init last,OutIt x,Pred pr,const T &val);
示例:
1,>
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int a[9] = {1,2,3,4,5,4,3,2,1};
cout << "Org data: ";
copy(a,a+9,ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org 2 replace to 10 (replace) : ";
vector v1(a,a+9);
replace(v1.begin(),v1.end(),2,10);
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org <4 replace to 20 : ";
vector v2(a,a+9);
replace_if(v2.begin(),v2.end(),bind2nd(less(),4),20);
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org 4 replace to 30 and v2 -> v4 (replace_copy) : ";
vector v3(a,a+9);
vector v4;
replace_copy(v3.begin(),v3.end(),back_inserter(v4),4,30);
copy(v4.begin(),v4.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org < 4 replace to 40 and v5 -> v6 (replace_copy_if): ";
vector v5(a,a+9);
vector v6;
replace_copy_if(v5.begin(),v5.end(),back_inserter(v6),bind2nd(less(),4),40);
copy(v6.begin(),v6.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
2,>
以出错程序:
#include
#include
using namespace std;
int main()
{
int a[] = {2,1,3,2,2,5};
replace(a,a+6,a[0],10);
copy(a,a+6,ostream_iterator(cout,"\t"));
return 0;
}
改正:
#include
#include
using namespace std;
int main()
{
int a[] = {2,1,3,2,2,5};
int t = a[0];
replace(a,a+6,t,10); //引用传递
copy(a,a+6,ostream_iterator(cout,"\t"));
return 0;
}
5,填充:
fill() , fill_n()
template
void fill(FwdIt first,FwdIt last,const T &x);
template
void fill_n(OutIt first,Size n,const T &x);
示例:
#include
#include
#include
#include
using namespace std;
int main()
{
int a[5];
fill(a,a+5,0);
cout << "a[5] = ";
copy(a,a+5,ostream_iterator(cout,"\t"));
cout << endl;
vector v1(5);
fill(v1.begin(),v1.end(),10);
cout << "vector v1 = ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector v2;
fill_n(back_inserter(v2),5,20);
cout << "vector v2 = ";
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
6,生成:
generate() , generate_n()
template
void generate(FwdIt first,FwdIt last,Gen g);
template
void generate_n(OutIt first,Dist n,Gen g);
示例:
1,> //不能重新生成,由于静态变量的存在
#include
#include
#include
using namespace std;
int Fibonacci()
{
static int r ;
static int f1 = 0 ;
static int f2 = 1;
r = f1 + f2;
f1 = f2;
f2 = r;
return f1;
}
int main()
{
vector v1(10);
generate(v1.begin(),v1.end(),Fibonacci);
cout << "0,1 start the first 10 element of Fib is : " << endl;
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector v2(10);
generate(v2.begin(),v2.end(),Fibonacci);
cout << "0,1 start the first 10 element of Fib is : " << endl;
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
2,> 修改后的程序
#include
#include
#include
using namespace std;
class Fibonacci
{
int f1;
int f2;
public:
Fibonacci(int start1,int start2)
{
f1 = start1;
f2 = start2;
}
int operator() ()
{
int r = f1 + f2;
f1 = f2;
f2 = r;
return r;
}
};
int main()
{
vector v1(10);
generate(v1.begin(),v1.end(),Fibonacci(0,1));
cout << "0,1 start the first 10 element of Fib is : " << endl;
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector v2(10);
generate(v2.begin(),v2.end(),Fibonacci(0,1));
cout << "0,1 start the first 10 element of Fib is : " << endl;
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
3,>
#include
#include
#include
#include
#include
using namespace std;
template
class MyRandom
{
;
};
template<>
class MyRandom
{
public:
MyRandom()
{
srand(time(NULL));
}
int operator() ()
{
int result = rand() % 100;
return result;
}
};
template<>
class MyRandom
{
public:
MyRandom()
{
srand(time(NULL));
}
float operator() ()
{
float result = rand() % 100 * 0.01f;
return result;
}
};
int main()
{
cout << "generate [0,100) 10 times random data: " << endl;
vector v1(10);
generate_n(v1.begin(),10,MyRandom());
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "generate [0,1) 10 times random data: " << endl;
vector v2(10);
generate_n(v2.begin(),10,MyRandom());
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
4,>
#include
#include
#include
#include
using namespace std;
struct Point
{
float x;
float y;
};
class CircleTag
{
};
template
class MyCurve
{
};
template<>
class MyCurve
{
float ox;
float oy;
float r;
int angle;
public:
MyCurve(float ox,float oy,float r,int angle)
{
this -> ox = ox;
this -> oy = oy;
this -> r = r;
this -> angle = angle;
}
Point operator() ()
{
Point pt;
static int curAngle = 0;
pt.x = ox + r * cos(curAngle/360.0f * 2 * 3.14f);
pt.y = oy + r * sin(curAngle/360.0f * 2 * 3.14f);
curAngle += angle;
return pt;
}
};
ostream & operator<< (ostream &os,const Point &t)
{
os << "(" << t.x <<"," << t.y << ")";
return os;
}
int main()
{
vector v(10);
generate(v.begin(),v.end(),MyCurve(10.0f,10.0f,10.0f,36));
cout << "Take a point from 0 to 360 every 36 : " << endl;
copy(v.begin(),v.end(),ostream_iterator(cout,"\n"));
return 0;
}
7,删除
remove(), remove_if() , remove_copy() , remove_copy_if()
template
FwdIt remove(FwdIt first,FwdIt last, const T & val);
template
FwdIt remove_if(FwdIt first,FwdIt last,Pred pr);
template
OutIt remove_copy(Init first,Init last,OutIt x,const T &val);
template
OutIt remove_copy_if(Init first,Init last, OutIt x, Pred pr);
示例:
1,>
#include
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,2,2,4,5,4,3,2,1};
vector v1(a,a+9);
cout << "Before remove: ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector::iterator first = v1.begin();
vector::iterator last = NULL;
last = remove(v1.begin(),v1.end(),2);
cout << "After remove: ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "After remove the effective data: " ;
copy(first,last,ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
2,>
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,2,3,4,5};
cout << "Org data: ";
copy(a,a+5,ostream_iterator(cout,"\t"));
cout << endl;
vector v1(a,a+5);
vector::iterator last1 = remove_copy(v1.begin(),v1.end(),v1.begin(),3);
cout << "After remove 3 : ";
copy(v1.begin(),last1,ostream_iterator(cout,"\t"));
cout << endl;
vector v2(a,a+5);
vector v3;
cout << "move 3 to another vector: ";
remove_copy(v2.begin(),v2.end(),back_inserter(v3),3);
copy(v3.begin(),v3.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
3,>
#include
#include
#include
#include
#include
#include
using namespace std;
class Student
{
public:
string name;
string studno;
int chinese;
int math;
public:
Student()
{
}
Student(string name,string studno,int chinese,int math)
{
this -> name = name;
this -> studno = studno;
this -> chinese = chinese;
this -> math = math;
}
bool operator< (const int &total) const
{
return ((chinese + math) < total);
}
};
bool MyCompare(const Student &s)
{
return s < 10;
}
ostream & operator << (ostream &os,const Student &s)
{
os << s.name << "\t" << s.chinese << "\t" << s.math << endl;
return os;
}
int main()
{
Student s1("zhang","1001",60,70);
Student s2("li","1002",70,80);
Student s3("zhao","1003",75,85);
Student s4("wang","1004",68,78);
Student s5("zhou","1005",86,76);
Student s6("qian","1006",30,80);
vector v;
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
v.push_back(s5);
v.push_back(s6);
ofstream out("1.txt");
remove_copy_if(v.begin(),v.end(),ostream_iterator(out,"\n"),MyCompare);
out.close();
return 0;
}
8,唯一:
unique() , unique_copy()
template
FwdIt unique(FwdIt first,FwdIt last);
template
FwdIt unique(FwdIt first,FwdIt last, Pred pr);
template
OutIt unique_copy(Init first,Init last,OutIt x);
template
OutIt unique_copy(Init first,Init last,OutIt x,Pred pr)
示例:
1,,>
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,2,2,3,4,2,2,5};
vector v1(a,a+8);
cout << "Org vector v1 = ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector::iterator last = unique(v1.begin(),v1.end());
cout << "After unique v1 = ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "After unique the effective v1 = ";
copy(v1.begin(),last,ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
2,>
#pragma warning(disable : 4786)
#include
#include
#include
#include
#include
using namespace std;
bool MyStrCompare(string s1,string s2)
{
bool bRet = false;
int value = stricmp(s1.c_str(),s2.c_str());
if(value == 0)
bRet = true;
return bRet;
}
int main()
{
ifstream out("1.txt");
vector v;
copy(istream_iterator(out),istream_iterator(),back_inserter(v));
cout << endl;
cout <<"The word in txt: ";
copy(v.begin(),v.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector vstr;
cout << "remove repeated word: ";
unique_copy(v.begin(),v.end(),back_inserter(vstr),MyStrCompare);
copy(vstr.begin(),vstr.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
9,反转:
reverse() , reverse_copy()
template
void reverse(BidIt first,BidIt last);
template
OutIt reverse_copy(BidIt first,BidIt last, OutIt x);
示例:
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,2,3,4,5};
vector v1(a,a+5);
cout << "Org vector v1 = ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "reverse vector v1: ";
reverse(v1.begin(),v1.end());
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector v2(a,a+5);
reverse_copy(v2.begin(),v2.end(),v2.begin());
cout << "reverse vector v2 --> v2(reverse_copy) : ";
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
vector v3(a,a+5);
vector v4;
reverse_copy(v3.begin(),v3.end(),back_inserter(v4));
cout << "reverse vector v3 --> v4 (reverse_copy): ";
copy(v4.begin(),v4.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
10,环移:
rotate() , rotate_copy()
template
void rotate(FwdIt first , FwdIt middle , FwdIt last);
template
OutIt rotate(FwdIt first,FwdIt middle,FwdIt last, OutIt x);
示例:
1,>
#include
#include
#include
#include
#include
using namespace std;
void delay()
{
int start = time(NULL);
int end = start;
do
{
if(start != end)
break;
}while(end = time(NULL));
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8};
vector v(a,a+8);
cout << endl;
while(!kbhit())
{
copy(v.begin(),v.end(),ostream_iterator(cout,"\t"));
rotate(v.begin(),v.begin()+1,v.end());
delay();
cout << endl;
}
return 0;
}
2,>
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,2,3,4,5,6,7,8};
cout << "Org data a[] = ";
copy(a,a+8,ostream_iterator(cout,"\t"));
cout << endl;
list v1(a,a+8);
list::iterator middle = v1.begin();
advance(middle,3);
rotate(v1.begin(),middle,v1.end());
cout << "Circle 4 rotate: ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
list v2(a,a+8);
list v3;
middle = v2.begin();
cout << "Circle 4 rotate v2 --> v3(rotate_copy) : ";
advance(middle,3);
rotate_copy(v2.begin(),middle,v2.end(),back_inserter(v3));
copy(v3.begin(),v3.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
11,随机:
random_shuffle()
template
void random_shuffle(RanIt first,RanIt last);
template
void random_shuffle(RanIt first, RanIt last, Fun &f);
示例:
1,> 每次产生的随机数序列是固定的!!!
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,2,3,4,5};
float b[] = {1.1f,2.1f,3.1f,4.1f,5.1f};
char c[] = {'a','b','c','d','e'};
cout << "Org a[] = ";
copy(a,a+5,ostream_iterator(cout,"\t"));
cout << endl;
random_shuffle(a,a+5);
cout << "After random_shuffle a[] = ";
copy(a,a+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org b[] = ";
copy(b,b+5,ostream_iterator(cout,"\t"));
cout << endl;
random_shuffle(b,b+5);
cout << "After random_shuffle: ";
copy(b,b+5,ostream_iterator(cout,"\t"));
cout << endl;
cout << "Org c[] = ";
copy(c,c+5,ostream_iterator(cout,"\t"));
cout << endl;
random_shuffle(c,c+5);
cout << "After random_shuffle: ";
copy(c,c+5,ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
2,> 要解决上面问题,需要用到第二个模板
#include
#include
#include
#include
#include
using namespace std;
class MyRandom
{
public:
int operator() (int n)
{
srand(time(NULL));
return rand() % n;
}
};
int main()
{
int a[] = {10,21,32,43,54,65,76,87,98};
vector v(a,a+5);
random_shuffle(v.begin(),v.end(),MyRandom());
copy(v.begin(),v.end(),ostream_iterator(cout,"\t"));
cout << endl;
random_shuffle(v.begin(),v.end(),MyRandom());
copy(v.begin(),v.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
3,>
#include
#include
#include
#include
#include
using namespace std;
class MyRand
{
int total;
public:
MyRand(int total)
{
this -> total = total;
}
int operator() (int n)
{
srand(time(NULL));
int m = rand() % n;
if(n >= total/2 && m < total/2)
{
m += total/2;
}
return m;
}
};
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9};
vector v(a,a+9);
MyRand obj(9);
char ch;
while((ch = getch()) != 'a')
{
random_shuffle(v.begin(),v.end(),obj);
copy(v.begin(),v.end(),ostream_iterator(cout,"\t"));
cout << endl;
}
return 0;
}
12,划分:
partition() , stable_partition()
template
BidIt partition(BidIt first,BidIt last,Pred pr);
template
FwdIt stable_partition(FwdIt first,Fwdit last,Pred pr);
示例:
1,>
#include
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,7,2,5,3,4,8,2,3,6};
vector v1(a,a+10);
cout << "Org data: ";
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
cout << "As <4 partition: ";
partition(v1.begin(),v1.end(),bind2nd(less(),4));
copy(v1.begin(),v1.end(),ostream_iterator(cout,"\t"));
cout << endl;
int b[] = {1,7,2,5,3,4,8,2,3,6};
vector v2(b,b+10);
cout << "As <4 stable_partition: ";
stable_partition(v2.begin(),v2.end(),bind2nd(less(),4));
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
2,>
#include
#include
#include
#include
#include
using namespace std;
struct Student
{
char name[20];
int grade;
};
bool GradeCmp(Student &s)
{
return s.grade < 75;
}
ostream & operator<< (ostream &os,const Student &s)
{
os << "(" << s.name << "\t" << s.grade <<")";
return os;
}
int main()
{
Student s[] = {{"li1",79},{"li2",70},{"li3",68},{"li4",78},{"li5",65}};
cout << "Org que: " << endl;
copy(s,s+5,ostream_iterator(cout,"\t"));
cout << endl;
vector v1(s,s+5);
cout << "Not stable partition grade <75 (partition) : " <(cout,"\t"));
cout << endl;
vector v2(s,s+5);
cout << "stable partition grade <75 (partition) : " << endl;
stable_partition(v2.begin(),v2.end(),GradeCmp);
copy(v2.begin(),v2.end(),ostream_iterator(cout,"\t"));
cout << endl;
return 0;
}
3,>
#include
#include
#include
#include
using namespace std;
int main()
{
int a[] = {1,7,3,6,4,10,9,5,2,8};
list v(a,a+10);
list::iterator mid = partition(v.begin(),v.end(),bind2nd(less(),4));
int nSize = distance(v.begin(),mid);
cout << nSize << endl;
return 0;
}