Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ map

#include <map>

// ===== CONSTRUCTOR

// map (char -> int)
std::map<char,int> map_int;
map_int['a']=10;
map_int['b']=30;

// map (int -> string)
std::map<int, std::string> map_string;
map_string[24] = "46";

// ===== EXISTENCE and SIZE

// map size
map_int.size() : size_type (int)

// search for 'a'
// map.find(key) : map.iterator
if(map_int.find('a') != map_int.end()) {/*...*/}
// map_iterator->first : type(key)
// map_iterator->second : type(value)

// check if the map is empty
map_int.empty() // : bool

// ===== SET

// just name and set 
// if the element already exists, the statement will override that value
map_int['a']=10;

// check before set
if(map_int.find('a') == map_int.end()) 
	map_int['a']=10;

// ===== DELETE

// first: iterator, then: erase(it)
auto it = map_int.find('a');
if(it != map_int.end())
	map_int.erase(it);

// or also, using keys
map_int.erase('a');
 
PREVIOUS NEXT
Tagged: #map
ADD COMMENT
Topic
Name
6+9 =