STL algorithm

// algorithm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

int arr[] = {1,3,5,4,7,6,0,9,8,2};
int arr2[] = {1,1,5,2,7,3,10,13,18,13};
class Less9 {
public: 
    bool operator () (int value) { 
       return value < 9;
    }
};
class Greater10{
public: 
    bool operator () (int value) {
       return value >= 10;
    }
};
bool greater10(int value)
 {
     return value > 10;
 }

int main(int argc, char* argv[])
{
    vector<int> v1(arr,arr+10);
    vector<int>::const_iterator it;
    for(it = v1.begin();it != v1.end();++it){//直接输出
        cout<<*it<<" ";
    }
    cout<<endl<<"---------------------------"<<endl;
    sort(v1.begin(),v1.end());//默认排序:从小到大
    for(it = v1.begin();it != v1.end();++it){
        cout<<*it<<" ";
    }
    cout<<endl;
    sort(v1.begin(),v1.end(),greater<int>());//从大到小排序。less greater  
    for(it = v1.begin();it != v1.end();++it){
        cout<<*it<<" ";
    }
    cout<<endl;
    vector<int>::iterator fIt;
    fIt = find(v1.begin(),v1.end(),4);//查找为4的数
//  int *p = find(v1.begin(),v1.end(),7);
     if (fIt == v1.end()) {
       cout << "not found in vector" << endl;
    }
    else {
       cout<< *fIt <<endl;
    } 
    vector<int>::iterator EventIterator =find_if (v1.begin(), v1.end(), Less9());//自定义查找
    if (EventIterator==v1.end()) {
       cout <<"Event not found in list" << endl;
    }
   else{
       cout<<*EventIterator << endl;
    }
    fIt = adjacent_find(v1.begin(),v1.end());
    if (fIt == v1.end())    cout << "not found in vector" << endl;   
    else        cout<< *fIt <<endl;
    bool bFind = binary_search(v1.begin(),v1.end(),5);
    if (bFind)    cout << "binary_search not found in vector" << endl;   
    else cout << "binary_search found in vector" << endl; 
    cout<<"Count of 4:"<<count(v1.begin() , v1.end() , 4)<<endl;//查找元素为4的个数
    //
    vector<int> v2(arr2,arr2+10);
    vector<int>::size_type  result1 = count_if(v2.begin(), v2.end(), greater10);    
    cout<<result1<<endl;
    result1 = count_if(v2.begin(), v2.end(), Greater10());//查找元素大于10的个数
    cout<<result1<<endl;
    for(it = v2.begin();it != v2.end();++it){
        cout<<*it<<" ";
    }cout<<endl;
    reverse(v2.begin(), v2.end());//反向
    for(it = v2.begin();it != v2.end();++it){
        cout<<*it<<" ";
    }cout<<endl;
    
    copy (arr+2, arr + 4, v2.begin()); //copy(first_source, Last_source, dest) 
    for(it = v2.begin();it != v2.end();++it){
        cout<<*it<<" ";
    }cout<<endl;
    //
    for(it = v1.begin();it != v1.end();++it){
        cout<<*it<<" ";
    }cout<<endl;
  //  vector<int>::iterator ret = remove(v1.begin(), v1.end(), 2) ;  
    v1.erase(remove(v1.begin(),v1.end(),2), v1.end());
    //如果你真的想删除东西的话就在类似remove的算法后接上erase
    //v.erase(remove(v.begin(), v.end(), 99), v.end());     // 真的删除所有
   for(it = v1.begin();it != v1.end();++it){
        cout<<*it<<" ";
    }cout<<endl;
    v1.erase(remove_if(v1.begin(),v1.end(),Less9()), v1.end());//删除所有小于9的数
    for(it = v1.begin();it != v1.end();++it){
        cout<<*it<<" ";
    }cout<<endl;
    //
    int arr3[] = {2,3,1,345,123,56,23,888,67,100};
    vector<int> v3(arr3,arr3+10);
    // 移除所有小于100的元素
//  v3.erase( std::remove_if( v3.begin(),  v3.end(),std::bind2nd( std::less< int>(), 100)), v3.end());//std::bind1st( std::greater< int>()
    for(it = v3.begin();it != v3.end();++it){
        cout<<*it<<" ";
    }cout<<endl;cout<<"-----"<<endl;
    vector<int> v4(arr3,arr3+10);
    // 移除所有大于100的元素 
    v4.erase( std::remove_if( v4.begin(),  v4.end(),std::bind1st( std::less< int>(), 100)), v4.end());
    for(it = v4.begin();it != v4.end();++it){
        cout<<*it<<" ";
    }cout<<endl;cout<<"-----"<<endl;
    //这里的表达式相当于100 < arr.value
    //移除所有小于等于100的元素
    v3.erase( std::remove_if( v3.begin(),  v3.end(),std::not1(std::bind2nd( std::greater<int>(), 100))), v3.end());
    for(it = v3.begin();it != v3.end();++it){
        cout<<*it<<" ";
    }cout<<endl;
    return 0;
}

bool divbyfive(int x)  
{  
    return x % 5 ? 0 : 1;  
}  
// First.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#if 1
#include <vector>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <functional>  

using namespace std;
void f(int (&a)[5]);
int main(int argc, char* argv[])
{
    cout<<"test..."<<endl;
    int arr[] = {1,2,3,4,5};
    f(arr);
    getchar();
    return 0;
}
bool comp9(const int &a,const int &b)
{
    return a>b;
}

void f(int (&a)[5])  
{  
    a[1] = 100;
    for(int i = 0; i < 5; i++)  
        cout<<a[i]<<endl;
    cout<<"-----------------------"<<endl;
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(55);
    vec.push_back(3);
    sort(vec.begin(),vec.end());
//  sort(vec.begin(),vec.end(),comp9);
    sort(vec.begin(),vec.end(),greater<int>());//less
//bind1st( less<int>(), 10)(20);

    //const_iterator
    vector<int>::const_iterator it;
//  vector<int>::iterator it;
    for(it = vec.begin();it != vec.end();it++){
        cout<<*it<<endl;
    }
    cout<<"A---------------------------------"<<endl;
//  int i;    
vector<int> lv;
for(i = 0; i < 100; i++) 
{
    lv.push_back(i);
}
//对vector中小于20的数进行记数
cout << count_if(lv.begin(), lv.end(), bind2nd(less<int>(), 20)) << endl;
    cout<<"B---------------------------------"<<endl;
//由大到小排序
sort(lv.begin(), lv.end(), not2(less<int>()) ) ;
for (i = 0; i < 100; i++) 
{
    cout << lv.at(i) << endl;
}
} 
#else
#include <iostream>
#include <algorithm>

using namespace std;

#define SIZE 100
int iarray[SIZE];
 class Test{
public:
    int operator()(int x,int y){
        return x+y;
    }
};
template <class T>
T add(T x,T y)
{
    return x+y;
}
int main()
{
Test t;
cout<<t(10,12)<<endl;
//
  iarray[20] = 50;
  int* ip = find(iarray, iarray + SIZE, 50);
  if (ip == iarray + SIZE)
    cout << "50 not found in array" << endl;
  else
    cout << *ip << " found in array" << endl;
  ///
   cout<<add(1,2)<<endl;
cout<<add(1.12,2.12)<<endl;
  return 0;
}
#endif 
// Vector.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <deque>
using namespace std;
//begin(),end(),push_back(),assign(),front(),back(),erase(),empty(),at(),size()。
//insert,
#if 0
int ar[10] = {  12, 45, 234, 64, 12, 35, 63, 23, 12, 55  };
int main(int argc, char* argv[])
{
    vector<int> v1(ar, ar+10);
    vector<int> v2(10,1);
    v2.assign(10,4);
    vector<int>::const_iterator it;
    for(it=v2.begin();it!=v2.end();++it){
        cout<<*it<<endl;
    }
    cout<<v1.front()<<endl;//12
    cout<<v1.size()<<endl;//10
    cout<<v1.back()<<endl;//55
    v1.pop_back();
    cout<<v1.back()<<endl;//12  
    cout<<v1.at(3)<<"---"<<v1[3]<<endl;//64
    cout<<v1.empty()<<endl;//false
    v1.erase(v1.begin()+1,v1.end()-2);//删除成员
    for(it=v1.begin();it!=v1.end();++it){
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}

#else
void printDeque(deque<int> d)
{
//使用下标
//for (unsigned int i = 0; i < d.size(); i++)
//{
// cout<<"d["<<i<<"] = "<<d[i]<<", ";
//}

//使用迭代器
//deque<int>::iterator iter = d.begin();
//for (;iter != d.end(); iter ++)
//{
// cout<<"d["<<iter-d.begin()<<"] = "<<(*iter)<<", ";
//}

//使用迭代器指针
deque<int>::iterator *pIter = new deque<int>::iterator;
if ( NULL == pIter )
{
   return ;
}
for (*pIter = d.begin(); *pIter != d.end(); (*pIter)++)
{
   cout<<"d["<<*pIter - d.begin() <<"]="<<**pIter<<", ";
}
if (NULL != pIter)
{
   delete pIter;
   pIter = NULL;
}

cout<<endl;
}

int main()
{
    //创建deque
deque<int> d1; //创建一个没有任何元素的deque对象
deque<int> d2(10);//创建一个具有10个元素的deque对象,每个元素值为默认
deque<double> d3(10, 5.5); //伊妹一个具有10个元素的deque对象,每个元素的初始值为5.5
deque<double> d4(d3); //通过拷贝一个deque对象的元素值, 创建一个新的deque对象
 int iArray[] = {11, 13, 19, 23, 27};
deque< int> d5(5,12);//(iArray, iArray+5);//将迭代器区间[first, last)所指的元素拷贝到一个新创建的deque对象中

//初始化赋值:同vector一样,使用尾部插入函数push_back()
for (int i = 1; i < 6 ; i++)
   d1.push_back(i*10);
//遍历元素: 1-下标方式 2-迭代器方式 反向遍历(略)
cout<<"printDeque(d1) : "<<endl;
printDeque(d1);

//元素插入:尾部插入用push_back(),头部插入用push_front(),其它位置插入用insert(&pos, elem)
cout<<"d1.push_front(100): "<<endl;
d1.push_front(100);
printDeque(d1);
cout<<"d1.insert(d1.begin()+3, 200): "<<endl; //支持随机存取(即[]操作符),所以begin()可以+3
d1.insert(d1.begin()+2,200);

printDeque(d1);



//元素删除 尾部删除用pop_back();头部删除用pop_front(); 
//任意迭代位置或迭代区间上的元素删除用erase(&pos)/erase(&first, &last);删除所有元素用clear();
cout<<"d1.pop_front(): "<<endl;
d1.pop_front();
printDeque(d1);

cout<<"d1.erase(d1.begin()+1): "<<endl;
d1.erase(d1.begin()+1); //删除第2个元素d1[1]
printDeque(d1);

cout<<"d1.erase(d1.begin(), d1.begin() + 2) = "<<endl;
d1.erase(d1.begin(), d1.begin() + 2);
printDeque(d1);

cout<<"d1.clear() :"<<endl;
d1.clear();
printDeque(d1);


//其它常用
cout<<"其它常用用法: "<<endl;
int flag = 0;
while(flag < 2)
{
   if (0 == flag )
   {
    for (int i = 1; i < 6 ; i++) //恢复
     d1.push_back(i*10);
   }
   else
   {
    d1.clear();
    cout<<"after d1.clear() , d1.front(), d1.back() is abnormal! other info.:"<<endl;
   }
   cout<<"d1.empty() = "<<d1.empty()<<endl;
   cout<<"d1.size() = "<<d1.size()<<endl;
   cout<<"d1.max_size() = "<<hex<<d1.max_size()<<endl;
   if (!d1.empty())
   {
    cout<<"d1.front() = "<<d1.front()<<endl;
    cout<<"d1.back() = "<<d1.back()<<endl;
   }

   flag++;

}

    //交换
    cout<<"d1.swap(d5)= "<<endl;
    d1.swap(d5);
    cout<<"d1 = ";
    printDeque(d1);
    cout<<"d5 = ";
    printDeque(d5);
    //printDeque(d)


}
#endif

#if 0
C++  Lists用法
Lists将元素按顺序储存在链表中. 与向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.


assign() 给list赋值 
back() 返回最后一个元素 
begin() 返回指向第一个元素的迭代器 
clear() 删除所有元素 
empty() 如果list是空的则返回true
end() 返回末尾的迭代器 
erase() 删除一个元素 
front() 返回第一个元素 
get_allocator() 返回list的配置器 
insert() 插入一个元素到list中 
max_size() 返回list能容纳的最大元素数量 
merge() 合并两个list
pop_back() 删除最后一个元素 
pop_front() 删除第一个元素 
push_back() 在list的末尾添加一个元素 
push_front() 在list的头部添加一个元素 
rbegin() 返回指向第一个元素的逆向迭代器 
remove() 从list删除元素 
remove_if() 按指定条件删除元素 
rend() 指向list末尾的逆向迭代器 
resize() 改变list的大小 
reverse() 把list的元素倒转 
size() 返回list中的元素个数 
sort() 给list排序 
splice() 合并两个list
swap() 交换两个list
unique() 删除list中重复的元素

附List用法实例:

#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>

using namespace std;

//创建一个list容器的实例LISTINT
typedef list<int> LISTINT;

//创建一个list容器的实例LISTCHAR
typedef list<char> LISTCHAR;

void main(void)
{
    //--------------------------
    //用list容器处理整型数据
    //--------------------------
    //用LISTINT创建一个名为listOne的list对象
    LISTINT listOne;
    //声明i为迭代器
    LISTINT::iterator i;

    //从前面向listOne容器中添加数据
    listOne.push_front (2);
    listOne.push_front (1);

    //从后面向listOne容器中添加数据
    listOne.push_back (3);
    listOne.push_back (4);

    //从前向后显示listOne中的数据
    cout<<"listOne.begin()--- listOne.end():"<<endl;
    for (i = listOne.begin(); i != listOne.end(); ++i)
        cout << *i << " ";
    cout << endl;

    //从后向后显示listOne中的数据
LISTINT::reverse_iterator ir;
    cout<<"listOne.rbegin()---listOne.rend():"<<endl;
    for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
        cout << *ir << " ";
    }
    cout << endl;

    //使用STL的accumulate(累加)算法
    int result = accumulate(listOne.begin(), listOne.end(),0);
    cout<<"Sum="<<result<<endl;
    cout<<"------------------"<<endl;

    //--------------------------
    //用list容器处理字符型数据
    //--------------------------

    //用LISTCHAR创建一个名为listOne的list对象
    LISTCHAR listTwo;
    //声明i为迭代器
    LISTCHAR::iterator j;

    //从前面向listTwo容器中添加数据
    listTwo.push_front ('A');
    listTwo.push_front ('B');

    //从后面向listTwo容器中添加数据
    listTwo.push_back ('x');
    listTwo.push_back ('y');

    //从前向后显示listTwo中的数据
    cout<<"listTwo.begin()---listTwo.end():"<<endl;
    for (j = listTwo.begin(); j != listTwo.end(); ++j)
        cout << char(*j) << " ";
    cout << endl;

    //使用STL的max_element算法求listTwo中的最大元素并显示
    j=max_element(listTwo.begin(),listTwo.end());
    cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
}

#include <iostream>
#include <list>

using namespace std;
typedef list<int> INTLIST;

//从前向后显示list队列的全部元素
void put_list(INTLIST list, char *name)
{
    INTLIST::iterator plist;

    cout << "The contents of " << name << " : ";
    for(plist = list.begin(); plist != list.end(); plist++)
        cout << *plist << " ";
    cout<<endl;
}

//测试list容器的功能
void main(void)
{
    //list1对象初始为空
    INTLIST list1;  
    //list2对象最初有10个值为6的元素 
    INTLIST list2(10,6);
    //list3对象最初有3个值为6的元素 
    INTLIST list3(list2.begin(),--list2.end());

    //声明一个名为i的双向迭代器
    INTLIST::iterator i;

    //从前向后显示各list对象的元素
    put_list(list1,"list1");
    put_list(list2,"list2");
    put_list(list3,"list3");

    //从list1序列后面添加两个元素
    list1.push_back(2);
    list1.push_back(4);
    cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;
        put_list(list1,"list1");

    //从list1序列前面添加两个元素
    list1.push_front(5);
    list1.push_front(7);
    cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;
        put_list(list1,"list1");

    //在list1序列中间插入数据
    list1.insert(++list1.begin(),3,9);
    cout<<"list1.insert(list1.begin()+1,3,9):"<<endl;
        put_list(list1,"list1");

    //测试引用类函数
    cout<<"list1.front()="<<list1.front()<<endl;
    cout<<"list1.back()="<<list1.back()<<endl;

    //从list1序列的前后各移去一个元素
    list1.pop_front();
    list1.pop_back();
    cout<<"list1.pop_front() and list1.pop_back():"<<endl;
        put_list(list1,"list1");

    //清除list1中的第2个元素
    list1.erase(++list1.begin());
    cout<<"list1.erase(++list1.begin()):"<<endl;
        put_list(list1,"list1");

    //对list2赋值并显示
    list2.assign(8,1);
    cout<<"list2.assign(8,1):"<<endl;
    put_list(list2,"list2");

    //显示序列的状态信息
    cout<<"list1.max_size(): "<<list1.max_size()<<endl;
    cout<<"list1.size(): "<<list1.size()<<endl;
    cout<<"list1.empty(): "<<list1.empty()<<endl;

    //list序列容器的运算
    put_list(list1,"list1");
    put_list(list3,"list3");
    cout<<"list1>list3: "<<(list1>list3)<<endl;
    cout<<"list1<list3: "<<(list1<list3)<<endl;

    //对list1容器排序
    list1.sort();
        put_list(list1,"list1");

    //结合处理
    list1.splice(++list1.begin(), list3);
    put_list(list1,"list1");
    put_list(list3,"list3");
}

//补充:STL标准函数find进行vector 、list链表查找

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

class example
{
    public:
    example(int val)
    {
        i = val;
    }

    bool operator==(example const & rhs)
    {
        return (i == rhs.i) ? true : false;
    }

    private:
    int i;
};
using namespace std;
int main(void)
{
    vector<example> ve;
    ve.push_back(1);
    vector<example>::iterator it;
    example elem(1);
    it = find(ve.begin(), ve.end(), elem);
    cout<<boolalpha<<(*it == elem);
}
#endif
/*
vector<T>::iterator it;
list<T>::iterator it;
deque<T>::iterator it;

 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值