# map

就是一個對應表的概念,一對一的形式,跨越型態的限制

# 標頭檔

#include<map>

# 構造器 && 初始化

  1. 默認構造器: empty container constructor
  2. 範圍構造器: range constructor
  3. 複製構造器: copy constructor
#include <iostream>
#include <map>
struct classcomp {
    bool operator() (const char& lhs, const char& rhs) const
    {return lhs<rhs;}
};
int main (){
    map<char,int> first; // 默認構造器
    first['a']=10;
    first['b']=30;
    first['c']=50;
    first['d']=70;
    map<char,int> second (first.begin(),first.end()); // 範圍構造器
    map<char,int> third (second);   // 複製構造器
    map<char,int,classcomp> fourth; // 有自訂比較函式的構造器
}

# 下標操作

map 最常使用的方法就只是直接使用 mp [ A ] = B 的方式互相對應

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main (){
    map<char,string> mymap;
    mymap['a']="an element";
    mymap['b']="another element";
    mymap['c']=mymap['b'];
    cout << "mymap['a'] is " << mymap['a'] << '\n';
    cout << "mymap['b'] is " << mymap['b'] << '\n';
    cout << "mymap['c'] is " << mymap['c'] << '\n';
    cout << "mymap['d'] is " << mymap['d'] << '\n';
    cout << "mymap now contains " << mymap.size() << " elements.\n";
}

# insert

重載函數形参
插入單個值pair<iterator,bool> insert (const value_type& val);
有位置提示iterator insert (const_iterator position, const value_type& val);
範圍templatevoid insert (InputIterator first, InputIterator last);
#include <iostream>
#include <map>
using namespace std;
void print(map<char,int>tmp){
    for (auto it : tmp)
        cout << it.first << " => " << it.second << '\n';
}
int main (){
    map<char,int> mymap;
    // 單個值插入
    mymap.insert ( pair<char,int>('a',100) );
    mymap.insert ( pair<char,int>('z',200) );
    pair<map<char,int>::iterator,bool> ret;
    ret = mymap.insert ( pair<char,int>('z',500) );
    if (ret.second==false) {
        cout << "元素 'z' 已經存在了";
        cout << " 他的值為: " << ret.first->second << '\n';
    }
    // 有位置提示的插入
    map<char,int>::iterator it = mymap.begin();
    mymap.insert (it, pair<char,int>('b',300));  // 最有效率的插入
    mymap.insert (it, pair<char,int>('c',400));  // 比較沒有效率的插入
    // 範圍型插入
    map<char,int> anothermap;
    anothermap.insert(mymap.begin(),mymap.find('c'));
    // 顯示內容物
    cout << "mymap contains:\n";
    print(mymap);
    cout << "anothermap contains:\n";
    print(anothermap);
}

輸出

元素 'z' 已經存在了,他的值為: 200
mymap contains:
a => 100
b => 300
c => 400
z => 200
anothermap contains:
a => 100
b => 300

# erase

map 刪除已存元素的方式,常見共有三種。

#include <iostream>
#include <map>
using namespace std;
int main ()
{
    map<char,int> mymap;
    map<char,int>::iterator it;
    mymap['a']=10;
    mymap['b']=20;
    mymap['c']=30;
    mymap['d']=40;
    mymap['e']=50;
    mymap['f']=60;
    it=mymap.find('b');
    mymap.erase (it);                   // 用指標清除
    mymap.erase ('c');                  // 用 key 值清除
    it=mymap.find ('e');
    mymap.erase ( it, mymap.end() );    // 範圍型清除
    // 展示
    print(mymap);
    return 0;
}

輸出

a => 10
d => 40

# find

尋找 map 中某一個 key 值的位置,如果找到的位置為 mp.end() 代表尚未找到

#include <iostream>
#include <map>
using namespace std;
int main (){
    map<char,int> mymap;
    map<char,int>::iterator it;
    mymap['a']=50;
    mymap['b']=100;
    mymap['c']=150;
    mymap['d']=200;
    it = mymap.find('b');
    if (it != mymap.end())
        mymap.erase (it);
    cout << "elements in mymap:" << '\n';
    cout << "a => " << mymap.find('a')->second << '\n';
    cout << "c => " << mymap.find('c')->second << '\n';
    cout << "d => " << mymap.find('d')->second << '\n';
    return 0;
}

輸出

elements in mymap:
a => 50
c => 150
d => 200

# count

判斷 map 中,key 值是否已出現

#include <iostream>
#include <map>
using namespace std;
int main (){
  map<char,int> mymap;
  char c;
  mymap ['a']=101;
  mymap ['c']=202;
  mymap ['f']=303;
  for (c='a'; c<='g'; c++){
    cout << c;
    if (mymap.count(c))
      cout << " is an element of mymap.\n";
    else 
      cout << " is not an element of mymap.\n";
  }
  return 0;
}

輸出

a is an element of mymap.
b is not an element of mymap.
c is an element of mymap.
d is not an element of mymap.
e is not an element of mymap.
f is an element of mymap.
g is not an element of mymap.

# clear

清除整個 map 的工具

#include <iostream>
#include <map>
using namespace std;
int main (){
    map<char,int> mymap;
    mymap['x']=100;
    mymap['y']=200;
    mymap['z']=300;
    cout << "mymap contains:\n";
    print(mymap);
    mymap.clear();
    mymap['a']=1101;
    mymap['b']=2202;
    cout << "mymap contains:\n";
    print(mymap);`
}

輸出

mymap contains:
x => 100
y => 200
z => 300
mymap contains:
a => 1101
b => 2202

# empty

如果 map 為空, empty 返回 true ;

# size

返回 map 容器元素個數

# multimap

就是一個對應表的概念,一對多的形式,跨越型態的限制

# 標頭檔

#include<map>

# 使用

map 的操作大部分相同,但不能使用 下標[] 功能,插入值僅能用 insert

#include <map>
#include <string>
#include <iostream>
using namespace std;
void print(multimap<int,string>tmp){
    for (auto it : tmp)
        cout << it.first << " => " << it.second << '\n';
}
int main(){
    multimap<int, string> mapStudent;
    mapStudent.insert(pair<int, string>(101, "林小明"));
    mapStudent.insert(pair<int, string>(101, "王大明"));/// 一對多
    mapStudent.insert(pair<int, string>(102, "張大同"));
    mapStudent.insert(pair<int, string>(102, "葉哈囉"));
    mapStudent.insert(pair<int, string>(103, "李小龍"));
    mapStudent.insert(pair<int, string>(103, "陳小山"));
    mapStudent.insert(pair<int, string>(103, "黃中間"));
    cout << "mapStudent.size() is " << mapStudent.size() << endl << endl;
    print(mapStudent);
}

輸出

mapStudent.size() is 7

101 => 林小明
101 => 王大明
102 => 張大同
102 => 葉哈囉
103 => 李小龍
103 => 陳小山
103 => 黃中間
更新於 閱讀次數

用實際行動犒賞爆肝的我😀

Zrn Ye LinePay

LinePay