C + + Basics - map

summary

  • map is a typical Association container

    Associated containers are characterized by saving and accessing according to keywords. The underlying structure is realized by red black tree.

  • map or ordered container

    By defau lt, the standard library uses the < operator of keyword type to compare two keywords. Therefore, the type of keyword needs to define the < operator of "normal behavior".

    All elements are automatically sorted according to the key value of the element.

  • All elements in the map are pair

    The first element in pair is key, which serves as an index, and the second element is vaule.

  • No repetition

    Duplicate keyword elements in containers are not allowed in map.

establish

categorystatement
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());

Create an empty map

std::map<std::string, int> map_; // Create an empty map with key type of string and value type of int

see

empty

bool empty() const noexcept;

Check whether the map is empty. If the size is 0, it is true. It is recommended to use this function to check whether the map is, rather than compare size() == 0

size

size_type size() const noexcept;

size() returns the number of elements in the map.

lookup

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

Find out whether a keyword exists, and return the iterator corresponding to the element; There is no return map::end

std::map<std::string, int> map_;
const char *key = "test_key";
std::map<std::string, int>::iterator it = map_.find(key);
if (it != map_.end()) {
    // found.
} else {
    // not found.
}

be careful:

It is not recommended to use subscript to search. There is a serious side effect of subscript search: if the keyword is not in the map, the subscript operation will insert an element with a given keyword.

ergodic

Traversing the map through iterators

std::map<std::string, int> map_;
std::map<std::string, int>::iterator it;
for (it = map_.begin(); it != map_.end(); ++it) {
    printf("key: %s, value: %d\n", it->first.c_str(), it->second);
}

increase

insert

categorystatement
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);

Using make_pair

std::map<std::string, int> map_;
map_.insert(std::make_pair("key1", 1));

In C++11, the easiest way to create a pair is to initialize it with curly braces in the parameter list

std::map<std::string, int> map_;
map_.insert({"key1", 1});

be careful:

  • Cannot modify

    During insert, if the keyword already exists, no operation will be done, that is, the value corresponding to the keyword cannot be modified.

  • Return value

    Insert returns a pair. The first member of pair is an iterator, pointing to the element with the given keyword; The second member is a bool value indicating whether the element was successfully inserted or already exists in the container. If the keyword is already in the container, the insert does nothing, and the bool part of the return value is false. If the keyword does not exist, the element is inserted into the container and the bool value is true.

subscript

Insert element with subscript []

std::map<std::string, int> map_;
map_["key1"] = 1;

The process of subscript insertion is:

If the keyword exists in the container, update the original value with the latest value; If the keyword does not exist, insert the element.

delete

erase

iterator  erase (const_iterator position);
size_type erase (const key_type& k);
iterator  erase (const_iterator first, const_iterator last);\

Delete by keyword

std::map<std::string, int> map_;
const char *key = "key1";
map_.erase(key);

Delete by iterator

std::map<std::string, int> map_;
const char *key = "key1";
// Find whether the keyword exists
std::map<std::string, int>::iterator it = map_.find(key);
if (it != map_.end()) {
    map_.erase(it); // Delete if present
}

Delete by iterator scope

std::map<std::string, int> map_;
map_.erase(map_.begin(), map_.end()); // Delete entire map

clear

void clear() noexcept;

If the map is emptied and the resources are released, we need to study it again.

modify

Modify by subscript insertion

std::map<std::string, int> map_;
const char *key = "key1";
int value = 1;
... ...
// Judgment map_ Whether the contains a keyword. If it exists, it will be updated
std::map<std::string, int>::iterator it = map_.find(key);
if (it != map_.end()) {
    map_[key] = value; // Update the original value by subscript
}

reference resources

Tags: C++

Posted on Tue, 30 Nov 2021 23:22:54 -0500 by The1PatO