copy ,abs,includes 3个函数

copy:

 Copy函数原型:标头: <algorithm>

  OutputIterator copy(

InputIterator begin,

InputIterator end,

outputIterator Result)

返回的结果为:OutputIterator容器的ResultResult+end-begin)位置被InputIterator 容器beginend位置的数据覆盖。[begin,end)

#include <vector>

#include <algorithm>

#include <iostream>

 

using  namespace std;

 

void main()

{

     vector<int> vec1,vec2;  

 

     for(int i=0;i<=5;++i)

     {

         vec1.push_back(i*2);

     }

 

     for(int i=0;i<=10;++i)

     {

         vec2.push_back(i*3);

     }

 

     cout<<"vec1 数据项依次为:";

     for(vector<int>::iterator iter=vec1.begin();iter!=vec1.end();iter++)

     {

        cout<<*iter<<"   ";

     }

 

    cout<<endl<<"vec2的数据项依次为:";

     for(vector<int>::iterator iter=vec2.begin();iter!=vec2.end();iter++)

     {

         cout<<*iter<< ";

     }

 

     //copy,更新vec2的数据,

     copy(vec1.begin(),vec1.end(),vec2.begin());

    cout<<endl<<"更新后的vec2:";

     for(vector<int>::iterator iter=vec2.begin();iter!=vec2.end();iter++)

     {

         cout<<*iter<<"   ";

     }

     system("pause");

}

 

abs: 数据的绝对值

#include <iostream>

#include <valarray>

 

using namespace std;

const int  nums=10;

 

void main()

{

     //集合应用

      valarray<int> val_old(nums);

     for(int i=0;i<nums;++i)

     {

         val_old[i]=-i;

     }

 

     cout<<"the size of val_old  is :"<<val_old.size()<<endl;

   

     cout<<"before abs list is: ";

     for(int i=0;i<nums;++i)

     {

         cout<<val_old[i];

         if(i<nums-1)

         {

              cout<<",";

         }

     }

 

     valarray<int> val_new=abs(val_old); //将整合集合对象传给方法,每个元素都变

     cout<<endl<<"after abs:";

 

     for(int i=0;i<nums;++i)

     {

         cout<<val_new[i];

         if(i<nums-1)

         {

              cout<<",";

         }

     }

 

     //单个对象。

     double d=-9.909;

     double abs_d=abs(d);

     cout<<endl<<abs_d+d;

     system("pause");

}

 

includes:标头: <algorithm>

函数原型:bool includes(

      InputIterator1 First1,

      InputIterator1 Last1,

      InputIterator2 First2,

      InputIterator2 Last2

   )
集合序列与集合序列之间的包含与否!

#include <iostream>

#include <algorithm>

#include <functional>

#include <string>

#include <deque>

 

using namespace std;

 

 

int main()

{

     deque<string>::size_type nums=5;

    deque<string> strs_big(nums);

     deque<string> strs_small(3);

 

     strs_big.push_back("one");

     strs_big.push_back("two");

     strs_big.push_back("three");

     strs_big.push_back("four");

     strs_big.push_back("");

 

     strs_small.push_back("");

     strs_small.push_back("two");

     strs_small.push_back("four");

 

     sort(strs_big.begin(),strs_big.end());

     sort(strs_small.begin(),strs_small.end());

 

     cout<<endl<<"较大串的数据元素有:";

     for(deque<string>::iterator ite=strs_big.begin();ite!=strs_big.end();++ite)

     {

         cout<<*ite<<";

     }

   

     cout<<endl<<"较小串的数据元素有:";

     for(deque<string>::iterator it=strs_small.begin();it!=strs_small.end();++it)

     {

         cout<<*it<<";

     }

 

     if(includes(strs_big.begin(),strs_big.end(),strs_small.begin(),strs_small.end()))

     {

         cout<<endl<<"大串包含小串!Y";

     }

     else

     {

         cout<<endl<<"大串不包含小串!N";

     }

     system("pause");

}

 

posted @ 2013-06-17 17:51  hometown  阅读(160)  评论(0编辑  收藏  举报