cpp ► STL container - > sort container - > map

describe

std::map

template <class Key, class T, class Compare = less<Key>,
          class Alloc = allocator<pair<const Key, T>>>
class map;

map is an association container used to store elements composed of key value pairs in a specific order.

In a map, the key value is usually used to sort and uniquely identify elements, and the mapped value stores the content associated with the key. The types of key and value may be different, and through the member type value_type. This is a pair type that combines the following two: typedef pair < const key, t > value_type;

Internally, the elements in the map are always sorted by their key according to the specific strict weak sorting criteria indicated by their internal comparison object (Compare type).

map is usually better than unordered_ Maps are slow to access individual elements through their key s, but they allow direct iteration of subsets based on their order.

The mapping values in the map can be accessed directly through the corresponding key using the bracket operator (operator []).

map is usually used as a typical implementation of binary search tree.

Container properties
  • Associative
    The element in the associated container is referenced by its key, not by its absolute position in the container.
  • Ordered
    The elements in the container always follow a strict order. All inserted elements are given a position in this order.
  • Map
    Each element associates a key with a value: the key is used to identify the element whose main content is value.
  • Unique keys
    No two elements in a container can have equal key s.
  • Allocator-aware
    Containers use allocator objects to dynamically process their storage requirements.
Member types
Member typedefinitionremarks
key_typeFirst template parameter (Key)
mapped_typeSecond template parameter
value_typepair<const key_type, mapped_type>
key_compareEvery three template parameters (Compare)The default is: less < key_ type>
value_compareNested function class for comparing elementsSee: value_comp
iteratorOne for value_ Bidirectional iterator for typeConvertible to constant iterator
Member functions

Constructor:

// 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
	return 0;
}
Observers
  • key_comp
  • value_comp
    value_compare value_comp() const;
    The returned comparison object is the member type map::value_compare object, which is a nested class. Use the internal comparison object to generate the appropriate comparison function class. Its definition is the same as the following behavior:
template <class Key, class T, class Compare, class Alloc>
class map<Key, T, Compare, Alloc>::value_compare {
	friend class map;
protected:
	Compare comp;
	value_compare(Compare c) : comp(c) {}  // constructed with map's comparison object
public:
	typedef bool result_type;
	typedef value_type first_argument_type;
	typedef value_type second_argument_type;
	bool operator() (const value_type& x, const value_type& y) const {
		return comp(x.first, y.first);
	}
}
// map::value_comp
#include <iostream>
#include <map>
int main() {
	std::map<char, int> mymap;
	mymap['x'] = 1001;
	mymap['y'] = 2002;
	mymap['z'] = 3003;
	std::cout << "mymap contains:\n";
	std::pair<char, int> highest = *mymap.rbegin();// last element

	std::map<char, int>::iterator it = mymap.begin();
	do {
		std::cout << it->first << " => " << it->second << '\n';
	} while (mymap.value_comp()(*it++, highest));
	return 0;
}
mymap contains:
x => 1001
y => 2002
z => 3003

Keywords: C++

Added by justinede on Mon, 24 Jan 2022 18:56:13 +0200