stl之multimap

xiaoxiao2021-02-28  45

C++ MultiMaps

C++ Multimaps和maps很相似,但是MultiMaps允许重复的元素。

begin()返回指向第一个元素的迭代器clear()删除所有元素count()返回一个元素出现的次数empty()如果multimap为空则返回真end()返回一个指向multimap末尾的迭代器equal_range()返回指向元素的key为指定值的迭代器对erase()删除元素find()查找元素get_allocator()返回multimap的配置器insert()插入元素key_comp()返回比较key的函数lower_bound()返回键值>=给定元素的第一个位置max_size()返回可以容纳的最大元素个数rbegin()返回一个指向mulitmap尾部的逆向迭代器rend()返回一个指向multimap头部的逆向迭代器size()返回multimap中元素的个数swap()交换两个multimapsupper_bound()返回键值>给定元素的第一个位置value_comp()返回比较元素value的函数 #include <iostream> #include <map> using namespace std; int main(int argc, char *argv[]) { multimap<int,string> mapStudent; for(int i = 0; i < 10; i++) { char student_name[10]; sprintf(student_name, "student%d", i); mapStudent.insert(make_pair(i,string(student_name))); } //遍历 multimap<int,string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end();iter++) { pair<int,string> p = *iter; // cout<<p.first<<" "<<p.second<<endl; } //查找 multimap<int,string>::iterator it = mapStudent.find(1); pair<int,string> student = *it; cout<<student.first<<" "<<student.second<<endl; return 0; }

转载请注明原文地址: https://www.6miu.com/read-2624684.html

最新回复(0)