1. 程式人生 > >stl map 基本用法

stl map 基本用法

建構函式

map<int, string> m_map;
map<int, string> m_map = {make_pair(1, "test")};

插入

m_map[0] = "test";
m_map.insert(pair<int,string>(1, "test"));
m_map.insert(map<int, string>::value_type (1, "test"));

刪除

   m_map.erase(1);

   map<int, string>::iterator iter;
   for(iter = m_map.begin(); iter != m_map.end(); iter++) {
      if
(iter->second == "test") m_map.erase(iter); }

查詢

map<char, string> b_map = {make_pair('a', "23333")};
cout << b_map['a'] << endl;

遍歷

for(iter = m_map.begin(); iter != m_map.end(); iter++) {
    cout << iter->first << " " << iter->second << endl;
}
for(int i = 0; i < m_map.size(); i++) { cout << m_map[i] << endl; }