GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C++ Library - <flat_map>
Selected Reading
C++ Library - <flat_map>The <flat_map> header is part of the containers library, provides various functions as a sorted associative container for low memory usage, fast access, and store the key-value pairs with unique keys. The flat_map container acts as a wrapper to the two underlying containers, and gives a single approach by combining the advantages in both ordered and contiguous storage. Including <flat_map> HeaderTo include the <flat_map> header in your C++ program, you can use the following syntax. #include <flat_map> Functions of <flat_map> HeaderBelow is list of all functions from <flat_map> header. Element AccessThe element access functions provide mechanisms like retrieving or modifying elements in the container. This can be done using indices and keys.
Accessing the ElementIn the following example we are going to use, operator[] for accessing or insertion of an element. #include <iostream>
#include <flat_map>
int main() {
std::flat_map<int, std::string> myMap;
myMap[1] = "One";
myMap[2] = "Two";
std::cout << "Element at key 1: " << myMap[1] << std::endl;
return 0;
}
OutputIf we run the above code it will generate the following output Element at key 1: One IteratorsIterators provide a way to traverse or manipulate the elements of a container. In flat_map, iterators are used to move from one key-value pair to another.
Retrieving the IteratorIn the following example we are going to use, begin() to get an iterator at the beginning of the container. #include <iostream>
#include <flat_map>
int main() {
std::flat_map<int, std::string> myMap = {{1, "One"}, {2, "Two"}};
auto it = myMap.begin();
std::cout << "First element: " << it->t << " -> " << it->second << std::endl;
return 0;
}
OutputIf we run the above code it will generate the following output First element: 1 -> One CapacityThe capacity functions are used to check and provide information about the containers size and capacity.
Checking Existence of ContainerIn the following example we are going to use, empty() to chech whether the container is empty. #include <iostream>
#include <flat_map>
int main() {
std::flat_map<int, std::string> myMap;
if (myMap.empty()) {
std::cout << "The flat_map is empty." << std::endl;
} else {
std::cout << "The flat_map is not empty." << std::endl;
}
return 0;
}
OutputIf we run the above code it will generate the following output The flat_map is empty. ModifiersModifier Functions change the contents of the container by inserting, replacing, or deleting elements.
Inserting an Element In-PlaceIn the following example we are going to use, emplace() to insert an element in a specific place. #include <iostream>
#include <flat_map>
int main() {
std::flat_map<int, std::string> myMap;
myMap.emplace(1, "One");
std::cout << "Element emplaced: " << myMap[1] << std::endl;
return 0;
}
OutputIf we run the above code it will generate the following output Element emplaced: One Lookup
Finding an Element Using KeyIn the following example we are going to use, find() to find an element by key. #include <iostream>
#include <flat_map>
int main() {
std::flat_map<int, std::string> myMap = {{1, "One"}, {2, "Two"}};
auto it = myMap.find(1);
if (it != myMap.end()) {
std::cout << "Found element: " << it->first << " -> " << it->second << std::endl;
} else {
std::cout << "Element not found." << std::endl;
}
return 0;
}
OutputIf we run the above code it will generate the following output Found element: 1 -> One ObserversThe observer functions provide information about how the container operates, particularly regarding key comparison and access to underlying data structures.
Comparing the KeysIn the following example we are going to use, key_comp() to compare the keys. #include <iostream>
#include <flat_map>
int main() {
std::flat_map<int, std::string> myMap = {{1, "One"}, {2, "Two"}};
auto comp = myMap.key_comp();
if (comp(1, 2)) {
std::cout << "Key 1 is less than Key 2." << std::endl;
} else {
std::cout << "Key 1 is not less than Key 2." << std::endl;
}
return 0;
}
OutputIf we run the above code it will generate the following output Key 1 is less than Key 2. Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |