STL C++之关联式容器set/multiset/map/multimap

模板类std::set

std::set

template < class T,                        // set::key_type/value_type
           class Compare = less<T>,        // set::key_compare/value_compare
           class Alloc = allocator<T>      // set::allocator_type
           > class set;

集合是按照特定顺序存储唯一元素的容器。

在集合中,元素的值也标识它(值本身就是键,类型为 T),并且每个值必须是唯一的。 集合中元素的值不能在容器中修改一次(元素始终为 const),但可以从容器中插入或删除它们。

在内部,集合中的元素始终按照其内部比较对象(比较类型)指示的特定严格弱排序标准进行排序。

set 容器通常比 unordered_set 容器通过键访问单个元素要慢,但它们允许根据它们的顺序对子集进行直接迭代。

集合通常实现为二叉搜索树。

创建C++ set容器

std::set::set

empty (1)	explicit set (const key_compare& comp = key_compare(),
           				const allocator_type& alloc = allocator_type());
            explicit set (const allocator_type& alloc);
range (2)	template <class InputIterator>
 			set (InputIterator first, InputIterator last,
   				const key_compare& comp = key_compare(),
       			const allocator_type& = allocator_type());
copy (3)	set (const set& x);
			set (const set& x, const allocator_type& alloc);
move (4)	set (set&& x);
			set (set&& x, const allocator_type& alloc);
initializer list (5)	
			set (initializer_list<value_type> il,
     			const key_compare& comp = key_compare(),
   				const allocator_type& alloc = allocator_type());
// constructing sets
#include <iostream>
#include <set>

bool fncomp(int lhs, int rhs) { return lhs < rhs; }

struct classcomp {
	bool operator() (const int& lhs, const int& rhs) const
	{
		return lhs < rhs;
	}
};

int main()
{
	std::set<int> first;                           // empty set of ints

	int myints[] = { 10, 20, 30, 40, 50 };
	std::set<int> second(myints, myints + 5);        // range

	std::set<int> third(second);                     // a copy of second

	std::set<int> fourth(second.begin(), second.end());  // iterator ctor.

	std::set<int, classcomp> fifth;                  // class as Compare

	bool(*fn_pt)(int, int) = fncomp;
	std::set<int, bool(*)(int, int)> sixth(fn_pt);   // function pointer as Compare

	std::cout << "fourth contains:";
	for (const int& x : fourth) std::cout << " " << x;
	std::cout << std::endl;

	return 0;
}

输出

fifth contains: 10 20 30 40 50

插入元素

std::set::insert

single element (1)	pair<iterator,bool> insert (const value_type& val);
					pair<iterator,bool> insert (value_type&& val);
with hint (2)		iterator insert (const_iterator position, const value_type& val);
					iterator insert (const_iterator position, value_type&& val);
range (3)			template <class InputIterator>
 						void insert (InputIterator first, InputIterator last);
initializer list (4)	void insert (initializer_list<value_type> il);

通过插入新元素来扩展容器,通过插入的元素数量有效地增加容器大小。

因为集合中的元素是唯一的,所以插入操作检查每个插入的元素是否与容器中已经存在的元素等价,如果是,则不插入元素,返回一个迭代器到这个现有元素(如果函数返回一个值 )。

对于允许重复元素的类似容器,请参阅 multiset

在内部,集合容器保持其所有元素按照其比较对象指定的标准进行排序。 元素始终按照此顺序插入到其各自的位置。

// set::insert (C++98)
#include <iostream>
#include <set>

int main()
{
    std::set<int> myset;
    std::set<int>::iterator it;
    std::pair<std::set<int>::iterator, bool> ret;

    // set some initial values:
    for (int i = 1; i <= 5; ++i) myset.insert(i * 10);    // set: 10 20 30 40 50

    ret = myset.insert(20);               // no new element inserted

    if (ret.second == false) it = ret.first;  // "it" now points to element 20

    myset.insert(it, 25);                 // max efficiency inserting
    myset.insert(it, 24);                 // max efficiency inserting
    myset.insert(it, 26);                 // no max efficiency inserting

    int myints[] = { 5, 10, 15 };              // 10 already in set, not inserted
    myset.insert(myints, myints + 3);

    std::cout << "myset contains:";
    for (it = myset.begin(); it != myset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}

输出

myset contains: 5 10 15 20 24 25 26 30 40 50

删除元素

std::set::erase

(1)	iterator  erase (const_iterator position);
(2)	size_type erase (const value_type& val);
(3)	iterator  erase (const_iterator first, const_iterator last);

从集合容器中删除单个元素或一系列元素([first,last)

// erasing from set
#include <iostream>
#include <set>

int main()
{
    std::set<int> myset;
    std::set<int>::iterator it;

    // insert some values:
    for (int i = 1; i < 10; i++) myset.insert(i * 10);  // 10 20 30 40 50 60 70 80 90

    it = myset.begin();
    ++it;                                         // "it" points now to 20

    myset.erase(it);

    myset.erase(40);

    it = myset.find(60);
    myset.erase(it, myset.end());

    std::cout << "myset contains:";
    for (it = myset.begin(); it != myset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}

输出

myset contains: 10 30 50

查找元素

std::set::find

const_iterator find (const value_type& val) const;
iterator       find (const value_type& val);

在容器中搜索与 val 等效的元素,如果找到则返回一个迭代器,否则返回一个迭代器set::end

// set::find
#include <iostream>
#include <set>

int main()
{
    std::set<int> myset;
    std::set<int>::iterator it;

    // set some initial values:
    for (int i = 1; i <= 5; i++) myset.insert(i * 10);    // set: 10 20 30 40 50

    it = myset.find(20);
    myset.erase(it);
    myset.erase(myset.find(40));

    std::cout << "myset contains:";
    for (it = myset.begin(); it != myset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}
myset contains: 10 30 50

模板类std::multiset

std::multiset

template < class T,                        // multiset::key_type/value_type
           class Compare = less<T>,        // multiset::key_compare/value_compare
           class Alloc = allocator<T> >    // multiset::allocator_type
           > class multiset;

多重集是按照特定顺序存储元素的容器,其中多个元素可以具有等效值。

在多重集中,元素的值也标识它(值本身就是键,类型为 T)。 多重集中元素的值不能在容器中修改一次(元素始终为 const),但可以从容器中插入或删除它们。

在内部,multiset 中的元素始终按照其内部比较对象(Compare 类型)指示的特定严格弱排序标准进行排序。

multiset 容器通常比 unordered_multiset 容器通过键访问单个元素要慢,但它们允许基于它们的顺序对子集进行直接迭代。

多重集通常实现为二叉搜索树。

创建C++ multiset容器

std::multiset::multiset

empty (1)	explicit multiset (const key_compare& comp = key_compare(),
                   const allocator_type& alloc = allocator_type());
			explicit multiset (const allocator_type& alloc);
range (2)	template <class InputIterator>
  			multiset (InputIterator first, InputIterator last,
            const key_compare& comp = key_compare(),
            const allocator_type& = allocator_type());
copy (3)	multiset (const multiset& x);
			multiset (const multiset& x, const allocator_type& alloc);
move (4)	multiset (multiset&& x);
			multiset (multiset&& x, const allocator_type& alloc);
initializer list (5)	
			multiset (initializer_list<value_type> il,
			const key_compare& comp = key_compare(),
			const allocator_type& alloc = allocator_type());
// constructing multisets
#include <iostream>
#include <set>

bool fncomp(int lhs, int rhs) { return lhs < rhs; }

struct classcomp {
	bool operator() (const int& lhs, const int& rhs) const
	{
		return lhs < rhs;
	}
};

int main()
{
	std::multiset<int> first;                            // empty multiset of ints

	int myints[] = { 10, 20, 30, 20, 20 };
	std::multiset<int> second(myints, myints + 5);       // pointers used as iterators

	std::multiset<int> third(second);                    // a copy of second

	std::multiset<int> fourth(second.begin(), second.end());  // iterator ctor.

	std::multiset<int, classcomp> fifth;                 // class as Compare

	bool(*fn_pt)(int, int) = fncomp;
	std::multiset<int, bool(*)(int, int)> sixth(fn_pt);  // function pointer as Compare

	std::cout << "fourth contains:";
	for (const int& x : fourth) std::cout << " " << x;
	std::cout << std::endl;

	return 0;
}

输出

fourth contains: 10 20 20 20 30

插入元素

std::multiset::insert

single element (1)	iterator insert (const value_type& val);
					iterator insert (value_type&& val);
with hint (2)	iterator insert (const_iterator position, const value_type& val);
				iterator insert (const_iterator position, value_type&& val);
range (3)	template <class InputIterator>
  			void insert (InputIterator first, InputIterator last);
initializer list (4)	void insert (initializer_list<value_type> il);

通过插入新元素来扩展容器,通过插入的元素数量有效地增加容器大小。

在内部,多重集容器保持其所有元素按照其比较对象指定的标准进行排序。 元素始终按照此顺序插入到其各自的位置。

// multiset::insert (C++98)
#include <iostream>
#include <set>

int main()
{
    std::multiset<int> mymultiset;
    std::multiset<int>::iterator it;

    // set some initial values:
    for (int i = 1; i <= 5; i++) mymultiset.insert(i * 10);  // 10 20 30 40 50

    it = mymultiset.insert(25);

    it = mymultiset.insert(it, 27);    // max efficiency inserting
    it = mymultiset.insert(it, 29);    // max efficiency inserting
    it = mymultiset.insert(it, 24);    // no max efficiency inserting (24<29)

    int myints[] = { 5, 10, 15 };
    mymultiset.insert(myints, myints + 3);

    std::cout << "mymultiset contains:";
    for (it = mymultiset.begin(); it != mymultiset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}

输出

mymultiset contains: 5 10 10 15 20 24 25 27 29 30 40 50

删除元素

std::multiset::erase

(1)	iterator  erase (const_iterator position);
(2)	size_type erase (const value_type& val);
(3)	iterator  erase (const_iterator first, const_iterator last);
// erasing from multiset
#include <iostream>
#include <set>

int main()
{
    std::multiset<int> mymultiset;
    std::multiset<int>::iterator it;

    // insert some values:
    mymultiset.insert(40);                            // 40
    for (int i = 1; i < 7; i++) mymultiset.insert(i * 10);   // 10 20 30 40 40 50 60

    it = mymultiset.begin();
    it++;                                              //    ^

    mymultiset.erase(it);                             // 10 30 40 40 50 60

    mymultiset.erase(40);                             // 10 30 50 60

    it = mymultiset.find(50);
    mymultiset.erase(it, mymultiset.end());         // 10 30

    std::cout << "mymultiset contains:";
    for (it = mymultiset.begin(); it != mymultiset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}

输出

mymultiset contains: 10 30

查找元素

std::multiset::find

const_iterator find (const value_type& val) const;
iterator       find (const value_type& val);

在容器中搜索与 val 等效的元素,如果找到则返回一个迭代器,否则返回一个迭代器到 multiset::end

请注意,此函数将迭代器返回到单个元素(可能是多个等效元素)。 要获取等效元素的整个范围,请参阅 multiset::equal_range

// multiset::find
#include <iostream>
#include <set>

int main()
{
    std::multiset<int> mymultiset;
    std::multiset<int>::iterator it;

    // set some initial values:
    for (int i = 1; i <= 5; i++) mymultiset.insert(i * 10);   // 10 20 30 40 50

    it = mymultiset.find(20);
    mymultiset.erase(it);
    mymultiset.erase(mymultiset.find(40));

    std::cout << "mymultiset contains:";
    for (it = mymultiset.begin(); it != mymultiset.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';

    return 0;
}
mymultiset contains: 10 30 50

模板类std::map

std::map

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;

映射是关联容器,它按照特定顺序存储由键值和映射值组合形成的元素。

在映射中,键值通常用于对元素进行排序和唯一标识,而映射值存储与该键关联的内容。 keymapped value 的类型可能不同,并在成员类型 value_type 中组合在一起,这是一种结合了两者的 pair 类型:

typedef pair<const Key, T> value_type;

在内部,map 中的元素始终按照其内部比较对象(Compare 类型)指示的特定严格弱排序标准按其键排序。

map容器通常比 unordered_map 容器通过键访问单个元素要慢,但它们允许基于它们的顺序对子集进行直接迭代。

映射中的映射值可以通过使用括号运算符 (operator[]) 的相应键直接访问。

映射通常实现为二叉搜索树

创建C++ map容器

std::map::map

empty (1)	explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
			explicit map (const allocator_type& alloc);
range (2)	template <class InputIterator>
			map (InputIterator first, InputIterator last,
			const key_compare& comp = key_compare(),
			const allocator_type& = allocator_type());
copy (3)	map (const map& x);
			map (const map& x, const allocator_type& alloc);
move (4)	map (map&& x);
			map (map&& x, const allocator_type& alloc);
initializer list (5)	
			map (initializer_list<value_type> il,
     		const key_compare& comp = key_compare(),
     		const allocator_type& alloc = allocator_type());
// constructing maps
#include <iostream>
#include <map>

bool fncomp(char lhs, char rhs) { return lhs < rhs; }

struct classcomp {
	bool operator() (const char& lhs, const char& rhs) const
	{
		return lhs < rhs;
	}
};

int main()
{
	std::map<char, int> first;

	first['a'] = 10;
	first['b'] = 30;
	first['c'] = 50;
	first['d'] = 70;

	std::map<char, int> second(first.begin(), first.end());

	std::map<char, int> third(second);

	std::map<char, int, classcomp> fourth;                 // class as Compare

	bool(*fn_pt)(char, char) = fncomp;
	std::map<char, int, bool(*)(char, char)> fifth(fn_pt); // function pointer as Compare

	// showing contents:
	std::cout << "third contains:\n";
	for (auto it = third.begin(); it != third.end(); ++it)
		std::cout << it->first << " => " << it->second << '\n';
	return 0;
}

输出

third contains:
a => 10
b => 30
c => 50
d => 70

插入元素

std::map::insert

single element (1)	pair<iterator,bool> insert (const value_type& val);
					template <class P> pair<iterator,bool> insert (P&& val);
with hint (2)	iterator insert (const_iterator position, const value_type& val);
				template <class P> iterator insert (const_iterator position, P&& val);
range (3)	template <class InputIterator>
  			void insert (InputIterator first, InputIterator last);
initializer list (4)	void insert (initializer_list<value_type> il);

通过插入新元素来扩展容器,通过插入的元素数量有效地增加容器大小。

因为 map 中的元素键是唯一的,所以插入操作会检查每个插入的元素是否具有与容器中已有元素的键等效的键,如果是,则不插入该元素,并返回一个指向该现有元素的迭代器 ( 如果函数返回一个值)。

对于允许重复元素的类似容器,请参阅 multimap

在映射中插入元素的另一种方法是使用成员函数 map::operator[]

在内部,地图容器保持其所有元素按照其比较对象指定的标准按其键排序。 元素始终按照此顺序插入到其各自的位置。

// map::insert (C++98)
#include <iostream>
#include <map>

int main()
{
    std::map<char, int> mymap;

    // first insert function version (single parameter):
    mymap.insert(std::pair<char, int>('a', 100));
    mymap.insert(std::pair<char, int>('z', 200));

    std::pair<std::map<char, int>::iterator, bool> ret;
    ret = mymap.insert(std::pair<char, int>('z', 500));
    if (ret.second == false) {
        std::cout << "element 'z' already existed";
        std::cout << " with a value of " << ret.first->second << '\n';
    }

    // second insert function version (with hint position):
    std::map<char, int>::iterator it = mymap.begin();
    mymap.insert(it, std::pair<char, int>('b', 300));  // max efficiency inserting
    mymap.insert(it, std::pair<char, int>('c', 400));  // no max efficiency inserting

    // third insert function version (range insertion):
    std::map<char, int> anothermap;
    anothermap.insert(mymap.begin(), mymap.find('c'));

    // showing contents:
    std::cout << "mymap contains:\n";
    for (it = mymap.begin(); it != mymap.end(); ++it)
        std::cout << it->first << " => " << it->second << '\n';

    std::cout << "anothermap contains:\n";
    for (it = anothermap.begin(); it != anothermap.end(); ++it)
        std::cout << it->first << " => " << it->second << '\n';

    return 0;
}

输出

element 'z' already existed with a value of 200
mymap contains:
a => 100
b => 300
c => 400
z => 200
anothermap contains:
a => 100
b => 300

删除元素

std::map::erase

(1)	iterator  erase (const_iterator position);
(2)	size_type erase (const key_type& k);
(3)	iterator  erase (const_iterator first, const_iterator last);

从地图容器中删除单个元素或一系列元素([first,last)

// erasing from map
#include <iostream>
#include <map>

int main()
{
    std::map<char, int> mymap;
    std::map<char, int>::iterator it;

    // insert some values:
    mymap['a'] = 10;
    mymap['b'] = 20;
    mymap['c'] = 30;
    mymap['d'] = 40;
    mymap['e'] = 50;
    mymap['f'] = 60;

    it = mymap.find('b');
    mymap.erase(it);                   // erasing by iterator

    mymap.erase('c');                  // erasing by key

    it = mymap.find('e');
    mymap.erase(it, mymap.end());    // erasing by range

    // show content:
    for (it = mymap.begin(); it != mymap.end(); ++it)
        std::cout << it->first << " => " << it->second << '\n';

    return 0;
}
a => 10
d => 40

查找元素

std::map::find

      iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

在容器中搜索具有等效于 k 的键的元素,如果找到则返回一个迭代器,否则返回一个迭代器map::end

如果容器的比较对象反射性地返回 false(即,无论元素作为参数传递的顺序如何),则认为两个键是等效的。

// map::find
#include <iostream>
#include <map>

int main()
{
    std::map<char, int> mymap;
    std::map<char, int>::iterator it;

    mymap['a'] = 50;
    mymap['b'] = 100;
    mymap['c'] = 150;
    mymap['d'] = 200;

    it = mymap.find('b');
    if (it != mymap.end())
        mymap.erase(it);

    // print content:
    std::cout << "elements in mymap:" << '\n';
    std::cout << "a => " << mymap.find('a')->second << '\n';
    std::cout << "c => " << mymap.find('c')->second << '\n';
    std::cout << "d => " << mymap.find('d')->second << '\n';

    return 0;
}

输出

elements in mymap:
a => 50
c => 150
d => 200

按键访问元素

std::map::operator[]

mapped_type& operator[] (const key_type& k);
mapped_type& operator[] (key_type&& k);

如果 k 匹配容器中元素的键,则该函数返回对其映射值的引用。

如果 k 不匹配容器中任何元素的键,则该函数使用该键插入一个新元素并返回对其映射值的引用。 请注意,这总是将容器大小增加一,即使没有为元素分配映射值(该元素是使用其默认构造函数构造的)。

类似的成员函数 map::at 在具有键的元素存在时具有相同的行为,但在不存在时抛出异常。

// accessing mapped values
#include <iostream>
#include <map>
#include <string>

int main()
{
	std::map<char, std::string> mymap;

	mymap['a'] = "an element";
	mymap['b'] = "another element";
	mymap['c'] = mymap['b'];

	std::cout << "mymap['a'] is " << mymap['a'] << '\n';
	std::cout << "mymap['b'] is " << mymap['b'] << '\n';
	std::cout << "mymap['c'] is " << mymap['c'] << '\n';
	std::cout << "mymap['d'] is " << mymap['d'] << '\n';

	std::cout << "mymap now contains " << mymap.size() << " elements.\n";

	return 0;
}
mymap['a'] is an element
mymap['b'] is another element
mymap['c'] is another element
mymap['d'] is
mymap now contains 4 elements.

模板类std::multimap

std::multimap

template < class Key,                                     // multimap::key_type
           class T,                                       // multimap::mapped_type
           class Compare = less<Key>,                     // multimap::key_compare
           class Alloc = allocator<pair<const Key,T> >    // multimap::allocator_type
           > class multimap;

多重映射是关联容器,它存储由键值和映射值组合形成的元素,遵循特定顺序,并且多个元素可以具有等效键。

在多映射中,键值通常用于对元素进行排序和唯一标识,而映射值存储与该键关联的内容。 keymapped value 的类型可能不同,并在成员类型 value_type 中组合在一起,这是一种结合了两者的 pair 类型:

typedef pair<const Key, T> value_type;

在内部,multimap 中的元素始终按照其内部比较对象(Compare 类型)指示的特定严格弱排序标准按其键排序。

multimap 容器通常比 unordered_multimap 容器通过它们的键访问单个元素要慢,但它们允许基于它们的顺序对子集进行直接迭代。

多重映射通常实现为二叉搜索树。

创建C++ multimap容器

std::multimap::multimap

empty (1)	explicit multimap (const key_compare& comp = key_compare(),
                   const allocator_type& alloc = allocator_type());
			explicit multimap (const allocator_type& alloc);
range (2)	template <class InputIterator>
  			multimap (InputIterator first, InputIterator last,
       		const key_compare& comp = key_compare(),
       		const allocator_type& = allocator_type());
copy (3)	multimap (const multimap& x);
			multimap (const multimap& x, const allocator_type& alloc);
move (4)	multimap (multimap&& x);
			multimap (multimap&& x, const allocator_type& alloc);
initializer list (5)	multimap (initializer_list<value_type> il,
     					const key_compare& comp = key_compare(),
     					const allocator_type& alloc = allocator_type());
// constructing multimaps
#include <iostream>
#include <map>

bool fncomp(char lhs, char rhs) { return lhs < rhs; }

struct classcomp {
	bool operator() (const char& lhs, const char& rhs) const
	{
		return lhs < rhs;
	}
};

int main()
{
	std::multimap<char, int> first;

	first.insert(std::pair<char, int>('a', 10));
	first.insert(std::pair<char, int>('b', 15));
	first.insert(std::pair<char, int>('b', 20));
	first.insert(std::pair<char, int>('c', 25));

	std::multimap<char, int> second(first.begin(), first.end());

	std::multimap<char, int> third(second);

	std::multimap<char, int, classcomp> fourth;                 // class as Compare

	bool(*fn_pt)(char, char) = fncomp;
	std::multimap<char, int, bool(*)(char, char)> fifth(fn_pt); // function pointer as comp

	// showing contents:
	std::cout << "third contains:\n";
	for (auto it = third.begin(); it != third.end(); ++it)
		std::cout << it->first << " => " << it->second << '\n';

	return 0;
}

输出

third contains:
a => 10
b => 15
b => 20
c => 25

插入元素

std::multimap

single element (1)	iterator insert (const value_type& val);
					template <class P> iterator insert (P&& val);
with hint (2)	iterator insert (const_iterator position, const value_type& val);
				template <class P> iterator insert (const_iterator position, P&& val);
range (3)	template <class InputIterator>
  			void insert (InputIterator first, InputIterator last);
initializer list (4)	void insert (initializer_list<value_type> il);
// multimap::insert (C++98)
#include <iostream>
#include <map>

int main()
{
    std::multimap<char, int> mymultimap;
    std::multimap<char, int>::iterator it;

    // first insert function version (single parameter):
    mymultimap.insert(std::pair<char, int>('a', 100));
    mymultimap.insert(std::pair<char, int>('z', 150));
    it = mymultimap.insert(std::pair<char, int>('b', 75));

    // second insert function version (with hint position):
    mymultimap.insert(it, std::pair<char, int>('c', 300));  // max efficiency inserting
    mymultimap.insert(it, std::pair<char, int>('z', 400));  // no max efficiency inserting

    // third insert function version (range insertion):
    std::multimap<char, int> anothermultimap;
    anothermultimap.insert(mymultimap.begin(), mymultimap.find('c'));

    // showing contents:
    std::cout << "mymultimap contains:\n";
    for (it = mymultimap.begin(); it != mymultimap.end(); ++it)
        std::cout << (*it).first << " => " << (*it).second << '\n';

    std::cout << "anothermultimap contains:\n";
    for (it = anothermultimap.begin(); it != anothermultimap.end(); ++it)
        std::cout << (*it).first << " => " << (*it).second << '\n';

    return 0;
}

输出

mymultimap contains:
a => 100
b => 75
c => 300
z => 400
z => 150
anothermultimap contains:
a => 100
b => 75

删除元素

std::multimap::erase

(1)	iterator  erase (const_iterator position);
(2)	size_type erase (const key_type& k);
(3)	iterator  erase (const_iterator first, const_iterator last);
// erasing from multimap
#include <iostream>
#include <map>

int main()
{
    std::multimap<char, int> mymultimap;

    // insert some values:
    mymultimap.insert(std::pair<char, int>('a', 10));
    mymultimap.insert(std::pair<char, int>('b', 20));
    mymultimap.insert(std::pair<char, int>('b', 30));
    mymultimap.insert(std::pair<char, int>('c', 40));
    mymultimap.insert(std::pair<char, int>('d', 50));
    mymultimap.insert(std::pair<char, int>('d', 60));
    mymultimap.insert(std::pair<char, int>('e', 70));
    mymultimap.insert(std::pair<char, int>('f', 80));

    std::multimap<char, int>::iterator it = mymultimap.find('b');

    mymultimap.erase(it);                     // erasing by iterator (1 element)

    mymultimap.erase('d');                    // erasing by key (2 elements)

    it = mymultimap.find('e');
    mymultimap.erase(it, mymultimap.end()); // erasing by range

    // show content:
    for (it = mymultimap.begin(); it != mymultimap.end(); ++it)
        std::cout << (*it).first << " => " << (*it).second << '\n';

    return 0;
}

输出

a => 10
b => 30
c => 40

查找元素

std::multimap::find

      iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
// multimap::find
#include <iostream>
#include <map>

int main()
{
	std::multimap<char, int> mymm;

	mymm.insert(std::make_pair('x', 10));
	mymm.insert(std::make_pair('y', 20));
	mymm.insert(std::make_pair('z', 30));
	mymm.insert(std::make_pair('z', 40));

	std::multimap<char, int>::iterator it = mymm.find('x');
	mymm.erase(it);
	mymm.erase(mymm.find('z'));

	// print content:
	std::cout << "elements in mymm:" << '\n';
	std::cout << "y => " << mymm.find('y')->second << '\n';
	std::cout << "z => " << mymm.find('z')->second << '\n';

	return 0;
}
elements in mymm:
y => 20
z => 40
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值