firemail
标题:
算法
[打印本页]
作者:
Qter
时间:
2020-1-1 14:59
标题:
算法
/*
* Copyright (c) 2009,CVICSE
* All rights reserved.
*
* 文件名称:algorithm.cpp
* 文件标识:通用算法技术
* 摘 要:常用算法
*
* 当前版本:1.0
* 作 者:draeag
* 完成日期:2009年7月10日
*
* 取代版本:
* 原作者 :
* 完成日期:
*
* 参考文献:C++STL程序员开发指南
* 页码 :p469 PDF:p448
*/
#include "stl.h"
//模板函数对象:打印元素
template <class Type>
class Print
{
public:
void operator() (Type& elem) const
{
cout << elem << " ";
}
};
//模板函数对象:元素与给定的因子相乘
template <class Type>
class MultValue
{
private:
Type Factor;
public:
MultValue(const Type& _Val) : Factor( _Val )
{
}
int operator() ( Type& elem ) const
{
elem *= Factor;
return elem;
}
};
//函数对象:计算平均数
class Average
{
private:
long num;
long sum;
public:
Average() : num(0), sum(0)
{
}
void operator() (int elem)
{
num ++;
sum += elem;
}
operator double () ///////////////////////////////////??????????????????????? 返回容器中所有元素的平均值
{
return static_cast<double>(sum)/static_cast<double>(num);
}
};
/*
* 函数介绍:参数1*2是否等于参数2
* 输入参数:
* 输出参数:
* 返回值:bool
*/
bool twice( int elem1, int elem2)
{
return elem1 * 2 == elem2;
}
bool DEGreater(int elem1, int elem2)
{
return elem1 > elem2;
}
bool mod_equal(int elem1, int elem2)
{
if (elem1 < 0 )
elem1 = -elem1;
if (elem2 < 0 )
elem2 = -elem2;
return elem1 == elem2;
}
int settwo(void)
{
return 2;
}
bool great5(int value)
{
return value > 5;
}
//函数对象字符串比较
struct twicefun
{
bool inline operator() (int elem1, int elem2)
{
return elem1 * 2 == elem2;
}
};
/*
* 函数介绍:输出iterator的模板函数
* 输入参数:@it:迭代器
* 输出参数:
* 返回值:void
*/
template <class ITERATOR>
void print_map_item(ITERATOR it)
{
cout << (*it).first << ", " << (*it).second << endl;
}
//函数对象字符串比较
struct ltstr
{
public:
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
void main()
{
// system("cls");
cout << endl;
cout << " ***********************************************************" << endl;
cout << " ** 非修正序列算法 **" << endl;
cout << " ***********************************************************" << endl;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 1.查找容器中相邻的元素 adjacent_find **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//adjacent_find差额函数:查找相邻相同元素,返回指向一对临近相等元素中的第一个元素的迭代器
const int ARRAY_SIZE = 8;
int IntArray[ARRAY_SIZE] = {1, 2, 3, 4, 4, 5, 6, 7};
int *location;
location = adjacent_find(IntArray, IntArray + ARRAY_SIZE);
cout << "在数组中相邻两个元素相等: "<< "(" << *location << ", " << *(location+1) << ")" << endl;
cout <<"位置在 " << location - IntArray << endl;
int counresult;
//在first和end范围内统计value元素的个数,语法如下
/*template< class InputIterator, class T> inline
size_t count(InputIterator first, InputIterator last, const T& value)*/
counresult = count(IntArray, IntArray + ARRAY_SIZE, 4);
cout << "元素4的个数为: "<< counresult << endl;
cout << lin;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 2.容器对象变量比较 equal **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
vector<int> v1, v2, v3;
for ( int i = 0; i <=5; i++)
{
v1.push_back(i);
v2.push_back(i);
v3.push_back(i*2);
}
v2.push_back(3);
bool b = false;
cout << endl;
cout << "v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
//equal如果两个对象变量相等返回true否则返回false 只能比较同类型不同变量,
//且只要first1到last1范围的值与first2起始相同范围的值相等,则函数返回true.
b = equal(v1.begin(), v1.end(), v2.begin());
if ( b )
{
cout << "v1 = v2" << endl;
}
else
{
cout << "v1 != v2" << endl;
}
//b = equal(v1.begin(), v1.end(), v3.begin(), twice); //函数指针法
b = equal(v1.begin(), v1.end(), v3.begin(), twicefun()); //函数对象法
if ( b )
{
cout << "v1*2 = v3" << endl;
}
else
{
cout << "v1*2 != v3" << endl;
}
cout << lin;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 3.查找元素find list vector **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//find函数返回first和last所规定的范围内的第一个数值等于value的元素的迭代器
//find引申很多谓词版本,如加一个比较函数,就产生算法find_if
//find_end(),find_first_of(),返回迭代器,
//指向最后(第一次)在first和last规定范围内找到first2和last2序列相等的子序列的位置
list<int> l;
l.push_back(10);
l.push_back(20);
l.push_back(30);
l.push_back(40);
list<int>::iterator relst;
relst = find(l.begin(), l.end(), 30);
if(relst != l.end())
{
cout << "30 在 l中,位置为: " /*<< relst - l.begin()*/ << endl; // 列表中不能直接减
}
else
{
cout << "30不在l中!"<< endl;
}
vector<int>::iterator result2;
result2 = find_end(v1.begin(),v1.end(),v3.begin(),v3.end(),twice);
if(result2 == v1.end())
{
cout << "v3不在v1中" << endl;
}
else
{
cout << "v3在v1中,开始位置为: " << result2 - v1.begin() << endl;
}
cout << lin;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 4.特殊循环函数 for_each() **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
vector<int>::iterator Iter1;
v1.clear();
int i;
for ( i = -4; i <= 2; i++)
{
v1.push_back( i );
}
cout << "原v1 = ( ";
for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
{
cout << *Iter1 << " ";
}
cout << ")." << endl;
//把每个元素都乘以因子-2
for_each( v1.begin(), v1.end(), MultValue<int>(-2));
cout << "乘-2后: v1 = ( ";
for_each(v1.begin(), v1.end(), Print<int>());
cout << ")." << endl;
//把每个元素都乘以因子5
for_each( v1.begin(), v1.end(), MultValue<int>(5));
cout << "乘5后: v1 = ( ";
for_each(v1.begin(), v1.end(), Print<int>());
cout << ")." << endl;
//求平均值
double avemod2 = for_each( v1.begin(), v1.end(), Average());
cout << "v1的平均值为: " << avemod2 << endl;
cout << lin;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 3.不相等元素查找 **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
vector<int>::iterator it1;
pair<vector<int>::iterator, list<int>::iterator> pair_results1;
pair<vector<int>::iterator, vector<int>::iterator> pair_results2;
//mismatch(),不相等元素查找,返回一对迭代器,指向在first和last所规定的范围内第一个不相等的元素的位置,以及first2开始与first及last相同的长度的序列中第一个不相同的元素的位置
v1.clear();
v2.clear();
list<int> l1;
for (int i = 0; i <= 5; i++)
{
v1.push_back(i);
v2.push_back(i*2);
}
for(int i = 0; i<= 7; i++)
{
l1.push_back(i*10);
}
v2.push_back(16);
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "list l1: ";
copy(l1.begin(), l1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
pair_results1 = mismatch(v1.begin(), v1.end(), l1.begin());
if (pair_results1.first == v1.end())
{
cout << "v1和l1相匹配"<<endl;
}
else
{
cout << "v1和l1有差异,第一次不匹配出现在" << *pair_results1.first << "和" << *pair_results1.second << endl;
}
pair_results2 = mismatch(v1.begin(), v1.end(), v2.begin(), twice);
if (pair_results2.first == v1.end())
{
cout << "v1和v2相匹配,且v2是v1元素的两倍"<<endl;
}
else
{
cout << "v1和v2第一次不匹配出现在" << *pair_results2.first << "和" << *pair_results2.second << endl;
}
cout << endl;
v2.clear();
v2.push_back(2);
v2.push_back(3);
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
//search返回第一次出现在first1和last1所规定的范围内与序列first2和last2相等的位置
it1 = search(v1.begin(), v1.end(), v2.begin(), v2.end());
if (it1 == v1.end())
{
cout << "在v1中找不到v2" << endl;
}
else
{
cout << "v2在v1中第一次出现的相对位置为" << it1 - v1.begin() << endl;
}
cout << endl;
cout << " ***********************************************************" << endl;
cout << " ** 修正序列算法 **" << endl;
cout << " ***********************************************************" << endl;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 1.元素复制 copy copy_backward **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//copy把first及last所规定的范围内的元素复制到另外一个由迭代器_DestBeg指向的元素所开始的范围,原来的元素则被覆盖,被拷贝的最后一个元素不包含在内
//copy_backward区别是从最后一个元素开始复制由后向前直到第一个元素
v1.clear();
v2.clear();
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i+100);
}
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "把v1的前三个元素拷贝到v2中第五个元素开始的连续三个元素";
copy( v1.begin(), v1.begin() + 3, v2.begin() + 4);
cout << endl;
cout << "*v1.begin(): " << *v1.begin() << endl;
cout << "*(v1.begin()+3): " << *(v1.begin()+3) << "最后一个元素不包含在内" << endl;
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "把v2的倒数第二和倒第三的两个元素拷贝到第一和第二" << endl ;
copy( v2.begin()+7, v2.begin() + 9, v2.begin());
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
copy_backward(v1.begin() + 1, v1.begin() + 4, v1.begin() + 9);
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 2.赋值操作 fill **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//fill把数值val赋值到由迭代器first及last所规定的范围内的所有元素
//fill_n把count个元素赋值
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "把v1中的后5个元素赋值为100" << endl;
fill(v1.begin() + 5, v1.end(), 100);
//fill(v1.end() - 5, v1.end(), 100);
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "把v2中的前6个元素赋值为99" << endl;
fill_n(v2.begin(), 6, 99);
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
//generate对每一个元素调用_Gen函数,并把函数的结果复制到容器的元素中.
deque<int> deq1(5,1); //初始化大小为5,值为1;
copy(deq1.begin(), deq1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "通过generate调用rand随机函数赋值后:" ;
generate(deq1.begin(), deq1.end(), rand);
cout << endl;
copy(deq1.begin(), deq1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "前3个元素赋值为2" << endl;
generate_n(deq1.begin(), 5, settwo);
cout << endl;
copy(deq1.begin(), deq1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 3.容器拆分技术 随机排列元素 partition **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//partition()算法将一个序列分成两个部分,其中第一个部分被谓词_Comp作用后返回true值的元素,第二部分是返回false的元素
//这个函数返回一个指向两个部分分界点的迭代器
//random_shuffle()算法在first及last所规定范围内随机排列元素
v1.clear();
for (int i = 0; i < 10; i++ )
{
v1.push_back(i);
}
cout << "原v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "打乱顺序v1:";
random_shuffle(v1.begin(), v1.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "根据是否大于5分组后的v1:";
it1 = partition(v1.begin(), v1.end(), great5 );
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout <<"分组后指向分界点的元素为: "<< *it1 << endl;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 4.元素删除 remove **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//remove在first及last所规定的范围内删除所有等于_Val的元素,返回新的迭代器结束点。
//删除后若大小不改变,或输出的结束位置仍和原来一样,则会发现后面原来位置的元素没有变,帮要重新设置大小
for (int i = 0; i < 3; i++ )
{
v1.push_back(7);
}
random_shuffle(v1.begin(), v1.end());
cout << " vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
vector<int>::iterator new_end;
new_end = remove(v1.begin(), v1.end(), 7);
cout << "remove 7后 v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << " resize 后: ";
v1.erase(new_end, v1.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 5.元素替换 replace **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
for (int i = 0; i < 3; i++ )
{
v1.push_back(7);
}
random_shuffle(v1.begin(), v1.end());
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "将v1中的7替换为700:" << endl;
replace(v1.begin(), v1.end(), 7, 700);
cout << "替换后vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
cout << lin;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 5.元素的旋转 rotate **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//rotate():把middle到last范围中的元素向左旋转到由first开始的范围中去。
v1.clear();
deq1.clear();
for (int i = -3; i <= 5; i++)
{
v1.push_back(i);
}
for ( int i= 0; i <= 5; i++)
{
deq1.push_back(i);
}
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "deque deq1: ";
copy(deq1.begin(), deq1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
rotate(v1.begin(), v1.begin() + 3, v1.end());
cout << "旋转后v1:";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
int iii = 1;
while ( iii <= deq1.end() - deq1.begin() )
{
rotate(deq1.begin(), deq1.begin() + 1, deq1.end());
cout << "After the rotation of a single deque element to the back, \n deq1 is ( " ;
copy(deq1.begin(), deq1.end(), ostream_iterator<int>(cout, " "));
cout << " )." << endl;
iii++;
}
cout << endl;
cout << lin;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 6.元素的颠倒 reverse **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//reverse把双向迭代器first和last所规定范围中的元素颠倒排列。
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
reverse(v1.begin(), v1.end());
cout << "颠倒后 v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
cout << lin;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 6.元素的交换 swap swap_ranges **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//swap交换所有的容器元素
//swap_ranges把first和last规定范围中的元素以及由first2所开始的等长范围中的元素进行互相交换。
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "deque deq1: ";
copy(deq1.begin(), deq1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v1与v2交换后:" << endl;
swap(v1,v2);
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
v2.erase(v2.end() - 4, v2.end());
cout << "*(v2.end()-2):的元素是" << *(v2.end()-2) << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "v2全部元素-4后与deq1相同数量的元素交换后:" << endl;
swap_ranges(v2.begin(), v2.end(), deq1.begin() );
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "deque deq1: ";
copy(deq1.begin(), deq1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
cout << lin;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 7.容器运算技术 transform **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//transform对first和last所规定范围中的元素调用函数_Func,并把运算的结果存储在_Result所开始的等长范围内 .
v1.clear();
v2.clear();
v3.clear();
v2.resize(6); //用transform给v2赋值时注意设置v2的大小
v3.resize(6);
for (int i = -4; i < 2; i++)
{
v1.push_back(i);
}
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v3: ";
copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "把v1中的元素都乘2." << endl ;
transform(v1.begin(), v1.end(), v1.begin(), MultValue<int>(2));
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "把v1中的元素*5后放v2中.其实v1中的元素也已经被改变." << endl;
cout << "v2.size(): " << v2.size() << endl;
transform(v1.begin(), v1.end(), v2.begin(), MultValue<int>(5));
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "把v1中的元素乘v2中的元素放v3中." << endl ;
transform(v1.begin(), v1.end(), v2.begin(),v3.begin(), multiplies<int>());
cout << "vector v3: ";
copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << endl;
cout << lin;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 8.删除容器中的重复元素 unique **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
//unique删除first和last范围中所有重复的相邻元素
v1.clear();
for (int i = 0; i <= 3; i++)
{
v1.push_back(5);
v1.push_back(-5);
}
for (int i = 0; i <= 3; i++)
{
v1.push_back(4);
}
v1.push_back(7);
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
vector<int>::iterator it_new_end1 ;
it_new_end1 = unique(v1.begin(), v1.end());
cout << "去掉相邻重复元素 v1: ";
copy(v1.begin(),it_new_end1, ostream_iterator<int>(cout, " "));//结束位置为it_new_end1
cout << endl;
it_new_end1 = unique(v1.begin(),it_new_end1, mod_equal);
cout << "按绝对值去掉相邻重复元素 v1: ";
copy(v1.begin(),it_new_end1, ostream_iterator<int>(cout, " "));//结束位置为it_new_end1
cout << endl;
it_new_end1 = unique(v1.begin(),it_new_end1, greater<int>());
cout << "按大于比较确保相邻元素按递增排列 v1: ";
copy(v1.begin(),it_new_end1, ostream_iterator<int>(cout, " "));//结束位置为it_new_end1
cout << endl;
cout << endl;
cout << " ***********************************************************" << endl;
cout << " ** 排序算法 **" << endl;
cout << " ***********************************************************" << endl;
cout << endl;
cout << " ******************************" << endl;
cout << " ** **" << endl;
cout << " ** 1.排序 sort stable_sort() partial_sort() **" << endl;
cout << " ** **" << endl;
cout << " ******************************" << endl;
v1.clear();
v2.clear();
l1.clear();
for (int i = 0; i <= 5; i++)
{
v1.push_back( 2 * i);
}
for (int i=0; i <= 5; i++)
{
v1.push_back(2 * i + 1);
}
l1.push_back(60);
l1.push_back(50);
l1.push_back(40);
l1.push_back(30);
l1.push_back(20);
l1.push_back(10);
for (int i=0; i <= 9; i++)
{
v2.push_back( i );
}
cout << "vector v1: ";
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "vector v2: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "list l1: ";
copy(l1.begin(), l1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "排序后v1: " ;
sort (v1.begin(), v1.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "greater排序后v1: ";
sort(v1.begin(), v1.end(), greater<int>());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "DEGreater排序后v1: ";
sort(v1.begin(), v1.end(), DEGreater);
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "打乱排序后v1: ";
random_shuffle(v1.begin(), v1.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "partital_sort对前四个数字排序后v1: ";
partial_sort(v1.begin(), v1.begin() + 4, v1.end(), DEGreater);
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "partital_sort_copy l1 into v2: ";
it1 = partial_sort_copy(l1.begin(), l1.end(),v2.begin(), v2.begin() + 6,greater<int>());
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
cout << "The first v2 element one position beyond" << "\n the last l1 element inserted was "<< *it1 << endl;
}
复制代码
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3