cpp学习笔记(四)

来源:互联网 发布:海盗船mac驱动 编辑:程序博客网 时间:2024/06/04 19:50

第22章 理解函数对象
第23章 STL算法
第24章 自适应容器:栈和队列
第25章 使用STL位标志

第22章 理解函数对象
函数对象的概念
将函数对象作为谓词
如何使用函数对象实现一元和二元谓词

第23章 STL算法
了解算法的分类:变序算法和非变序算法
非变序算法
计算cout cout_if
搜索 search,search_n,find,find_if,find_end,find_first_of,adjacent_find
比较算法
equal,mismatch,lexicographical_compare

变序算法
初始化算法 fill,fill_n,generate,generate_n
修改算法 for_each,transform
赋值算法 copy copy_backward
删除算法 remove remove_if remove_copy remove_copy_of unique unique_copy
替换算法
replace replace_if
排序算法
sort,stable_sort,partial_sort,partial_sort_copy

分区算法    partition    stable_partition可用于排序容器的算法    binary_search    lower_bound    upper_bound

第24章 自适应容器:栈和队列
栈和队列的行为特征
使用stack
使用queue
使用STL priority_queue

第25章 使用STL位标志
bitset类
vector

////  main.cpp//  more_stl_22_25////  Created by bikang on 16/10/09.//  Copyright (c) 2016年 bikang. All rights reserved.//#include <iostream>#include <algorithm>#include <list>#include <vector>#include <set>#include <stack>#include <queue>#include <bitset>using namespace std;template <typename elementType>struct Display{    int mCount;    Display(){        mCount = 0;    }    void operator () (const elementType & elem){        ++mCount;        cout << elem << " ";    }};template <typename numberType>struct IsMultiple {    numberType mDiv;    IsMultiple(const numberType&div){        mDiv = div;    }    bool operator ()(const numberType& elem)const{        return ((elem%mDiv) == 0);    }};//二元函数template <typename numberType>class CMultiple {public:    numberType operator ()(const numberType& elem1,const numberType& elem2){        return elem1*elem2;    }};class XMultiple {public:    bool operator ()(const int& elem1,const int& elem2){        return (elem1 < elem2);    }};void tfuncOjbect21();void tfuncAlgorithm22();//algorithm略过了,到时候查参考手册void tfuncStackQueue23();void tfuncBitset25();int main(int argc, const char * argv[]) {    //tfuncOjbect21();    //tfuncStackQueue23();    tfuncBitset25();    return 0;}void tfuncBitset25(){    //bitset    bitset<4> b1;    cout << b1 << endl;    bitset<5> b2(string("10101"));    cout << b2 << endl;    bitset<5> b3(255);    cout << b3 << endl;    b2.set(2,0);    cout << b2<<endl;    bitset<4>b4(b1);    bitset<4>b5(string("1010"));    cout << (b4 | b5)<<endl ;    //vector<bool>    vector<bool> vecb(1);    vecb[0] = false;    vecb.push_back(true);    for(int i=0;i<vecb.size();i++){        cout << vecb[i]<<",";    }}void tfuncStackQueue23(){    //use stack    //stack<int> s1;    stack<int,vector<int>> s1;    s1.push(6);    s1.push(2);    s1.push(3);    s1.push(5);    cout << s1.size() <<endl;    while(s1.size() != 0) s1.pop();    if(s1.empty()) cout << "s1 empty"<<endl;    //use queue    queue<int,list<int>> q1;    q1.push(6);    q1.push(2);    q1.push(3);    q1.push(5);    cout << q1.size()<<endl;    //priority_queu    priority_queue<int> pq1;    pq1.push(6);    pq1.push(2);    pq1.push(13);    pq1.push(5);    cout << pq1.top()<<endl;    pq1.pop();    cout << pq1.top()<<endl;}void tfuncOjbect21(){    //实现了operator的对象    //一元函数,二元函数    vector<int> vec1;    for (int i=1; i<10; ++i) {        vec1.push_back(i);    }    vector<int> vec2;    for (int i=11; i<20; ++i) {        vec2.push_back(i);    }    Display<int> res;    res = for_each(vec1.begin(), vec1.end(), Display<int>());    cout <<endl << res.mCount<<endl;    //一元谓词的使用    vector<int>::iterator iter1;    iter1 = find_if(vec1.begin(), vec1.end(), IsMultiple<int>(4));    if(iter1 != vec1.end()){        cout << *iter1 <<endl;    }    vector<int>result;    result.resize(9);    //二元函数的使用    transform(vec1.begin(), vec1.end(), vec2.begin(),result.begin(),CMultiple<int>());    for(iter1=result.begin();iter1!=result.end();++iter1){        cout << *iter1<<",";    }    cout << "\n";    //二元谓词的使用,可以用来排序    typedef set<int ,XMultiple> SET_NUMBERS;    SET_NUMBERS s1;    s1.insert(123);    s1.insert(2);    s1.insert(1);    SET_NUMBERS::const_iterator ite1;    for (ite1=s1.begin(); ite1!= s1.end(); ite1++)    {        cout << *ite1 <<",";    }}
0 0
原创粉丝点击