C++集锦

转自【http://blog.csdn.net/jerryjbiao/article/details/6827508】
感觉写的不错,基本测试了一遍,以后代码中可以借鉴这些用法。
1  for_each用于逐个遍历容器元素,它对迭代器区间[first,last)所指的每一个元素,执行由单参数函数对象f所定义的操作。for_each 算法范围 [first, last) 中的每个元素调用函数 F,并返回输入的参数 f。此函数不会修改序列中的任何元素。
template<class InputIterator, class Function>
   Function for_each(
      InputIterator _First, 
      InputIterator _Last, 
      Function _Func
      );
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
//print为仿函数
struct print{
int count;
print(){count = 0;}
void operator()(int x)
{
cout << x << endl;
++count;
}
};
int main(void)
{
list<int> ilist;
//初始化
for ( size_t i = 1; i < 10; ++i)
{
ilist.push_back(i);
}
//遍历ilist元素并打印
print p = for_each(ilist.begin(), ilist.end(), print());
//打印ilist元素个数
cout << p.count << endl;
return 0;
}
 仿函数,又或叫做函数对象,是STL(标准模板库)六大组件(容器、配置器、迭代器、算法、配接器、仿函数)之一;仿函数虽然小,但却极大的拓展了算法的功能,几乎所有的算法都有仿函数版本。例如,查找算法find_if就是对find算法的扩展,标准的查找是两个元素向等就找到了,但是什么是相等在不同情况下却需要不同的定义,如地址相等,地址和邮编都相等,虽然这些相等的定义在变,但算法本身却不需要改变,这都多亏了仿函数。 仿函数之所以叫做函数对象,是因为仿函数都是定义了()函数运算操作符的类。
2 find算法用于查找等于某值的元素。它在迭代器区间[first , last)上查找等于value值的元素,如果迭代器iter所指的元素满足 *iter == value ,则返回迭代器iter,未找则返回last。
template<class InputIterator, class Type>
   InputIterator find(
      InputIterator _First, 
      InputIterator _Last, 
      const Type& _Val
   );
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> ilist;
for (size_t i = 0; i < 10; ++i)
{
ilist.push_back(i+1);
}
ilist.push_back(10);
list<int>::iterator iLocation = find(ilist.begin(), ilist.end(), 10);
if (iLocation != ilist.end())
{
cout << "找到元素 10" << endl;
}
cout << "前一个元素为:" << *(--iLocation) << endl;
return 0;
}
3  find_if算法 是find的一个谓词判断版本,它利用返回布尔值的谓词判断pred,检查迭代器区间[first, last)上的每一个元素,如果迭代器iter满足pred(*iter) == true,表示找到元素并返回迭代器值iter;未找到元素,则返回last。
template<class InputIterator, class Predicate>
   InputIterator find_if(
      InputIterator _First, 
      InputIterator _Last, 
      Predicate _Pred
   );
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
//谓词判断函数 divbyfive : 判断x是否能5整除
bool divbyfive(int x)
{
return x % 5 ? 0 : 1;
}
int main()
{
//初始vector
vector<int> iVect(20);
for(size_t i = 0; i < iVect.size(); ++i)
{
iVect[i] = (i+1) * (i+3);
}
vector<int>::iterator iLocation;
iLocation = find_if(iVect.begin(), iVect.end(), divbyfive);
if (iLocation != iVect.end())
{
cout << "第一个能被5整除的元素为:"
<< *iLocation << endl//打印元素:15 
<< "元素的索引位置为:"
<< iLocation - iVect.begin() << endl;  //打印索引位置:2
}
return 0;
}
4 adjacent_find算法用于查找相等或满足条件的邻近元素对。其有两种函数原型:一种在迭代器区间[first , last)上查找两个连续的元素相等时,返回元素对中第一个元素的迭代器位置。另一种是使用二元谓词判断binary_pred,查找迭代器区间[first , last)上满足binary_pred条件的邻近元素对,未找到则返回last。
<strong>template<class ForwardIterator>
   ForwardIterator adjacent_find(
      ForwardIterator _First, 
      ForwardIterator _Last
      );
template<class ForwardIterator , class BinaryPredicate>
   ForwardIterator adjacent_find(
      ForwardIterator _First, 
      ForwardIterator _Last, 
            BinaryPredicate _Comp
   );
#include <algorithm>
#include <list>
#include <iostream>




using namespace std;




//判断X和y是否奇偶同性
bool parity_equal(int x, int y)
{
return (x - y) % 2 == 0 ? 1 : 0;
}
int main()
{
//初始化链表
list<int> iList;
iList.push_back(3);
iList.push_back(6);
iList.push_back(9);
iList.push_back(11);
iList.push_back(11);
iList.push_back(18);
iList.push_back(20);
iList.push_back(20);
//输出链表
list<int>::iterator iter;
for(iter = iList.begin(); iter != iList.end(); ++iter)
{
cout << *iter << "  ";
}
cout << endl;
//查找邻接相等的元素
list<int>::iterator iResult = adjacent_find(iList.begin(), iList.end());
if (iResult != iList.end())
{
cout << "链表中第一对相等的邻近元素为:" << endl;
cout << *iResult++ << endl;
cout << *iResult << endl;
}
//查找奇偶性相同的邻近元素
iResult = adjacent_find(iList.begin(), iList.end(), parity_equal);
if (iResult != iList.end())
{
cout << "链表中第一对奇偶相同的元素为:" << endl;
cout << *iResult++ << endl;
cout << *iResult << endl;
}
return 0;
}
5   find_first_of算法用于查找位于某个范围之内的元素。它有两个使用原型,均在迭代器区间[first1, last1)上查找元素*i,使得迭代器区间[first2, last2)有某个元素*j,满足*i ==*j或满足二元谓词函数comp(*i, *j)==true的条件。元素找到则返回迭代器i,否则返回last1。
template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
   );
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2,
      BinaryPredicate _Comp
   );
 #include <algorithm>
#include <iostream>
using namespace std;


int main()
{
const char* strOne = "abcdef1212daxs";
const char* strTwo = "2ef";
const char* result = find_first_of(strOne, strOne + strlen(strOne),
strTwo, strTwo + strlen(strTwo));
cout << "字符串strOne中第一个出现在strTwo的字符为:"
<< *result << endl;
return 0;
}
6     count算法用于计算容器中的某个给定值的出现次数。它有两个使用原型,均计算迭代器区间[first, last)上等于value值的元素个数n,区别在于计数n是直接返回还是引用返回。
template<class InputIterator, class Type>
   typename iterator_traits<InputIterator>::difference_type count(
      InputIterator _First, 
      InputIterator _Last, 
      const Type& _Val
   );
#pragma warning(disable:4786)
#include <iostream>
#include <algorithm>
#include <list>
#include <string>
#include <vector>
using namespace std;
int main()
{
list<int> ilist;
for (list<int>::size_type index = 0; index < 100; ++index)
{
ilist.push_back( index % 20 );
}
list<int>::difference_type num = 0;
int value = 9;
num = count(ilist.begin(), ilist.end(), value);
cout << "链表中元素等于value的元素的个数:"
<< num << endl;
vector<string> vecString;
vecString.push_back("this");
vecString.push_back("is");
vecString.push_back("a");
vecString.push_back("test");
vecString.push_back("program");
vecString.push_back("is");
string valString("is");
ptrdiff_t result = count(vecString.begin(), vecString.end(), valString);
cout << "容器中元素为is的元素个数:"
<< result << endl;
return 0;
}
7   count_if算法是使用谓词判断pred统计迭代器区间[first , last) 上满足条件的元素个数n,按计数n是否引用返回,有如下两种函数原型:
template<class InputIterator, class Predicate>
   typename iterator_traits<InputIterator>::difference_type count_if(
      InputIterator _First, 
      InputIterator _Last,
      Predicate _Pred
   );
 template<class InputIterator, class T> inline
   size_t count(
      InputIterator First,
      InputIterator Last,
      const T& Value
   )
 #pragma warning(disable:4786)
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
//学生记录结构体
struct stuRecord{
struct stuInfo{
char* name;
int year;
char* addr;
};
int id; //学号
stuInfo m_stuInfo; //学生信息
stuRecord(int m_id, char* m_name, int m_year, char* m_addr)
{
id = m_id;
m_stuInfo.name = m_name;
m_stuInfo.year = m_year;
m_stuInfo.addr = m_addr;
}
};


typedef stuRecord::stuInfo stuRI;
bool setRange( pair<int, stuRI> s )
{
if (s.second.year > 20 && s.second.year < 30)
{
return true;
}
return false;
}
int main()
{
//学生数据
stuRecord stu1 = stuRecord(1, "张三", 21, "北京");
stuRecord stu2 = stuRecord(2, "李四", 29, "上海");
stuRecord stu3 = stuRecord(3, "王五", 12, "深圳");
stuRecord stu4 = stuRecord(4, "赵六", 25, "长沙");
stuRecord stu5 = stuRecord(5, "孙七", 30, "广东");

//插入学生记录
map<int, stuRI> m;
m.insert(make_pair(stu1.id, stu1.m_stuInfo));
m.insert(make_pair(stu2.id, stu2.m_stuInfo));
m.insert(make_pair(stu3.id, stu3.m_stuInfo));
m.insert(make_pair(stu4.id, stu4.m_stuInfo));
m.insert(make_pair(stu5.id, stu5.m_stuInfo));
//条件统计
int num = count_if(m.begin(), m.end(), setRange);
cout << "学生中年龄介于20至30之间的学生人数为:"
<< num << endl;
return 0;
}
8  mismatch算法是比较两个序列,找出首个不匹配元素的位置。它有如下两个函数原型,找出迭代器区间[first1, last1) 上第一个元素 *i , 它和迭代器区间[first2, first2 + (last1 - first1))上的元素* (first2 + (i - first1))不相等(或者不满足二元谓词binary_pred条件)。通过匹配对象pair返回这两个元素的迭代器,指示不匹配元素位置。
template<class InputIterator1, class InputIterator2>
   pair<InputIterator1, InputIterator2> mismatch(
      InputIterator1 _First1, 
      InputIterator1 _Last1,
      InputIterator2 _First2
    );
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
   pair<InputIterator1, InputIterator2> mismatch(
      InputIterator1 _First1, 
      InputIterator1 _Last1,
      InputIterator2 _First2
      BinaryPredicate _Comp
   );
 #include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

bool strEqual(const char* s1, const char* s2)
{
return strcmp(s1, s2) == 0 ? true : false;
}
typedef vector<int>::iterator ivecIter;
int main()
{
vector<int> ivec1, ivec2;
ivec1.push_back(2);
ivec1.push_back(0);
ivec1.push_back(1);
ivec1.push_back(4);


ivec2.push_back(2);
ivec2.push_back(0);
ivec2.push_back(1);
ivec2.push_back(4);
ivec2.push_back(5);
pair<ivecIter, ivecIter> retCode;
retCode = mismatch(ivec1.begin(), ivec1.end(), ivec2.begin());
if (retCode.first == ivec1.end() && retCode.second == ivec2.begin() +ivec1.size() )
{
cout << "ivec1 和 ivec2完全相同" << endl;

else
{
cout << "ivec1 和 ivec2 不相同,不匹配的元素为:\n"
<< *retCode.first << endl
<< *retCode.second << endl;
}
    char* str1[] = {"appple", "pear", "watermelon", "banana", "grape"};
char* str2[] = {"appple", "pears", "watermelons", "banana", "grape"};
pair<char**, char**> retCode2 = mismatch(str1, str1+5, str2, strEqual);
if (retCode2.first == str1+5 && retCode2.second == str2+5)
{
cout << "str1 和 str2 完全相同" << endl;

else
{
cout << "str1 和 str2  不相同,不匹配的字符串为:" << endl
<< str1[retCode2.first - str1] << endl
<< str2[retCode2.second - str2] << endl;
}

return 0;
}
//已做修改
9 equal算法类似于mismatch,equal算法也是逐一比较两个序列的元素是否相等,只是equal函数的返回值为bool值true/false,不是返回迭代器值。它有如下两个原型,如果迭代器区间[first1,last1)和迭代器区间[first2, first2+(last1 - first1))上的元素相等(或者满足二元谓词判断条件binary_pred) ,返回true,否则返回false。
template<class InputIterator1, class InputIterator2>
   bool equal(
      InputIterator1 _First1, 
      InputIterator1 _Last1, 
      InputIterator2 _First2
      );
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
   bool equal(
      InputIterator1 _First1, 
      InputIterator1 _Last1, 
      InputIterator2 _First2, 
      BinaryPredicate _Comp
      );

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

bool absEqual(int a, int b)
{
return (a == abs(b) || b == abs(a)) ? true : false;
}

int main()
{
vector<int> ivect1(5);
vector<int> ivect2(5);
for (vector<int>::size_type i = 0; i < ivect1.size(); ++i)
{
ivect1[i] = i;
ivect2[i] = (-1) * i;
}
if ( equal( ivect1.begin(), ivect1.end(), ivect2.begin(), absEqual ) )
{
cout << "ivect1 和 ivect2 元素的绝对值完全相等" << endl;

else
{
cout << "ivect1 和 ivect2 元素的绝对值不完全相等" << endl;
}
return 0;
}
10   search算法函数在一个序列中搜索与另一序列匹配的子序列。它有如下两个原型,在迭代器区间[first1, last1)上找迭代器区间[first2, last2)完全匹配(或者满足二元谓词binary_pred)子序列,返回子序列的首个元素在[first1, last1)区间的迭代器值,或返回last1表示没有匹配的子序列。
template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 search(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
   );
template<class ForwardIterator1, class ForwardIterator2, class Pr>
   ForwardIterator1 search(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
      BinaryPredicate _Comp
   );
 
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;


int main()
{
vector<int> v1;
v1.push_back(5);
v1.push_back(8);
v1.push_back(1);
v1.push_back(4);


vector<int> v2;
v2.push_back(8);
v2.push_back(1);


vector<int>::iterator iterLocation;
iterLocation = search(v1.begin(), v1.end(), v2.begin(), v2.end());
if (iterLocation != v1.end())
{
cout << "v2的元素包含在v1容器中,起始元素为"
<< "v1[" << iterLocation - v1.begin() << "]" <<endl;

else
{
cout << "v2的元素不包含在v1容器" << endl;
}
return 0;
}
11   重复元素子序列搜索search_n算法:搜索序列中是否有一系列元素值均为某个给定值的子序列,它有如下两个函数原型,分别在迭代器区间[first, last)上搜索是否有count个连续元素,其值均等于value(或者满足谓词判断binary_pred的条件),返回子序列首元素的迭代器,或last以表示没有重复元素的子序列。
template<class ForwardIterator1, class Diff2, class Type>
   ForwardIterator1 search_n(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      Size2 _Count, 
      const Type& _Val
   );
template<class ForwardIterator1, class Size2, class Type, class BinaryPredicate>
   ForwardIterator1 search_n(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      Size2 _Count, 
      const Type& _Val,
      BinaryPredicate _Comp
   );
#include <algorithm>
#include <vector>
#include <iostream>


bool twice(const int para1, const int para2)
{
return 2 * para1 == para2;
}


using namespace std;


int main()
{
vector<int> ivect;
ivect.push_back(1);
ivect.push_back(8);
ivect.push_back(8);
ivect.push_back(8);
ivect.push_back(4);
ivect.push_back(4);
ivect.push_back(3);


vector<int>::iterator iLocation;
iLocation = search_n(ivect.begin(), ivect.end(), 3, 8);
if (iLocation != ivect.end())
{
cout << "在ivect中找到3个连续的元素8" << endl;
}
else
{
cout << "在ivect中没有3个连续的元素8" << endl;
}


iLocation = search_n(ivect.begin(), ivect.end(), 4, 8);
if (iLocation != ivect.end())
{
cout << "在ivect中找到4个连续的元素8" << endl;
}
else
{
cout << "在ivect中没有4个连续的元素8" << endl;
}


iLocation = search_n(ivect.begin(), ivect.end(), 2, 4);
if (iLocation != ivect.end())
{
cout << "在ivect中找到2个连续的元素4" << endl;
}
else
{
cout << "在ivect中没有2个连续的元素4" << endl;
}




iLocation = search_n(ivect.begin(), ivect.end(), 3, 16, twice);
if (iLocation != ivect.end())
{
cout << "在ivect中找到3个连续元素的两倍为16" << endl;
}
else
{
cout << "在ivect中没有3个连续元素的两倍为16" << endl;
}
return 0;
}
//思考:是否有支持多元判断的?如三个元素只之和为16
12    find_end算法在一个序列中搜索出最后一个与另一序列匹配的子序列。有如下两个函数原型,在迭代器区间[first1, last1)中搜索出与迭代器区间[first2, last2)元素匹配的子序列,返回首元素的迭代器或last1。
template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_end(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
   );
template<class ForwardIterator1, class ForwardIterator2, class Pr>
   ForwardIterator1 find_end(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2,
      BinaryPredicate _Comp
   );
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;


int main()
{
vector<int> v1;
v1.push_back(5);
v1.push_back(-2);
v1.push_back(4);
v1.push_back(3);
v1.push_back(-2);
v1.push_back(4);
v1.push_back(8);
v1.push_back(-2);
v1.push_back(4);
v1.push_back(9);


vector<int>::const_iterator iter;
cout << "v1: " ;
for (iter = v1.begin(); iter != v1.end(); ++iter)
{
cout << *iter << "  ";
}
cout << endl;


vector<int> v2;
v2.push_back(-2);
v2.push_back(4);


cout << "v2: " ;
for (iter = v2.begin(); iter != v2.end(); ++iter)
{
cout << *iter << "  ";
}
cout << endl;
vector<int>::iterator iLoaction;
iLoaction = find_end(v1.begin(), v1.end(), v2.begin(), v2.end());
if (iLoaction != v1.end())
{
cout << "v1中找到最后一个匹配V2的子序列,起始位置在:"
<< "v1[" << iLoaction - v1.begin() << "]" << endl;
}
return 0;
}
 13 copy
  前面十二个算法所展现的都属于非变易算法(Non-mutating algorithms)系列,现在我们来看看变易算法。所谓变易算法(Mutating algorithms)就是一组能够修改容器元素数据的模板函数,可进行序列数据的复制,变换等。
       我们现在来看看第一个变易算法:元素复制算法copy。该算法主要用于容器之间元素的拷贝,即将迭代器区间[first,last)的元素复制到由复制目标result给定的区间[result,result+(last-first))中。下面我们来看看它的函数原型:
template<class InputIterator, class OutputIterator>
   OutputIterator copy(
      InputIterator _First, 
      InputIterator _Last, 
      OutputIterator _DestBeg
   );
参数
_First, _Last
指出被复制的元素的区间范围[ _First,_Last).
_DestBeg 
指出复制到的目标区间起始位置
返回值
返回一个迭代器,指出已被复制元素区间的最后一个位置
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () 
{
int myints[] = {10, 20, 30, 40, 50, 60, 70};
vector<int> myvector;
vector<int>::iterator it;
myvector.resize(7);   // 为容器myvector分配空间
//copy用法一:
//将数组myints中的七个元素复制到myvector容器中
copy ( myints, myints+7, myvector.begin() );

cout << "myvector contains: ";
for ( it = myvector.begin();  it != myvector.end();  ++it )
{
cout << " " << *it;
}
cout << endl;
//copy用法二:
//将数组myints中的元素向左移动一位
copy(myints + 1, myints + 7, myints);
cout << "myints contains: ";
for ( size_t i = 0; i < 7; ++i )
{
cout << " " << myints[i];
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>
using namespace std;
int main () 
{
typedef vector<int> IntVector;
typedef istream_iterator<int> IstreamItr;
typedef ostream_iterator<int> OstreamItr;
typedef back_insert_iterator< IntVector > BackInsItr;
 
IntVector myvector;
// 从标准输入设备读入整数
// 直到输入的是非整型数据为止 请输入整数序列,按任意非数字键并回车结束输入
cout << "Please input element:" << endl;
copy(IstreamItr(cin), IstreamItr(), BackInsItr(myvector));
//输出容器里的所有元素,元素之间用空格隔开
cout << "Output : " << endl;
copy(myvector.begin(), myvector.end(), OstreamItr(cout, " ")); 
cout << endl;
return 0;
}
14   前文中展示了copy的魅力,现在我们来看一下它的孪生兄弟copy_backward,copy_backward算法与copy在行为方面相似,只不过它的复制过程与copy背道而驰,其复制过程是从最后的元素开始复制,直到首元素复制出来。也就是说,复制操作是从last-1开始,直到first结束。这些元素也被从后向前复制到目标容器中,从result-1开始,一直复制last-first个元素。举个简单的例子:已知vector {0, 1, 2, 3, 4, 5},现我们需要把最后三个元素(3, 4, 5)复制到前面三个(0, 1, 2)位置中,那我们可以这样设置:将first设置值3的位置,将last设置为5的下一个位置,而result设置为0的位置,这样,就会先将值5复制到2的位置,然后4复制到1的位置,最后3复制到0的位置,得到我们所要的序列{3, 4, 5, 3, 4, 5}。下面我们来看一下copy_backward的函数原型:
 template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                         BidirectionalIterator1 last,
                                         BidirectionalIterator2 result); 
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector;
vector<int>::iterator iter;
//为容器myvector赋初始值:10 20 30 40 50
for ( int i = 1; i <= 5; ++i )
{
myvector.push_back( i*10 );
}
//将myvector容器的大小增加3个单元
myvector.resize( myvector.size()+3 );
//将容器元素20、10拷贝到第八、第七个单元中:10 20 30 40 50 0 10 20
//注意copy_backward是反向复制,先将20拷贝到第八个单元,再将10拷贝到第七个单元
copy_backward( myvector.begin(), myvector.begin()+2, myvector.end() );
for ( iter = myvector.begin(); iter != myvector.end(); ++iter )
{
cout << " " << *iter;
}
cout << endl;
//清除myvector容器
myvector.clear();
//还原容器myvector的初始值:10 20 30 40 50
for (int i = 1; i <= 5; ++i )
{
myvector.push_back( i*10 );
}
//将容器元素40、50覆盖10、20, 即:40 50 30 40 50:
copy_backward( myvector.end()-2, myvector.end(), myvector.end()-3 );

for ( iter = myvector.begin(); iter != myvector.end(); ++iter )
{
cout << " " << *iter;
}
cout << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>
using namespace std;
class output_element
{
public:
//重载运算符()
void operator() (string element)
{
cout << element
<< ( _line_cnt++ % 7 ? " " : "\n\t"); //格式化输出,即每7个换行和制表位
}
static void reset_line_cnt() 
{
_line_cnt = 1;
}
private:
static int _line_cnt;
};
int output_element::_line_cnt = 1; //定义并初始静态数据成员
int main()
{
string sa[] = {
"The", "light", "untonusred", "hair",
"grained", "and", "hued", "like", "pale", "oak"
};
vector<string> svec(sa, sa+10);

//还记得for_each吧,呵呵,这里用它来作为输出
//for_each具体用法参考 http://blog.csdn.net/jerryjbiao/article/details/6827508
cout << "Original list of strings:\n\t";
for_each( svec.begin(), svec.end(), output_element() );
cout << "\n" << endl;
//将"The", "light", "untonusred", "hair","grained", 
//"and", "hued"后移三个单元覆盖了"like", "pale", "oak"
copy_backward(svec.begin(), svec.end()-3, svec.end());
output_element::reset_line_cnt();
cout << "sequence after "
<< "copy_backward(svec.begin(), svec.end()-3, svec.end()): \n\t";
for_each( svec.begin(), svec.end(), output_element() );
cout << "\n" << endl;
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章