zoukankan      html  css  js  c++  java
  • Sword STL容器分类介绍

    标准STL序列容器:vector、string、deque和list。
    标准STL关联容器:set、multiset、map和multimap。
    非标准序列容器slist和rope。slist是一个单向链表,rope本质上是一个重型字符串
    非标准关联容器hash_set、hash_multiset、hash_map和hash_multimap。
    
    标准STL容器提供了四种不同的迭代器:iterator、 const_iterator、reverse_iterator和const_reverse_iterator。
    
    每个标准容器类都提供四种迭代器类型。
    对于container<T>而言,iterator的作用相当于T*, 
    而const_iterator则相当于const T*(你可能也见过T const*这样的写法:它们意思一样的)。
    增加一个iterator或 者const_iterator可以在一个从容器开头趋向尾部的遍历中让你移动到容器的下一个元素。
    reverse_iterator与 const_reverse_iterator同样相当于对应的T*和const T*,
    所不同的是,增加reverse_iterator或者 const_reverse_iterator会在从尾到头的遍历中让你移动到容器的下一个元素。
    /* 序列容器删除 */
    #include <iostream>
    #include <string.h>
    #include <vector>
    #include <list>
    #include <map>
    #include <time.h>
    #include <algorithm> //remove_if头文件
    
    using namespace std;
    
    bool badValue1(int x)
    {
        return (4 == x ? true : false);
    }
    
    bool badValue2(int x)
    {
        return (3 == x ? true : false);
    }
    
    void printNum(int x)
    {
        cout << x << endl;
    }
    
    void test()
    {
        vector<int> v1 = { 1,2,3,4,5,6,7,8 };
    
        //删除vector、string、deque指定元素的方法
        v1.erase(remove_if(v1.begin(), v1.end(), badValue1));
        for_each(v1.begin(), v1.end(), printNum);
    
        //删除vector、string、deque过程中如果需要做特别操作,那么必须循环遍历使用erase方法删除
        vector<int>::iterator it;
        for (it = v1.begin(); it != v1.end();)
        {
            //注意序列容器的删除用法
            if (badValue2(*it))
            {
                cout << "i am erase ." << *it << endl;
                it = v1.erase(it);
            }
            else
            {
                it++;//这里使用前置++和后置+=没区别
            }
        }
    
        for_each(v1.begin(), v1.end(), printNum);
    
    }
    
    int main()
    {
        test();
        getchar();
        return 0;
    }
    /* 关联容器删除 */
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <map>
    #include <time.h>
    #include <algorithm> //remove_if头文件
    
    using namespace std;
    
    void test()
    {
        map<int, string> m1;
        m1.insert(make_pair(0, "a"));
        m1.insert(make_pair(1, "b"));
        m1.insert(make_pair(2, "c"));
        m1.insert(make_pair(3, "d"));
        m1.insert(make_pair(4, "e"));
        m1.insert(make_pair(5, "f"));
    
        //删除set、multiset、map和multimap指定元素的方法,关联容器没有remove方法,只能遍历删除
        map<int, string>::iterator it;
    
        for (it = m1.begin(); it != m1.end(); /*递增条件为空*/)
        {
            if ("a" == it->second)
            {
                m1.erase(it++);//注意这里使用后置++,表示先取得后面的迭代器,返回当前迭代器给earse方法
            }
            else
            {
                it++;
            }
        }
    
        for (it = m1.begin(); it != m1.end(); ++it)
        {
            cout << it->second << endl;
        }
    
    }
    
    int main()
    {
        test();
        getchar();
        return 0;
    }
    为了避免你奇怪list的适当方法是什么,事实表明对于迭代和删除,你可以像vector/string/deque一样或像关联 容器一样对待list;两种方法都可以为list工作
    总结:
    几乎所有的容器都在同名的头文件里,比如,vector在<vector>中声明,list在<list>中声明等。例外的 是<set>和<map>。<set>声明了set和multiset
    ,<map>声明了map和multimap。 除了四个算法外,所有的算法都在<algorithm>中声明。例外的是accumulate(参见条款37)、 inner_product、adjacent_difference和partial_sum。
    这些算法在<numeric>中声明。 特殊的迭代器,包括istream_iterators和istreambuf_iterator,在<iterator>中声明。 标准仿函数(比如less<T>)和仿函数适配器(比如not1、bind2nd)在<functional>中声明
  • 相关阅读:
    在IE和Firfox获取keycode
    using global variable in android extends application
    using Broadcast Receivers to listen outgoing call in android note
    help me!virtual keyboard issue
    using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.
    javascript:jquery.history.js使用方法
    【CSS核心概念】弹性盒子布局
    【Canvas学习笔记】基础篇(二)
    【JS核心概念】数据类型以及判断方法
    【问题记录】ElementUI上传组件使用beforeupload钩子校验失败时的问题处理
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/9880417.html
Copyright © 2011-2022 走看看