接着学习map容器的常见API操作,依然是大部分前面学习过的,名称相同的API的基本调用测试。本篇学习map容器大小相关API,然后学习容器交换,容器元素插入和删除,清空等操作。

1.map容器大小操作

统计map容器大小的函数原型

#include <iostream>
#include <string>
#include <map>
using namespace std;


void test01()
{
    // map容器的构造
    map<string, int> m;

    // map容器添加元素
    m.insert(pair<string, int>("Tom", 18));
    m.insert(pair<string, int>("Anthony", 23));
    m.insert(pair<string, int>("Bob", 24));
    m.insert(pair<string, int>("Sunny", 19));
    
    // 统计容器大小
    cout << "size of Map: " << m.size() << endl;

    // 容器是否为空
    cout << "is empty? " << m.empty() << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}

运行结果

数字0表示非空,数字1表示容器为空

 

2.map容器交换

函数原型还是swap(map)

#include <iostream>
#include <string>
#include <map>
using namespace std;

void printMap(map<string, int>& m)
{
    for(map<string, int> ::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "Key= " << (*it).first << " ,Value= " << (*it).second << endl;
    }
}

void test01()
{
    // map容器的构造
    map<string, int> m;

    // map容器添加元素
    m.insert(pair<string, int>("Tom", 18));
    m.insert(pair<string, int>("Anthony", 23));
    m.insert(pair<string, int>("Bob", 24));
    m.insert(pair<string, int>("Sunny", 19));
    
    map<string, int> m1;
    m1.insert(pair<string, int>("aaaa", 243254));
    m1.insert(pair<string, int>("bbbb", 10000));

    cout << "=======m=========== " << endl;
    printMap(m);
    cout << "========m1========== " << endl;
    printMap(m1);

    cout << "================== " << endl;
    // 容器交换
    m.swap(m1);
    cout << "=======m=========== " << endl;
    printMap(m);
    cout << "========m1========== " << endl;
    printMap(m1);
   
}

int main()
{
    test01();
    system("pause");
    return 0;
}

运行结果

 

3.map容器插入和删除

相关插入和删除函数原型

插入这里不重复,就是前面一直在使用的insert()函数。

#include <iostream>
#include <string>
#include <map>
using namespace std;

void printMap(map<string, int>& m)
{
    for(map<string, int> ::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "Key= " << (*it).first << " ,Value= " << (*it).second << endl;
    }
}

void test01()
{
    // map容器的构造
    map<string, int> m;

    // map容器添加元素
    m.insert(pair<string, int>("Tom", 18));
    m.insert(pair<string, int>("Anthony", 23));
    m.insert(pair<string, int>("Bob", 24));
    m.insert(pair<string, int>("Sunny", 19));
    printMap(m);
    
    //删除迭代器所指的元素
    map<string, int>::iterator pos = m.begin();
    m.erase(pos);
    cout << "after erase pos=======================" << endl;
    printMap(m);

    //删除容器中值为Sunny
    m.erase("Sunny");
    cout << "after erase key=======================" << endl;
    printMap(m);

    // 清空容器
    m.clear();
    cout << "is Empty?  " <<m.empty() << endl;
   
}

int main()
{
    test01();
    system("pause");
    return 0;
}

测试结果

 

4.map容器查找和统计

接着来看看查找和统计的API

#include <iostream>
#include <string>
#include <map>
using namespace std;

void printMap(map<string, int>& m)
{
    for(map<string, int> ::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "Key= " << (*it).first << " ,Value= " << (*it).second << endl;
    }
}

void test01()
{
    // map容器的构造
    map<string, int> m;

    // map容器添加元素
    m.insert(pair<string, int>("Tom", 18));
    m.insert(pair<string, int>("Anthony", 23));
    m.insert(pair<string, int>("Bob", 24));
    m.insert(pair<string, int>("Sunny", 19));
    printMap(m);
    
    // 查找
    map<string, int>::iterator pos = m.find("Anthony");
    if(pos != m.end())
    {
        cout << "find result: " << (*pos).first << " = " << (*pos).second << endl;
    }
    else
    {
        cout << "not find"<< endl;
    }

    // 统计,要不是返回结果0就是1
    cout << "count of Sunny:"<< m.count("Sunny") << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}

测试结果

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐