map get function c++ code example
Example 1: get map values c++
template<typename TK, typename TV>
std::vector<TV> extract_values(std::map<TK, TV> const& input_map) {
std::vector<TV> retval;
for (auto const& element : input_map) {
retval.push_back(element.second);
}
return retval;
}
Example 2: map c++
#include <iostream>
#include <string>
#include <map>
using std::string;
using std::map;
int main() {
map<string, int> myMap = {
{"one", 1},
{"two", 2},
{"three", 3}
};
for (auto& iter: myMap) {
std::cout << iter.first << ": " << iter.second << std::endl;
}
}
Example 3: map in cpp
#include <map>
int main(){
std::map <int,int> myMap;
myMap[0] = 1;
myMap[1] = 2;
myMap[2] = 3;
myMap[3] = 4;
}