栈,映射,集合

栈和队列共同支持的操作
 s1 op s2 op可以是==、!=、<、<=、>、>=之一,它会对两个容器适配器之间的元素按字典序进行比较
 s.size() 返回s的元素个数
 s.empty() 返回s是否为空
 s.push(t) 将元素t压入到s中
 s.pop() 将一个元素从s中弹出,对于栈来说,每次弹出的是最后被压入的元素,
而对于队列,每次被弹出的是最先被压入的元素
 不支持迭代器,因为它们不允许对任意元素进行访问

栈和队列不同的操作
 栈的操作
 s.top() 返回栈顶元素的引用
 队列操作
 s.front() 获得队头元素的引用
 s.back() 获得队尾元素的引用

利用栈反向弹出单词:

#include "stdafx.h"
#include<stack>
#include<iostream>
#include<string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    stack<char>s;
    string words;
    cin >> words;
    for (string::iterator iter = words.begin(); iter != words.end(); iter++)  //迭代器默认空位置为结束
        s.push(*iter);
    while (!s.empty())
    {
    
        cout << s.top();
        s.pop();
    }
    cout << endl;
    return 0;
}

集合(set):用来存储一组无重复的元素,集合中的元素本身是有序的,所以可以高效地查找指定元素,也可以方便地得到指定大小范围的元素在容器中所处的区间。

#include<utility>
#include "stdafx.h"
#include<set>
#include<iostream>
#include<iterator>
#include<string>
#include<algorithm>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    set<int>s;
    while (true)
    {
        int num;
        cin >> num;
        if (num == 0)
            break;
        pair<set<int>::iterator, bool> r = s.insert(num);
        //iterator表示插入的位置,bool标志着插入是否成功,若key_value已经在set中,则iterator表示key_value在set中的位置
        if (!r.second) //已经存在,没有成功插入,返回0。若插入成功,则返回1,r是pair类型,有first的迭代器和second的bool
            cout << num << "has duplicated" << endl;
    }
    
    set<int>::iterator iter = s.begin();
    set<int>::iterator itera = s.end();
    int mid = (*iter + *(--itera)) / 2;  //itera是尾后迭代器,指向的是末尾元素后一个位置,所以要使用的话,需要先将其位置减1,往前挪
    cout << "<=mid" << endl;
    copy(s.begin(),s.upper_bound(mid), ostream_iterator<int>(cout, " "));  //s.upper_bound(mid)是将mid作为区间的最大值
    cout << endl;
    cout << ">=mid" << endl;
    copy(s.lower_bound(mid), s.end(), ostream_iterator<int>(cout, " "));  //s.lower_bound(mid)是将mid作为区间的最小值
    return 0;
}

映射(map):与集合的区别:
集合的元素类型是键本身,映射的元素类型是由键和附加数据所构成的二元组,在集合中按照键查找一个元素时,一般用来确定这个元素是否存在
在映射中,按照键查找一个元素时,除了能确定它的存在外,还可以得到相应的附加数据。

有四门课程,每门课程有相应学分,选其中三门,并求和:

#include<utility>
#include "stdafx.h"
#include<map>
#include<iostream>
#include<string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    map<string, int>courses;
    courses.insert(make_pair("csapp", 3));
    courses.insert(make_pair("C++", 4));
    courses.insert(make_pair("math", 5));
    courses.insert(make_pair("english", 6));
    int num = 2;
    int sum = 0;
    while (num > 0)
    {
        string name;
        cin >> name;
        map<string, int>::iterator it = courses.find(name);
        if (it == courses.end())
        {
            cout << name << "is not a vailable courses" << endl;
        }
        else
        {
            sum += it->second;
            courses.erase(it);
            --num;
        }
    }
    cout << "total credit:" << sum << endl;
    return 0;
}

转载于:https://www.cnblogs.com/ymd12103410/p/9607909.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值