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;
}

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