c++ 11标准模板(STL) std::map(三)旧

查找

返回匹配特定键的元素数量

std::map::count

函数

size_type count( const Key& key ) const;

返回拥有关键比较等价于指定参数的元素数,因为此容器不允许重复故为 1 或 0。

参数

key - 要计量元素数的关键值

返回值

拥有比较等价于 key 的关键的元素数,对于 (1) 为 1 或 0。

复杂度

与容器大小成对数。

示例

    cout << "countTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    cout << "map1.count(1): " << map1.count(1) << endl;
    cout << "map1.count(6): " << map1.count(6) << endl;
    cout << "countTest() end" << endl;

 c++ 11标准模板(STL) std::map(三)旧_第1张图片

寻找带有特定键的元素

std::map::find

函数

iterator find( const Key& key );                             (1)  

const_iterator find( const Key& key ) const;                (2) 

 1,2) 寻找键等于 key 的的元素。

参数

key - 要搜索的元素键值

返回值

指向键等于 key 的元素的迭代器。若找不到这种元素,则返回尾后(见 end() )迭代器。

复杂度

与容器大小成对数。

示例

    cout << "countTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    map::const_iterator it = map1.find(1);
    cout << "map1.find(1): " << "key:   " << (it == map1.end() ? "find no" : it->second) << endl;
    auto  it2 = map1.find(6);
    cout << "map1.find(6): " << "key:   " << (it2 == map1.end() ? "find no" : it2->second) << endl;
    cout << "countTest() end" << endl;

c++ 11标准模板(STL) std::map(三)旧_第2张图片

返回匹配特定键的元素范围

std::map::equal_range

函数

std::pair equal_range( const Key& key );                        (1)  

std::pair equal_range( const Key& key ) const;      (2) 

返回容器中所有拥有给定关键的元素范围。范围以二个迭代器定义,一个指向首个不小于 key 的元素,另一个指向首个大于 key 的元素。首个迭代器可以换用 lower_bound() 获得,而第二迭代器可换用 upper_bound() 获得。

1,2) 比较关键与 key

参数

key - 要比较元素的关键值
x - 能与 Key 比较的替用值

返回值

含一对定义所需范围的迭代器的 std::pair :第一个指向首个不小于 key 的元素,第二个指向首个大于 key 的元素。

若无元素不小于 key ,则将尾后(见 end() )迭代器作为第一元素返回。类似地,若无元素大于 key ,则将尾后迭代器作为第二元素返回。

复杂度

与容器大小成对数。

示例

    cout << "equal_rangeTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    std::pair::iterator, map::iterator> it = map1.equal_range(1);
    cout << "map1.equal_range(1): " << endl;
    cout << "it.first   " << (it.first == map1.end() ? "find no"  : it.first->second) << endl;
    cout << "it.second  " << (it.second == map1.end() ? "find no"  : it.second->second) << endl;
    auto it2 = map1.equal_range(6);
    cout << "map1.equal_range(6): " << endl;
    cout << "it2.first   " << (it2.first == map1.end() ? "find no"  : it2.first->second) << endl;
    cout << "it2.second  " << (it2.second == map1.end() ? "find no"  : it2.second->second) << endl;
    cout << "equal_rangeTest() end" << endl;

c++ 11标准模板(STL) std::map(三)旧_第3张图片

 返回指向首个不小于给定键的元素的迭代器

std::map::lower_bound

函数

iterator lower_bound( const Key& key );                     (1)  

const_iterator lower_bound( const Key& key ) const;         (1) 

1) 返回指向首个不小于 key 的元素的迭代器。

参数

key - 要与元素比较的关键值

返回值

指向首个不小于 key 的元素的迭代器。若找不到这种元素,则返回尾后迭代器(见 end() )。

复杂度

与容器大小成对数。

示例

    cout << "lower_boundTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    map::iterator it = map1.lower_bound(1);
    cout << "map1.lower_bound(1): " << "key:   " << (it == map1.end() ? "find no" : it->second) << endl;
    auto it1 = map1.lower_bound(6);
    cout << "map1.lower_bound(6): " << "key:   " << (it1 == map1.end() ? "find no" : it1->second) << endl;
    cout << "lower_boundTest() end" << endl;

c++ 11标准模板(STL) std::map(三)旧_第4张图片

 返回指向首个大于给定键的元素的迭代器

std::map::upper_bound

函数

iterator upper_bound( const Key& key );                        (1)  

const_iterator upper_bound( const Key& key ) const;            (1) 

1) 返回指向首个大于 key 的元素的迭代器。

参数

key - 与元素比较的关键值

返回值

指向首个大于 key 的元素的迭代器。若找不到这种元素,则返回尾后(见 end() )迭代器。

复杂度

与容器大小成对数。

示例

    cout << "upper_boundTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    map::iterator it = map1.upper_bound(1);
    cout << "map1.upper_bound(1): " << "key:   " << (it == map1.end() ? "find no" : it->second) << endl;
    auto it1 = map1.upper_bound(6);
    cout << "map1.upper_bound(6): " << "key:   " << (it1 == map1.end() ? "find no" : it1->second) << endl;
    cout << "upper_boundTest() end" << endl;

c++ 11标准模板(STL) std::map(三)旧_第5张图片

代码汇总

#include 
#include 
#include 

using namespace std;

struct myCompare
{
    bool operator()(const uint16_t a, const uint16_t b) const
    {
        return (a > b);
    }
    myCompare() {}
};

template ,
          typename _Alloc = std::allocator > >
void print_Map(const string &name, const map<_Key, _Tp, _Compare, _Alloc> &tmap)
{
    cout << name << ": \n";
    for (const std::pair it : tmap)
    {
        cout << "key: " << it.first << ", value: " << it.second << endl;
    }
}

void constructorTest()
{
    cout << "constructorTest() begin" << endl;
    // 构造空容器
    map map1;
    cout << "map1.size() : " << map1.size() << endl;

    // 列表初始化,构造, 排序递增
    map> map2{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    print_Map("initializer_list, ASC", map2);
    // 列表初始化,构造, 排序递减
    map> map6{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    print_Map("initializer_list, DESC", map6);
    // 列表初始化,构造, 排序自定义
    map map7{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    print_Map("initializer_list, DESC", map7);

    // 范围构造,排序递增
    map> map3(map2.begin(), map2.end());
    print_Map("range, ASC", map3);
    // 范围构造,排序递减
    map> map8(map2.begin(), map2.end());
    print_Map("range, DESC", map8);


    // 拷贝构造,类型必须一致,包括排序,排序递增
    map> map4(map3);
    print_Map("copy, ASC", map4);
    // 拷贝构造,排序递增
    map> map9(map8);
    print_Map("copy, DESC", map9);

    // 赋值构造,类型必须一致,包括排序,排序递增
    map> map10 = map4;
    print_Map("assignment , ASC", map10);
    // 赋值构造,类型必须一致,包括排序,排序递减
    map> map11 = map9;
    print_Map("assignment , DESC", map11);

    cout << "constructorTest() end" << endl;
}

void elementAccessTest()
{
    cout << "elementAccessTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    cout << "at(key) : " << map1.at(1) << endl;
    cout << "[key] : " << map1[2] << endl;
    // map容器产生<6,"">节点
    cout << "[key] : " << map1[6] << endl;
    print_Map("[key] , ASC", map1);
    // map容器产生<7,"H">节点
    map1[7] = "H";
    print_Map("[key] , ASC", map1);
    // map容器key为1的节点value被替换为"K"
    map1[1] = "K";
    print_Map("[key] , ASC", map1);
    cout << "elementAccessTest() end" << endl;
}

void capacityTest()
{
    cout << "capacityTest begin" << endl;
    map map1;
    cout << "map1: " << (map1.empty() ? "empty" : "not empty");
    cout << "map1 size() " << map1.size() << endl;;
    map1[1] = "A";
    map1[2] = "B";
    cout << "map1: " << (map1.empty() ? "empty" : "not empty");
    cout << "map1 size() " << map1.size() << endl;;
    cout << "map max_size: " << map1.max_size() << std::endl;
    map map2;
    cout << "map max_size: " << map2.max_size() << std::endl;
    map map3;
    cout << "map max_size: " << map3.max_size() << std::endl;
    map map4;
    cout << "map max_size: " << map4.max_size() << std::endl;
    cout << "capacityTest end" << endl;
}

void iteratorTest()
{
    cout << "iteratorTest begin" << endl;
    map map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    // 遍历容器, const_iterator
    std::cout << "const_iterator:\n";
    for (map::const_iterator it = map1.cbegin(); it != map1.cend(); it++)
    {
        cout << "key: " << it->first << ", value: " << it->second << endl;
    }
    std::cout << "const_iterator:\n";
    for (map::const_reverse_iterator it = map1.crbegin(); it != map1.crend(); it++)
    {
        cout << "key: " << it->first << ", value: " << it->second << endl;
    }

    std::cout << "begin():   " << std::hex << (int)&*map1.begin() << std::endl;
    std::cout << "cbegin():  " << std::hex << (int)&*map1.cbegin() << std::endl;
    std::cout << "end():     " << std::hex << (int)&*map1.end() << std::endl;
    std::cout << "cend():    " << std::hex << (int)&*map1.cend() << std::endl;
    std::cout << "rbegin():  " << std::hex << (int)&*map1.rbegin() << std::endl;
    std::cout << "crbegin(): " << std::hex << (int)&*map1.crbegin() << std::endl;
    std::cout << "rend():    " << std::hex << (int)&*map1.rend() << std::endl;
    std::cout << "crend():   " << std::hex << (int)&*map1.crend() << std::endl;

    cout << "iteratorTest end" << endl;
}

void clearTest()
{
    cout << "clearTest() begin" << endl;
    map map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    print_Map("clear before", map1);
    map1.clear();
    cout << "map1: " << (map1.empty() ? "empty" : "not empty");
    cout << "clearTest() end" << endl;
}

void insertTest()
{
    cout << "insertTest() begin" << endl;
    map map1{{1, "A"}, {2, "B"}, {3, "C"}};
    print_Map("insert before", map1);
    std::pair::iterator, bool> itI = map1.insert({4, "D"});
    cout << "insert " << (itI.second ? "success" : "fail") ;
    cout << " key: " << itI.first->first << " value: " << itI.first->second <<  endl;
    print_Map("insert after", map1);

    map::const_iterator pIt = map1.find(2);
    cout << "find key: " << pIt->first << " value: " << pIt->second << endl;

    map::const_iterator nIt = map1.insert(pIt, {5, "E"});
    cout << "insert key: " << nIt->first << " value: " << nIt->second << endl;
    print_Map("insert after", map1);

    map map2{{6, "F"}, {7, "H"}};
    map2.insert(map1.begin(), map1.end());
    print_Map("map2 insert after", map2);
    cout << "insertTest() end" << endl;
}

void emplaceTest()
{
    cout << "emplaceTest() begin" << endl;
    map map1{{1, "A"}, {2, "B"}, {3, "C"}};
    print_Map("emplace before", map1);

    std::pair::iterator, bool> itI = map1.emplace(make_pair(4, "D"));
    cout << "emplace " << (itI.second ? "success" : "fail") ;
    cout << " key: " << itI.first->first << " value: " << itI.first->second <<  endl;
    print_Map("emplace after", map1);

    map::const_iterator pIt = map1.find(2);
    map::const_iterator nIt = map1.emplace_hint(pIt, make_pair(5, "E"));
    cout << "emplace_hint key: " << nIt->first << " value: " << nIt->second << endl;
    print_Map("emplace_hint after", map1);

    cout << "emplaceTest() end" << endl;
}

void eraseTest()
{
    cout << "eraseTest() begin" << endl;
    map map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    print_Map("erase before", map1);
    size_t ct = map1.erase(3);
    cout << "erase size: " << ct << std::endl;
    ct = map1.erase(3);
    cout << "erase size: " << ct << std::endl;
    print_Map("map1 erase after", map1);

    map::const_iterator pIt = map1.find(2);
    map::const_iterator eIt = map1.erase(pIt);
    cout << "erase key: " << eIt->first << " value: " << eIt->second << endl;
    print_Map("map1 erase after", map1);

    map map2{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    print_Map("map2 erase before", map2);
    map2.erase(map2.begin(), map2.end());
    print_Map("map2 erase after", map2);
    cout << "eraseTest() end" << endl;
}

void swapTest()
{
    cout << "swapTest() begin" << endl;
    map map1{{1, "A"}, {2, "B"}, {3, "C"}};
    map map2{{4, "D"}, {5, "E"}};
    print_Map("map1 swap before", map1);
    print_Map("map2 swap before", map2);
    map1.swap(map2);
    print_Map("map1 swap after", map1);
    print_Map("map2 swap after", map2);
    cout << "swapTest() end" << endl;
}

void countTest()
{
    cout << "countTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    cout << "map1.count(1): " << map1.count(1) << endl;
    cout << "map1.count(6): " << map1.count(6) << endl;
    cout << "countTest() end" << endl;
}

void findTest()
{
    cout << "countTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    map::const_iterator it = map1.find(1);
    cout << "map1.find(1): " << "key:   " << (it == map1.end() ? "find no" : it->second) << endl;
    auto  it2 = map1.find(6);
    cout << "map1.find(6): " << "key:   " << (it2 == map1.end() ? "find no" : it2->second) << endl;
    cout << "countTest() end" << endl;
}

void equal_rangeTest()
{
    cout << "equal_rangeTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    std::pair::iterator, map::iterator> it = map1.equal_range(1);
    cout << "map1.equal_range(1): " << endl;
    cout << "it.first   " << (it.first == map1.end() ? "find no"  : it.first->second) << endl;
    cout << "it.second  " << (it.second == map1.end() ? "find no"  : it.second->second) << endl;
    auto it2 = map1.equal_range(6);
    cout << "map1.equal_range(6): " << endl;
    cout << "it2.first   " << (it2.first == map1.end() ? "find no"  : it2.first->second) << endl;
    cout << "it2.second  " << (it2.second == map1.end() ? "find no"  : it2.second->second) << endl;
    cout << "equal_rangeTest() end" << endl;
}

void lower_boundTest()
{
    cout << "lower_boundTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    map::iterator it = map1.lower_bound(1);
    cout << "map1.lower_bound(1): " << "key:   " << (it == map1.end() ? "find no" : it->second) << endl;
    auto it1 = map1.lower_bound(6);
    cout << "map1.lower_bound(6): " << "key:   " << (it1 == map1.end() ? "find no" : it1->second) << endl;
    cout << "lower_boundTest() end" << endl;
}

void upper_boundTest()
{
    cout << "upper_boundTest() begin" << endl;
    map> map1{{1, "A"}, {2, "B"}, {3, "C"}, {4, "D"}, {5, "E"}};
    map::iterator it = map1.upper_bound(1);
    cout << "map1.upper_bound(1): " << "key:   " << (it == map1.end() ? "find no" : it->second) << endl;
    auto it1 = map1.upper_bound(6);
    cout << "map1.upper_bound(6): " << "key:   " << (it1 == map1.end() ? "find no" : it1->second) << endl;
    cout << "upper_boundTest() end" << endl;
}

int main()
{
//    constructorTest();
//    elementAccessTest();
//    capacityTest();
//    iteratorTest();
//    clearTest();
//    insertTest();
//    emplaceTest();
//    eraseTest();
//    swapTest();
    countTest();
    findTest();
    equal_rangeTest();
    lower_boundTest();
    upper_boundTest();
    return 0;
}

你可能感兴趣的:(#,std::map,c++,标准库模板,map,有序键值对容器,数据结构)