067-C++中vector,list,deque,queue,stack,proiority_queue,map的使用


/*
 泛型编程的思想
 如果说面向对象是一种通过间接层来调用函数
 泛型编程可以将算法与特定类型,进行剥离
 */

/*
 STL算法是泛型的 不
 
 容器:
 1. 序列式容器(Sequence Containers)
 其中的元素都是可排序的(ordered) STL提供了 vector, list, deque
 等序列式容器, 而stack, queue, priority_queue则是容器适配器
 2. 关联式容器(Associate Containers)
 map multimap set multiset
 
 STL容器的理解层次
 1. 会用
 2. 原理
 3. 自己定制 融合
 
 */
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>

using namespace std;


//int max(int a, int b){
//    return a>b? a: b;
//}

template<class T>
T maxx(T a, T b) {
   return a>b?a:b;
}

class Display {
public:
    void operator()(int i){
        cout<<i<<" ";
    }
    
    void access(int i){
        cout<<i<<endl;
    }
};

//仿函数
struct DisplayData {
    void operator()(pair<string, double> info){
        cout<<info.first<<":"<<info.second<<endl;
    };
};

struct StuScore {
    string name;
    double score;
    
    void show(){
        cout<<"name:"<<name<<",score:"<<score<<endl;
    }
};


int main(int argc, const char * argv[]) {
   int iArr[] = {1, 2, 3, 4, 5};
   // 向量
   vector<int> iVector(iArr, iArr+5);
   // 列表
   list<int> iList(iArr, iArr+5);
   // 双向队列
   deque<int> iDeque(iArr, iArr+5);

   // 队列 先进先出
   queue<int> iQueue(iDeque);
   // 栈  先进后出
   stack<int> iStack(iDeque);
   // 优先队列 按优先权
   priority_queue<int> iPueue(iArr, iArr+5);

   cout<<"依次取出vector中元素 "<<endl; 
   for_each(iVector.begin(), iVector.end(), Display());
   cout<<endl;
   cout<<"依次取出list中元素 "<<endl; 
   for_each(iList.begin(), iList.end(), Display());
   cout<<endl;
   cout<<"依次取出deque中元素 "<<endl; 
   for_each(iDeque.begin(), iDeque.end(), Display()); // Display 可以是class 也可以是struct
   cout<<endl;
   
   cout<<"依次取出queue中元素 "<<endl; 
   while (!iQueue.empty()){
       cout<<iQueue.front()<<" ";
       iQueue.pop();
   }
   cout<<endl;
   
   cout<<"依次取出stack中元素 "<<endl; 
   while (!iStack.empty()){
       cout<<iStack.top()<<" ";
       iStack.pop();
   }
   cout<<endl;
   
   cout<<"取出priority_queue中元素 "<<endl; 
   while (!iPueue.empty()){
       cout<<iPueue.top()<<" ";
       iPueue.pop();
   }
   cout<<endl;
    

    map<string, double> studentScores;
    studentScores["LiMing"] = 94.5;
    studentScores["LiHong"] = 90.2;
    studentScores.insert(pair<string, double>("zhangsan", 100.0));
    studentScores.insert(pair<string, double>("lisi", 91.0));
    studentScores.insert(pair<string, double>("wangwu", 92.0));
    studentScores.insert(map<string, double>::value_type("xiaohong", 90.0));
    // DisplayData加上()表示是一个函数
    for_each(studentScores.begin(), studentScores.end(), DisplayData());
    
    cout<<"vector遍历元素 "<<endl;
    vector<StuScore> vectors;
    StuScore stu1;
    stu1.name = "lisi";
    stu1.score = 99.0;
    vectors.push_back(stu1);
    struct StuScore stu2;
    stu2.name = "wangwu";
    stu2.score = 87;

    vectors.push_back(stu2);
    //vector<int>::iterator it = vectors.begin();
    auto it = vectors.begin();
    for (; it!=vectors.end(); it++) {
        (*it).show();
    }
    cout<<"-----------"<<endl;
    cout<<"map 查找元素 "<<endl;
    map<string, double>::iterator iter;
    iter = studentScores.find("wangwu");
    if (iter!=studentScores.end()){
        cout<<"found "<<iter->first<<" "<<iter->second<<endl;
    } else {
        cout<<"not found "<<endl;
    }
    return 0;
}

输出如下:

依次取出vector中元素 
1 2 3 4 5 
依次取出list中元素 
1 2 3 4 5 
依次取出deque中元素 
1 2 3 4 5 
依次取出queue中元素 
1 2 3 4 5 
依次取出stack中元素 
5 4 3 2 1 
取出priority_queue中元素 
5 4 3 2 1 
LiHong:90.2
LiMing:94.5
lisi:91
wangwu:92
xiaohong:90
zhangsan:100
vector遍历元素 
name:lisi,score:99
name:wangwu,score:87
-----------
map 查找元素 
found wangwu 92

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值