Class map. Methods that change data in the container
Contents
- 1. Method clear(). Clear associative array
- 2. Method erase(). Remove element from associative container
- 3. Method insert(). Add new pair to array
- 4. Method swap(). Swap the contents of two containers
- 5. Operator function operator=(). Assign one container to another
- Related topics
Search other resources:
1. Method clear(). Clear associative array
The clear() method removes all the elements from the associative array. Method Declaration Syntax
void clear();
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method clear() - remove all elements from array // 1. Form an associative array map<string, int> Days; Days.insert(make_pair("January", 31)); Days.insert(make_pair("February", 28)); Days.insert(make_pair("March", 31)); Days.insert(make_pair("April", 30)); // 2. Display the size of array cout << Days.size() << endl; // 3. Clear array Days.clear(); // 4. Redisplay the array size cout << Days.size() << endl; }
Result
4 0
⇑
2. Method erase(). Remove element from associative container
The erase() method removes an element or group of elements in a variety of ways. The method has several overloaded implementations.
2.1. Removing a single element by a given key
A common implementation of the erase() method is to remove an element based on a given key. The syntax for declaring this implementation is as follows
inline size_t erase(const keyType &keyVal);
here
- keyType – type of key;
- keyVal – value of key.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method erase() - delete one element by key // 1. Form an associative array map<char, string> m1; m1.insert(make_pair('A', "Assembler")); m1.insert(make_pair('B', "Bash")); m1.insert(make_pair('C', "C++")); m1.insert(make_pair('D', "Dart")); m1.insert(make_pair('E', "Erlang")); m1.insert(make_pair('F', "Fortran")); // 2. Delete elements 'A' and 'D' m1.erase('A'); m1.erase('D'); // 3. Display the modified array map<char, string>::iterator it = m1.begin(); cout << "m1: " << endl; while (it != m1.end()) { cout << it->first << " : " << it->second << endl; it++; } }
Result
m1: B : Bash C : C++ E : Erlang F : Fortran
⇑
2.2. Removing a single element pointed to by an iterator
This implementation of the erase() method removes the single element pointed to by the iterator. The method declaration syntax is as follows
inline iterator erase(const_iterator Where);
here
- Where – constant iterator pointing to the element to be removed.
Example.
#include <iostream> #include <map> using namespace std; void main() { // The erase() method - remove one element based on key // 1. Form an associative array map<char, string> m1; m1.insert(make_pair('A', "Assembler")); m1.insert(make_pair('B', "Bash")); m1.insert(make_pair('C', "C++")); m1.insert(make_pair('D', "Dart")); m1.insert(make_pair('E', "Erlang")); m1.insert(make_pair('F', "Fortran")); // 2. Set iterator to pair 'F':"Fortran" map<char, string>::iterator it = m1.find('F'); // 3. Delete pair m1.erase(it); // 4. Display the array cout << "m1:" << endl; it = m1.begin(); while (it != m1.end()) { cout << it->first << " : " << it->second << endl; it++; } }
Result
m1: A : Assembler B : Bash C : C++ D : Dart E : Erlang
⇑
2.3. Removing multiple elements that are given by a range
Range is defined by start iterator and end iterator
inline iterator erase(const_iterator First, const_iterator Last);
here
- iterator, const_iterator – type of iterator and const iterator for the current container;
- First – element that defines the start of the deletion range;
- Last – element defining the end of the deletion range. The Last value points to the element after the last element to be removed.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method erase() - remove range of elements // 1. Form an associative array map<int, char> m1; m1.insert(make_pair(1, 'A')); m1.insert(make_pair(2, 'B')); m1.insert(make_pair(3, 'C')); m1.insert(make_pair(4, 'D')); m1.insert(make_pair(5, 'E')); // 2. Display the array cout << "m1: " << endl; for (int i = 0; i < m1.size(); i++) cout << i + 1 << " : " << m1[i + 1] << endl; // 3. Delete elements 2, 3 - letters 'B', 'C' // 3.1. Declare iterators map<int, char>::iterator itFirst; map<int, char>::iterator itLast; // 3.2. Set iterators to positions 2, 3 itFirst = m1.find(2); // set to key 2 itLast = m1.find(4); // set to key 4 that follows key 3 // 3.3. Method erase() - delete range m1.erase(itFirst, itLast); // 4. Reuse an array with an iterator // 4.1. Declare an additional iterator map<int, char>::iterator it = m1.begin(); // 4.2. Direct array output cout << "------------------" << endl; cout << "m1: " << endl; while (it != m1.end()) { cout << it->first << " : " << (*it).second << endl; it++; } }
Result
m1: 1 : A 2 : B 3 : C 4 : D 5 : E ------------------ m1: 1 : A 4 : D 5 : E
⇑
3. Method insert(). Add new pair to array
The insert() method adds a new pair to the associative container. The method has many overloaded implementations. Below are some of these implementations.
3.1. Insert elements based on std::initializer_list
With this implementation of the insert() method, an initialization list of type std::initializer_list is first created, then this list is passed as an argument to the insert() method. The method declaration looks like
inline void insert(initializer_list<pair<const kType, vType>> _llist);
here
- kType – type of key;
- vType – type of value.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method insert() - insert element. // Form an associative array based on the initialization list // 1. Create initialization list initializer_list <pair<const int, string>> ls = { make_pair(1, "One"), make_pair(2, "Two"), make_pair(3, "Three") }; // 2. Create an associative container map<int, string> m1; // 3. Insert initialization list into array m1.insert(ls); // 4. Вывести массив map<int, string>::iterator it = m1.begin(); // declare iterator cout << "m1" << endl; while (it != m1.end()) { cout << it->first << " : " << it->second << endl; it++; } }
Result
m1 1 : One 2 : Two 3 : Three
⇑
3.2. Add a pair to a container
To add a key:value pair to a container, use the following implementation of the insert() method
inline iterator insert(pair<kType, vType> &_Val);
here
- kType – type of key;
- vType – type of value;
- _Val – pair value.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method insert() - insert a pair into a container // 1. Declare an empty container of strings map<string, long int> population; // 2. Insert a pair - methods insert() + make_pair() population.insert(make_pair("China", 1.402E9L)); // 3. Insert a pair - use an object of type pair pair<string, long int> p("India", 1.38E9L); population.insert(p); // 4. Insert a pair - use the constructor of the pair type population.insert(pair<string, long int>("USA", 328E6L)); population.insert(pair<string, long int>("Ukraine", 52E6L)); // 5. Display the newly created container map<string, long int>::iterator it = population.begin(); cout << "population:" << endl; while (it != population.end()) { cout << it->first << " : " << it->second << endl; it++; } }
Result
population: China : 1402000000 India : 1380000000 USA : 328000000 Ukraine : 52000000
⇑
3.3. Inserting a range
This implementation of the insert() method allows you to insert an entire range of key:value pairs from a previously formed associative array. The range is specified by two iterators. The first iterator specifies the beginning of the range. The second iterator points to the element after the last element in the range to be inserted.
Syntax of method declaration
inline void insert(_Iter First, _Iter Last);
here
- _Iter – type of iterator;
- First, Last – iterators that specify a range of elements.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method insert() - insert a range of elements // 1. Declare associative container map<int, double> m1; // 2. Form the container for (int i = 0; i < 10; i++) m1.insert(make_pair(i, i * 1.1)); // 3. Declare another associative container map<int, double> m2; // 4. Form m2 based on m1, // add elements that are placed in the range [2; four] // 4.1. Declare iterators and set them to appropriate positions map<int, double>::iterator itFirst = m1.find(2); map<int, double>::iterator itLast = m1.find(5); // 4.2. Form m2 m2.insert(itFirst, itLast); // 5. Display m2 map<int, double>::iterator it = m2.begin(); cout << "m2:" << endl; while (it != m2.end()) { cout << it->first << " : " << it->second << endl; it++; } }
Result
m2: 2 : 2.2 3 : 3.3 4 : 4.4
⇑
4. Method swap(). Swap the contents of two containers
Using the swap() method, you can swap the contents of two containers. Syntax of method declaration
inline void swap(map<kType, vType> &_Right);
here
- kType – type of key;
- vType – type of value;
- _Right – an object of the map class whose elements are exchanged with the elements of the caller.
The kType key type and vType value type must match the key and value types of the object that calls the swap() method.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Method swap() - swap contents of containers // 1. Create two containers and fill them with values // 1.1. Container m1 map<int, double> m1; m1.insert(make_pair(1, 1.1)); m1.insert(make_pair(5, 5.5)); m1.insert(make_pair(9, 9.9)); // 1.2. Container m2 map<int, double> m2; m2.insert(make_pair(2, 2.2)); m2.insert(make_pair(4, 4.4)); // 2. Swap containers - swap() method m1.swap(m2); // 3. Display container m1 cout << "m1:" << endl; map<int, double>::iterator it = m1.begin(); while (it != m1.end()) { cout << it->first << " : " << it->second << endl; it++; } // 4. Display container m2 cout << "m2:" << endl; it = m2.begin(); while (it != m2.end()) { cout << it->first << " : " << it->second << endl; it++; } }
Result
m1: 2 : 2.2 4 : 4.4 m2: 1 : 1.1 5 : 5.5 9 : 9.9
⇑
5. Operator function operator=(). Assign one container to another
The operator function operator=() implements the usual operation of assigning one container to another. Function declaration syntax
inline map<kType, vType> operator=(const map<kType, vType> &_Right);
here
- kType – type of key;
- vType – type of value;
- _Right – the object placed to the right of the assignment operation (original container). When copying containers, a full copy of the _Right container is created. This means that after the assignment operator = is executed, both containers are located in different memory locations.
Example.
#include <iostream> #include <map> using namespace std; void main() { // Operator function operator=() - assign the contents of a container to another // 1. Create a container and fill it with values map<int, char> m1; m1.insert(make_pair(1, 'A')); m1.insert(make_pair(2, 'B')); m1.insert(make_pair(3, 'C')); // 2. Create another empty container map<int, char> m2; // 3. Print the size of another container cout << "m2.size = " << m2.size() << endl; // 3. Perform container assignment m2 = m1; // invoke the function operator=() // 4. Display another container map<int, char>::iterator it = m2.begin(); cout << "m2 => "; while (it != m2.end()) { cout << "(" << it->first << ":" << it->second << ") "; it++; } cout << endl; // 5. Append an element to another container and re-render it m2.insert(make_pair(4, 'D')); it = m2.begin(); cout << "---------------------" << endl; cout << "m2 => "; while (it != m2.end()) { cout << "(" << (*it).first << ":" << (*it).second << ") "; it++; } cout << endl; // 6. Display the first container cout << "m1 => "; it = m1.begin(); while (it != m1.end()) { cout << "(" << it->first << ":" << it->second << ") "; it++; } }
Result
m2.size = 0 m2 => (1:A) (2:B) (3:C) --------------------- m2 => (1:A) (2:B) (3:C) (4:D) m1 => (1:A) (2:B) (3:C)
As you can see from the result, a full copy of the m2 container was created in a different memory area.
⇑
Related topics
- Creating a container. Constructors. Creating pair. Overview of class functions and operators
- Methods that provide information from the container: at(), begin(), end(), count(), empty(), find(), size(), max_size(), operator[]()
⇑