面试知识点总结——STL中map的用法

map属于关联容器,提供一对一的数据处理能力。内部是由红黑树实现的,具有自动排序能力。因此map内部的所有数据是有序的。

1. 插入操作
在map中插入数据有三种方法:

  • 使用数组
    用数组方式插入数据
#include<iostream>
#include<map>
#include<string>

using namespace std;
int main()
{
    map<string, int> student;
    student["Bob"] = 12;
    student["Jim"] = 13;
    student["Mary"] = 11;

    map<string,int>::iterator iter; //map迭代器
    for(iter=student.begin(); iter!=student.end(); iter++)
        cout<<iter->first<<" "<<iter->second<<endl;
}
  • insert函数插入pair数据
#include<iostream>
#include<string>
#include<map>

using namespace std;
int main()
{
    map<string, int> student;

    student.insert(pair<string,int>("Bob",12));
    student.insert(pair<string,int>("Jim",13));
    student.insert(pair<string,int>("Mary",11));

    map<string,int>::iterator iter; //map迭代器
    for(iter=student.begin(); iter!=student.end(); iter++)
        cout<<iter->first<<" "<<iter->second<<endl;
}
  • insert插入value_type数据
#include<iostream>
#include<string>
#include<map>

using namespace std;
int main()
{
    map<string, int> student;

    student.insert(map<string,int>::value_type("Bob",12));
    student.insert(map<string,int>::value_type("Jim",13));
    student.insert(map<string,int>::value_type("Mary",11));

    map<string,int>::iterator iter; //map迭代器
    for(iter=student.begin(); iter!=student.end(); iter++)
        cout<<iter->first<<" "<<iter->second<<endl;
}

使用后两者插入数据,利用的是集合的唯一性,也就是说当map容器中已经有了所要插入的数据,则insert操作是插入不了数据的。而使用数组操作时,会将已有的数据覆盖

2. 遍历操作

  • 前向迭代器
    使用前向迭代器的例子如上述几个例子中,均应用到了前向迭代器来遍历map容器中的数据。

  • 反向迭代器

map<string,int>::reverse_iterator iter;
for(iter=student.cbegin();iter!=student.end();++iter)
    cout<<iter->first<<" "<<iter->second<<endl;

方向迭代器,是将map容器中的元素反向遍历。

  • 数组遍历
    使用数组也可以对map容器进行遍历。
#include<iostream>
#include<map>
#include<string>

using namespace std;
int main()
{
    map<string, int> student;
    student["Bob"] = 12;
    student["Jim"] = 13;
    student["Mary"] = 11;

    int nSize = student.size();

    for(int index=1; index<=nSize; index++)
        cout<<student[index]<<endl;
}

3. 查找操作

  • count()函数的使用

    count函数只能返回0和1,无法定为数据出现的位置。如果待查元素在map容器中有,则返回1,没有则返回0。

  • find()函数的使用

    用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器

    不能使用数组下标的方法来查找,因为若使用数组下标查找,则对于map中没有的元素,会直接将这个元素插入。

4. 删除操作
移除某个map中某个条目用erase()
该成员方法的定义如下:
iterator erase(iterator it);//通过一个条目对象删除
iterator erase(iterator first,iterator last)//删除一个范围
size_type erase(const Key&key);//通过关键字删除
clear()就相当于enumMap.erase(enumMap.begin(),enumMap.end());

#include<iostream>
#include<map>
#include<string>

using namespace std;
int main()
{
    map<string, int> student;
    student["Bob"] = 12;
    student["Jim"] = 13;
    student["Mary"] = 11;

    map<string,int>::iterator iter; //map迭代器
    //使用迭代器删除某个元素
    iter = student.find("Jim");
    student.erase(iter);
    //使用key值删除某个元素,删除返回1,否则返回0
    int n = student.erase("Bob");
    //用迭代器,成片删除
    student.erase(student.begin(), student.end());
}

5. 排序操作
map中的元素是自动按Key升序排序,所以不能对map用sort函数。

猜你喜欢

转载自blog.csdn.net/caoyangxiaoyao/article/details/81234706