Basic operations of map in C++

  • 2020-05-19 05:20:34
  • OfStack

1. Introduction to map

map is a class 1 associative container. It is characterized by the fact that adding and removing nodes has little effect on the iterator and has no effect on any other nodes except the operation node. For iterators, you can modify the real value, not key.

2. Functions of map

Automatically creates a corresponding for Key-value. key and value can be any type you want.

According to the key value, find the records quickly. The complexity of the search is basically Log(N). If there are 1000 records, find the records 10 times at most, and 1,000,000 records, find the records 20 times at most.

Quickly insert the Key-Value record.

Fast delete record

Modify the value record according to Key.

Traverse all records.

3. Use map

Use map to include the header file where the map class resides

#include < map > // note that the STL header file has no extension.h

map object is a template class, which requires two template parameters: keyword and storage object:


std:map<int, string> personnel;

This defines a pointer that USES int as an index and has an associated pointer to string.

For ease of use, you can define the type of the template class 1,


typedef map<int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;

4. Insert an element in map

Changing the entries in map is easy because the [] operator has been overloaded by the map class


enumMap[1] = "One";
enumMap[2] = "Two";

.....

This is intuitive, but there is a performance problem. When inserting 2, first look for the item with the primary key of 2 in enumMap, but find nothing. Then insert 1 new object into enumMap, the key is 2, and the value is 1 empty string. After inserting, assign the string to "Two". This method assigns each value to the default and then to the displayed value, which is expensive if the element is a class object. We can avoid overhead by:


enumMap.insert(map<int, CString> :: value_type(2, "Two"))

5. Find and get the elements in map

The subscript operator gives the easiest way to get a value:


CString tmp = enumMap[2];

However, this is only true if there is an instance of the key in map, otherwise an instance is automatically inserted with the initialization value.

We can use the Find() and Count() methods to find out if a key exists.

The find() method is used to find whether a keyword entry is included in map. The parameter passed in is key to be found. The two members that need to be mentioned here are begin() and end(), representing the first entry and the last entry in the map object, respectively, and the two data types are iterator.


int nFindKey = 2; // You're looking for Key
// define 1 Item variables ( It's actually a pointer )
UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey);
if(it == enumMap.end()) {
// Did not find 
}
else {
// find 
}

The iterator data type obtained through the map object method is one std::pair object, including two data iterator- > first and iterator - > second represents the keyword and the stored data, respectively

6. Delete elements from map

Remove an entry from an map using erase()

The member method is defined as follows


iterator erase(iterator it); // through 1 Item object deleted  
iterator erase(iterator first, iterator last); // delete 1 A range of  
size_type erase(const Key& key); // Delete by keyword  
clear() Is equivalent to  enumMap.erase(enumMap.begin(), enumMap.end());

7. Basic operation functions of map:

C++ Maps is an associative container that contains "keyword/value" pairs
begin() returns the iterator pointing to the map header
clear() removes all elements
count() returns the number of occurrences of the specified element
empty() returns true if map is empty
end() returns an iterator that points to the end of map
equal_range() returns the iterator pair for the special entry
erase() removes an element
find() looks for an element
get_allocator() returns the configurator for map
insert() inserts the element
key_comp() returns the function that compares the element key
lower_bound() returns the key value > = the first position of a given element
max_size() returns the maximum number of elements that can be held
rbegin() returns a reverse iterator that points to the tail of map
rend() returns a reverse iterator that points to the map header
size() returns the number of elements in map
swap() swaps two map
upper_bound() returns the key value > The first position of a given element
value_comp() returns the function that compares the element value

Example:


// Through: 
map<string,CAgent>::iterator iter;
 for(iter = m_AgentClients.begin(); iter != m_AgentClients.end(); ++iter)
 {
  if(iter->first=="8001" {
    this->SendMsg(iter->second.pSocket,strMsg);//iter->first
  }
 }
// Looking for: 
map<string,CAgent>::iterator iter=m_AgentClients.find(strAgentName);
 if(iter!=m_AgentClients.end())// Have a namesake,  {
 }
 else // There is no {
 }
// Number of elements 
if (m_AgentClients.size()==0)
// delete 
map<string,CAgent>::iterator iter=m_AgentClients.find(pSocket->GetName());
 if(iter!=m_AgentClients.end())
 {
   m_AgentClients.erase(iter);// The list to remove 
 }

The above is the basic operation of map in C++ introduced to you by this site. I hope it can be of help to you. If you have any questions, please feel free to leave a message to me.


Related articles: