Map is a part of the container which stores values by associating it with a key value. The key value is the case of a set is calculated internally but, in the case of a map, the user has to insert the value along with the key. The key should be unique and if the key is repeated, then the value is neglected. All the insertion, deletion and find are of logarithmic time complexity.
Header file used:
#include<boost/container/flat_map.hpp>
Member function of map:
insert(key, value) - Inserts an element into the container.
empty() - Returns a Boolean value true if the container is empty.
emplace(key, value) - It provides an in-place insertion of the object.
begin() - Returns an iterator to the first element of the container.
end() -returns an iterator to the end of the container. It should be noted that it is the end of the container and not the last element.
rbegin() - Returns an iterator to the first element of the reversed container. It reverses the container and gives the last element he first element.
rend() - returns an iterator to the end of the reversed container.
size() - returns the no of elements in the container.
max_size() - returns the largest possible size of the container.
shrink_to_fit () - Tries to deallocate the excess memory allocated. The size of the container remains unchanged but the memory allocated would have been reduced.
nth(position) - returns an iterator to the mentioned position.
index_of(iterator) - returns the index of the position the iterator is pointing to.
erase(value) - Removes the value from the container.
find(value) - Gives an iterator to the value if present if not points to the end of the container.
clear() - Removes all the values of the container.
reserve(value) - Increases the capacity by the mentioned value if the current size is less than the specified value.
PROGRAM:
int main()
{
boost::container::flat_map<std::string, int> map;
map.insert(std::make_pair(std::string("Ram"),0));//using insert()
map.emplace("Raghav", 0);//using emplace()
map["Singh"]=2;//using direct insertion operator
map["John"]=4;
map.insert(std::make_pair(std::string("Ram"),0));//Duplicate value neglected
std::cout <<"Max no of elements that can be inserted: "<< map.max_size() <<std::endl;
std::cout <<"No of elements present: "<< map.size() <<std::endl;
std::cout<<"\nThe elements present: "<<std::endl;
for (boost::container::flat_map<std::string,int>::iterator it = map.begin();it != map.end(); it++ )
std::cout <<"Key:"<< it->first << " ,value: " << it->second << '\n';
std::cout<<std::endl;
std::string i="Ram";
map.find(i)!=map.end() ? std::cout<<i<<" is present\n" : std::cout<<i<<" is not present\n";
map.erase("John");
auto it = map.find("John");
std::cout<<"The index of John is "<<map.index_of(it)<<std::endl;
std::cout<<"\nAfter erasing John, the elements are "<<std::endl;
for (boost::container::flat_map<std::string,int>::iterator it = map.begin();it != map.end(); it++ )
std::cout <<"Key:"<< it->first << " ,value: " << it->second << "\n";
std::cout<<std::endl;
std::cout<<"The element at the third position is\n";
it =map.nth(2);
std::cout <<"Key:"<< it->first << " ,value: " << it->second << '\n';
}
OUTPUT:
Max no of elements that can be inserted: 461168601842738790
No of elements present: 4
The elements present:
Key:John,value: 4
Key:Raghav,value: 0
Key:Ram,value: 0
Key:Singh,value: 2
Ram is present
The index of John is 3
After erasing John, the elements are
Key:Raghav ,value: 0
Key:Ram,value: 0
Key:Singh,value: 2
The element at the third position is
Key:Singh,value: 2
Name | Views | Likes |
---|---|---|
C++ boost::circular_buffer | 4825 | 0 |
Implementing LRU Cache | 7704 | 0 |
C++ boost::container::string | 1013 | 0 |
c++ boost::container::slist | 633 | 0 |
C++ unordered_map | 581 | 0 |
C++ boost::container::flat_map | 5374 | 0 |
c++ boost::container::vector | 2461 | 0 |
C++ boost::container::stable_vector | 1293 | 0 |
C++ boost::multi_map | 2034 | 0 |
C++ boost::unordered_set | 2351 | 0 |
C++ boost::container::list | 1408 | 0 |
C++ boost::container::falt_multiset | 467 | 0 |
boost::container::string | 82 | 8 |
C++boost::container::string | 54 | 4 |
C++ boost::unordered_multiset | 580 | 0 |
C++ boost::container::static_vector | 3513 | 0 |
C++ boost::container::flat_set | 3350 | 0 |
Comments