Answers for "c++ std map"

C++
9

map c++

#include <iostream>
#include <string>
#include <map>

using std::string;
using std::map;

int main() {
	// A new map
  	map<string, int> myMap = { // This equals sign is optional
		{"one", 1},
    	{"two", 2},
    	{"three", 3}
  	};

  	// Print map contents with a range-based for loop
    // (works in C++11 or higher)
  	for (auto& iter: myMap) {
    	std::cout << iter.first << ": " << iter.second << std::endl;  
  	}
}
Posted by: Guest on August-22-2020
1

map c++

//Since c++17
for (auto& [key, value]: myMap) {
    cout << key << " has value " << value << endl;
}
//Since c++11
for (auto& kv : myMap) {
    cout << kv.first << " has value " << kv.second << endl;
}
Posted by: Guest on April-30-2021

Browse Popular Code Answers by Language