STL 算法

变异算法: 修改容器中的元素是主要的特点

 

1,复制:

copy(),copy_backward()


template<class Init, class OutIt>
OutIt copy(Init first, Init last , OutIt x);

template<class BidIt1,class BidIt2>
BidIt2 copy_backward(BidIt1 first,BidIt1 last,BidIt2 x);

 

 

 

示例:

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
	int a[5] = {1,2,3,4,5};
	int b[5];
	vector<int> v;

	copy(a,a+5,b);
	copy(a,a+5,back_inserter(v));

	cout << "Org arrar a : " ;
	copy(a,a+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "Array b : " ;
	copy(b,b+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "Vector : ";
	copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));

	return 0;
}


 

2,交换

swap(),swap_ranges(),iter_swap()


template<class T>
void swap(T &a,T&b);
 
template<class FwdIt1,class FwdIt2>
FwdIt2 swap_ranges(FwdIt1 first,FwdIt1 last,FwdIt2 x);

template<class FwdIt1,class FwdIt2>
void iter_swap(FwdIt1 x,FwdIt2 y);


示例:

 

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

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<int>(cout,"\t"));
	cout << endl;
	cout << "Org b2[5] = ";
	copy(b2,b2+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;
	swap_ranges(a2,a2+5,b2);
	cout << "After swap : a2[5] = ";
	copy(a2,a2+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;
	cout << "After swap : b2[5] = ";
	copy(b2,b2+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;


	int a3[5] = {10,20,30,40,50};
	int b3[5] = {15,25,35,45,55};
	vector<int> v1(a3,a3+5);
	vector<int> v2(b3,b3+5);
	cout << "Org vector1 = ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;
	cout << "Org vector2 = ";
	copy(v2.begin(),v2.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;
	swap(v1,v2);
	cout << "After swap vector1 = ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;
	cout << "After swap vector2 = ";
	copy(v2.begin(),v2.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


 

 3,变换

transfrom();


template<class Init,class OutIt,class Unop>
OutIt  transform(Init first,Init last,OutIt x,Unop uop);

template<class Init1,class Init2,class OutIt,class Binop>
OutIt transform(Init1 first1,Init1 last1,Init2 first1,OutIt x,Binop bop)


 

示例:

1,>

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

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<int> v1(a,a+5);
	vector<int> v2(5);

	cout << "Org vector v1 = ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;
	cout << "v1 * 2 -- v1 = ";
	transform(v1.begin(),v1.end(),v1.begin(),fun1);
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;
	cout << "v1 * 2 -- v2 = ";
	transform(v1.begin(),v1.end(),v2.begin(),fun1);
	copy(v2.begin(),v2.end(),ostream_iterator<int>(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<int>(cout,"\t"));
	cout << endl;
	cout << "b2[5] = ";
	copy(b2,b2+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;
	cout << "a2+b2 -- c2 = ";
	transform(a2,a2+5,b2,c2,fun2);
	copy(c2,c2+5,ostream_iterator<int>(cout,"\t"));

	return 0;
}


 

 2,>

 

#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

using namespace std;

template<class T>
class Encrypt
{
	;
};

template<>
class Encrypt<string>
{
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<string> v;
	vector<string> 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<string>());
	copy(vResult.begin(),vResult.end(),ostream_iterator<string>(cout,"\n"));

	return 0;
}


4,替换:

replace(), replace_if(), replace_copy(),replace_copy_if()


template<class FwdIt,class T>
void replace(FwdIt first,FwdIt last,const T & vold,const T &vnew);

template<class FwdIt,class Pred,class T>
void replace_if(FwdIt first,FwdIt last,Pred pr,const T &val);

template<class Init,class OutIt,class T>
OutIt replace_copy(Init first,Init last,OutIt x,const T &vold,const T &vnew);

template<class Init,class OutIt,class Pred,class T>
OutIt  replace_copy_if(Init first,Init last,OutIt x,Pred pr,const T &val);

 

示例:

1,>

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

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<int>(cout,"\t"));
	cout << endl;

	cout << "Org 2 replace to 10 (replace) : ";
	vector<int> v1(a,a+9);
	replace(v1.begin(),v1.end(),2,10);
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "Org <4  replace to 20 : ";
	vector<int> v2(a,a+9);
	replace_if(v2.begin(),v2.end(),bind2nd(less<int>(),4),20);
	copy(v2.begin(),v2.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "Org 4 replace to 30 and v2 -> v4 (replace_copy) : ";
	vector<int>  v3(a,a+9);
	vector<int> v4;
	replace_copy(v3.begin(),v3.end(),back_inserter(v4),4,30);
	copy(v4.begin(),v4.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "Org < 4 replace to 40 and v5 -> v6 (replace_copy_if): ";
	vector<int> v5(a,a+9);
	vector<int> v6;
	replace_copy_if(v5.begin(),v5.end(),back_inserter(v6),bind2nd(less<int>(),4),40);
	copy(v6.begin(),v6.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


 

2,>

以出错程序:

#include <iostream>
#include <algorithm>

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<int>(cout,"\t"));

	return 0;
}


 

改正:

#include <iostream>
#include <algorithm>

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<int>(cout,"\t"));

	return 0;
}

 

5,填充:

fill() , fill_n() 
 

template<class FwdIt,class T>
void fill(FwdIt first,FwdIt last,const T &x);

template<class OutIt,class Size,class T>
void fill_n(OutIt first,Size n,const T &x);


 

示例:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

int main()
{
	int a[5];
	fill(a,a+5,0);
	cout << "a[5] = ";
	copy(a,a+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;

	vector<int> v1(5);
	fill(v1.begin(),v1.end(),10);
	cout << "vector v1 = ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	vector<int> v2;
	fill_n(back_inserter(v2),5,20);
	cout << "vector v2 = ";
	copy(v2.begin(),v2.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


 

6,生成:

generate() , generate_n()

template<class FwdIt,class Gen>
void generate(FwdIt first,FwdIt last,Gen g);

template<class OutIt,class Pred,class Gen>
void generate_n(OutIt first,Dist n,Gen g);


 示例:

1,>  //不能重新生成,由于静态变量的存在

 

#include <iostream>
#include <vector>
#include <algorithm>

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<int> 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<int>(cout,"\t"));
	cout << endl;

	vector<int> 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<int>(cout,"\t"));
	cout << endl;
	return 0;
}


2,>  修改后的程序

 

#include <iostream>
#include <vector>
#include <algorithm>

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<int> 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<int>(cout,"\t"));
	cout << endl;

	vector<int> 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<int>(cout,"\t"));
	cout << endl;

	return 0;
}


 

 

3,>

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>

using namespace std;

template<class T>
class MyRandom
{
	;
};

template<>
class MyRandom<int>
{
public:
	MyRandom()
	{
		srand(time(NULL));
	}

	int operator() ()
	{
		int result = rand() % 100;
		return result;
	}
};

template<>
class MyRandom<float>
{
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<int> v1(10);
	generate_n(v1.begin(),10,MyRandom<int>());
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "generate [0,1)   10 times random data: " << endl;
	vector<float> v2(10);
	generate_n(v2.begin(),10,MyRandom<float>());
	copy(v2.begin(),v2.end(),ostream_iterator<float>(cout,"\t"));
	cout << endl;

	return 0;
}


 

4,>

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

struct Point
{
	float x;
	float y;
};

class CircleTag
{
};

template<class T>
class MyCurve
{
};

template<>
class MyCurve<CircleTag>
{
	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<Point> v(10);
	generate(v.begin(),v.end(),MyCurve<CircleTag>(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<Point>(cout,"\n"));

	return 0;
}


 

 

7,删除

 

remove(), remove_if() , remove_copy() , remove_copy_if() 


template<class FwdIt,class T>
FwdIt remove(FwdIt first,FwdIt last, const T & val);

template<class FwdIt,class Pred>
FwdIt remove_if(FwdIt first,FwdIt last,Pred pr);

template<class Init,class OutIt,class T>
OutIt remove_copy(Init first,Init last,OutIt x,const T &val);

template<class Init,class OutIt,class Pred>
OutIt remove_copy_if(Init first,Init last, OutIt x, Pred pr);

示例:

 

1,>

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
	int a[] = {1,2,2,4,5,4,3,2,1};
	vector<int> v1(a,a+9);

	cout << "Before remove: ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	vector<int>::iterator first = v1.begin();
	vector<int>::iterator last = NULL;

	last = remove(v1.begin(),v1.end(),2);

	cout << "After remove: ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;
	cout << "After remove the effective data: " ;
	copy(first,last,ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


2,>

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int a[] = {1,2,3,4,5};
	cout << "Org data: ";
	copy(a,a+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;

	vector<int> v1(a,a+5);
	vector<int>::iterator last1 = remove_copy(v1.begin(),v1.end(),v1.begin(),3);
	cout << "After remove 3 : ";
	copy(v1.begin(),last1,ostream_iterator<int>(cout,"\t"));
	cout << endl;


	vector<int> v2(a,a+5);
	vector<int> v3;
	cout << "move 3 to another vector: ";
	remove_copy(v2.begin(),v2.end(),back_inserter(v3),3);
	copy(v3.begin(),v3.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


3,>

 

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

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<Student> 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<Student>(out,"\n"),MyCompare);
	out.close();

	return 0;
}


8,唯一:

 

unique() , unique_copy() 


template<class FwdIt>
FwdIt unique(FwdIt first,FwdIt last);

template<class FwdIt,class Pred>
FwdIt unique(FwdIt first,FwdIt last, Pred pr);

template<class Init,class OutIt>
OutIt unique_copy(Init first,Init last,OutIt x);

template<class Init,class OutIt,class Pred>
OutIt unique_copy(Init first,Init last,OutIt x,Pred pr)

示例:

1,,>

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int a[] = {1,2,2,3,4,2,2,5};
	vector<int> v1(a,a+8);
	cout << "Org vector v1 = ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;


	vector<int>::iterator last = unique(v1.begin(),v1.end());

	cout << "After unique v1 = ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "After unique the effective v1 = ";
	copy(v1.begin(),last,ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}



2,>

 

#pragma warning(disable : 4786)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

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<string> v;
	copy(istream_iterator<string>(out),istream_iterator<string>(),back_inserter(v));
	cout << endl;

	cout <<"The word in txt: ";
	copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\t"));
	cout << endl;

	vector<string> vstr;
	cout << "remove repeated word: ";
	unique_copy(v.begin(),v.end(),back_inserter(vstr),MyStrCompare);
	copy(vstr.begin(),vstr.end(),ostream_iterator<string>(cout,"\t"));
	cout << endl;

	return 0;
}
		


9,反转:

 

reverse() , reverse_copy()

template<class BidIt>
void reverse(BidIt first,BidIt last);

template<class BidIt,class OutIt>
OutIt reverse_copy(BidIt first,BidIt last, OutIt x);

示例:

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int a[] = {1,2,3,4,5};

	vector<int> v1(a,a+5);
	cout << "Org vector v1 = ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "reverse vector v1: ";
	reverse(v1.begin(),v1.end());
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	vector<int> 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<int>(cout,"\t"));
	cout << endl;

	vector<int> v3(a,a+5);
	vector<int> v4;
	reverse_copy(v3.begin(),v3.end(),back_inserter(v4));
	cout << "reverse vector v3 --> v4 (reverse_copy): ";
	copy(v4.begin(),v4.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


 

10,环移:

 

rotate() , rotate_copy() 

template<class FwdIt>
void rotate(FwdIt first , FwdIt middle , FwdIt last);

template<class FwdIt, class OutIt>
OutIt rotate(FwdIt first,FwdIt middle,FwdIt last, OutIt x);

示例:

1,>

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <conio.h>
#include <time.h>

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<int> v(a,a+8);
	cout << endl;

	while(!kbhit())
	{
		copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
		rotate(v.begin(),v.begin()+1,v.end());
		delay();
		cout << endl;
	}

	return 0;
}


2,>

 

#include <list>
#include <iostream>
#include <vector>
#include <algorithm>
#include <conio.h>
#include <time.h>

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<int>(cout,"\t"));
	cout << endl;

	list<int> v1(a,a+8);
	list<int>::iterator middle = v1.begin();
	advance(middle,3);
	rotate(v1.begin(),middle,v1.end());
	cout << "Circle 4 rotate: ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	list<int> v2(a,a+8);
	list<int> 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<int>(cout,"\t"));
	cout << endl;


	return 0;
}


 

11,随机:

 

random_shuffle() 

template<class RanIt>
void random_shuffle(RanIt first,RanIt last);

template<class Ranit, class Fun>
void random_shuffle(RanIt first, RanIt last, Fun &f);

示例:

1,>     每次产生的随机数序列是固定的!!!

 

#include <iostream>
#include <algorithm>
#include <vector>

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<int>(cout,"\t"));
	cout << endl;
	random_shuffle(a,a+5);
	cout << "After random_shuffle a[] = ";
	copy(a,a+5,ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "Org b[] = ";
	copy(b,b+5,ostream_iterator<float>(cout,"\t"));
	cout << endl;
	random_shuffle(b,b+5);
	cout << "After random_shuffle: ";
	copy(b,b+5,ostream_iterator<float>(cout,"\t"));
	cout << endl;

	cout << "Org c[] = ";
	copy(c,c+5,ostream_iterator<char>(cout,"\t"));
	cout << endl;
	random_shuffle(c,c+5);
	cout << "After random_shuffle: ";
	copy(c,c+5,ostream_iterator<char>(cout,"\t"));
	cout << endl;

	return 0;
}


 

2,>  要解决上面问题,需要用到第二个模板

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <conio.h>
#include <time.h>

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<int> v(a,a+5);
	random_shuffle(v.begin(),v.end(),MyRandom());
	copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	random_shuffle(v.begin(),v.end(),MyRandom());
	copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


 

3,>

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <conio.h>
#include <time.h>

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<int> 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<int>(cout,"\t"));
			cout << endl;
		}
		return 0;
}


 

12,划分:

 

partition() , stable_partition()

template<class BidIt,class Pred>
BidIt partition(BidIt first,BidIt last,Pred pr);

template<class FwdIt , class Pred>
FwdIt stable_partition(FwdIt first,Fwdit last,Pred pr);

示例:

1,>

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;


int main()
{
	int a[] = {1,7,2,5,3,4,8,2,3,6};
	vector<int> v1(a,a+10);
	cout << "Org data: ";
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	cout << "As <4 partition: ";
	partition(v1.begin(),v1.end(),bind2nd(less<int>(),4));
	copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	int b[] = {1,7,2,5,3,4,8,2,3,6};
	vector<int> v2(b,b+10);

	cout << "As <4 stable_partition: ";
	stable_partition(v2.begin(),v2.end(),bind2nd(less<int>(),4));
	copy(v2.begin(),v2.end(),ostream_iterator<int>(cout,"\t"));
	cout << endl;

	return 0;
}


2,>

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>

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<Student>(cout,"\t"));
	cout << endl;

	vector<Student> v1(s,s+5);
	cout << "Not stable partition grade <75 (partition) : " <<endl;
	partition(v1.begin(),v1.end(),GradeCmp);
	copy(v1.begin(),v1.end(),ostream_iterator<Student>(cout,"\t"));
	cout << endl;

	vector<Student> 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<Student>(cout,"\t"));
	cout << endl;

	return 0;
}


3,>

 

#include <iostream>
#include <list>
#include <algorithm>
#include<functional>

using namespace std;

int main()
{
	int a[] = {1,7,3,6,4,10,9,5,2,8};
	list<int> v(a,a+10);

	list<int>::iterator mid = partition(v.begin(),v.end(),bind2nd(less<int>(),4));
	int nSize = distance(v.begin(),mid);

	cout << nSize << endl;

	return 0;
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值