版本 5f7ea40f340334e3de8519d3167755be96a7d3a0
Changes from 5f7ea40f340334e3de8519d3167755be96a7d3a0 to 88fc4f53d776d748f4fd2f06b533a6dbf8efc0b1
Vector
========
Introduction
--------
Vector是c++中陣列的替代型態,可以自主控制需要的記憶體。
Vector可以任意增加陣列長度及資料的數量,也可任意插入或刪除指定位置的資料。
基礎運用
--------
使用vector需要加入標頭檔<vector>
ex:
```c
#include <vector>
```
建立新vector的語法
vector<type> vector_name(amount,element);
ex:
```c
vector<int> acm(3,4);
vector<float> csie(5);
```
嘗試印出兩vector內容
output:
```c
4
4
4
acm : 4 4 4
csie : 0 0 0 0 0
```
也可以直接建立空白的vector
ex:
```c
vector<int> acm;
```
而欲讀取資料的話需給予他iterator
>注意:如果使用iterator時要符合格式.如果有格式不同的map則需另建一個新的iterator不可共用
若要讀取資料的話需要先定義iterator
```c
map <int, string>::iterator it;
map <int, char>::iterator it2;
vactor <int>::iterator it;
vactor <float>::iterator it2;
```
map在資料存取中.key的為.first, value的為.second
例如要讀取所有的map的資料
>.begin()是map第一筆資料的iterator
>.end()是最後一筆資料的iterator
```c
cout << "The datas stored in the map are: ";
for(it = m.begin(); it!= m.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
//.begin() stands for the first data stored in the map
```
或是如果只有要讀取"被你編號的某個" string
```c
int search;
cout << "Input the number ID of the string you are looking for : ";
cin >> search;
cout << m[search] << endl;
```
An fully executable program example :
```c
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, string> m;
map<int, string>::iterator it;
int n;
string s;
int search;
cout << "Please insert the datas you want to save into the map :" << endl;
cout << "(0 0 to quit)" << endl;
while(1)
{
cin >> n >> s;
if(n == 0 && s == "0")
break;
m[n] = s;
}
cout << "The datas stored in the map are : " << endl;
for(it = m.begin(); it!= m.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
cout << "Input the number ID of the string you are looking for : ";
cin >> search;
cout << m[search] << endl;
}
```
Output:
![](/map1.png)
Member functions
----------------------------
### .begin() / .end()
>.begin()是map第一筆資料的iterator
>.end()是最後一筆資料的iterator
### .size()
>可以用來檢測當前map裡有幾筆資料
例如
```c
map<int, string> m;
int n;
string s;
while(1)
{
cin >> n >> s;
if(n == 0 && s == "0")
break;
m[n] = s;
cout << "SIZE: " << m.size() << endl;
}
```
![](/map2.png)
### .clear() / .empty()
>.clear()是用來清空map裡所有資料的一個member function
>.empty()是用來察看map裡的資料是否是空的的member function (returns boolean)
```c
cout << "Is it empty ?" << endl;
if(m.empty())
cout << "Yes~" << endl;
else
cout << "No~~~~~" << endl;
cout << "Is it empty now?" << endl;
m.clear();
if(m.empty())
cout << "EMPTY!" << endl;
else
cout << "Nope" << endl;
```
![](/map3.png)
### .find()
.find()是用來搜尋某筆map資料的iterator
在取得該筆map資料的iterator後可對其進行修改value
但是key因為是read-only所以無法對齊進行修改
>格式: it = map.find(key);
Code example:
```c
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, string> m;
map<int, string>::iterator it;
int n;
string s;
int search;
m[1] = "apple";
m[2] = "banana";
m[3] = "water";
cout << "The datas stored in the map are : " << endl;
for(it = m.begin(); it!= m.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
cout << "Please insert the key of the value you want to change : ";
cin >> search;
it = m.find(search);
cout << "What do you want to change it into ?" << endl;
cin >> (*it).second;
cout << "Now the datas stored in the map are : " << endl;
for(it = m.begin(); it!= m.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
}
```
Output:
![](/map4.png)
### .erase()
.erase()是用來清除特定iterator所存的map的資料
格式(有兩種):
Erase by iterator
>配合上述的.find()使用
>it = map.find(key);
>map.erase(it);
Erase by key
>map.erase(key);
>Code example:
```c
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, string> m;
map<int, string>::iterator it;
int n;
string s;
int search;
m[1] = "apple";
m[2] = "banana";
m[3] = "water";
cout << "The datas stored in the map are : " << endl;
for(it = m.begin(); it!= m.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
cout << "Please insert the key of the value "<< endl;
cout << "you want to erase : ";
cin >> search;
m.erase(search);
cout << "Now the datas stored in the map are : " << endl;
for(it = m.begin(); it!= m.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
}
```
![](/map5.png)
A long code for you to look and execute for review(?)
--------
```c
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<int, string> m;
map<int, string>::iterator it;
void action_list()
{
cout << "Action List : (Input the number ID for the action)" << endl;
cout << "1. insert" << endl;
cout << "2. find" << endl;
cout << "3. size?" << endl;
cout << "4. erase" << endl;
cout << "5. clear" << endl;
cout << "6. emptied?" << endl;
cout << "7. print_map" << endl;
}
int main()
{
int n;
string s;
int action;
int search;
cout << "\033c";
cout << "Hi, this is a new map, you can now do several things to it" <<endl;
cout << "What do you want to do? " << endl;
action_list();
while(1)
{
cout << "Input action ID : ";
cin >> action;
if(action == 100)
{
cout << endl;
cout << "Program stopped" << endl;
break;
}
switch(action)
{
case 0:
cout << "\033c";
action_list();
break;
case 1:
cout << "\033c";
cout << "Please insert the datas you want to save into the map : (0 0 to quit)" << endl;
while(1)
{
cin >> n >> s;
if(n == 0 && s == "0")
break;
m[n] = s;
}
break;
case 2:
cout << "\033c";
cout << "Input the number ID of the string you are looking for : ";
cin >> search;
cout << m[search] << endl;
break;
case 3:
cout << "\033c";
cout << "There are " << m.size() << " datas now stored in the map" << endl;
break;
case 4:
cout << "\033c";
cout << "Please insert the key of the value you want to erase : ";
cin >> search;
m.erase(search);
break;
case 5:
cout << "\033c";
cout << "Map cleared to empty" << endl;
m.clear();
break;
case 6:
cout << "\033c";
if(m.empty())
cout << "Yeah ~ it's empty now..." << endl;
else
cout << "No ~ not empty yet ~" << endl;
break;
case 7:
cout << "\033c";
cout << "The datas now stored in the map are : " << endl;
for(it = m.begin(); it!= m.end(); it++)
{
cout << (*it).first << " " << (*it).second << endl;
}
break;
default:
cout << "\033c";
cout << "Error action input, type 0 to look at the action list" << endl;
break;
}
cout << endl;
cout << "Anything you also want to do ?" << endl;
cout << "(To end the program, you can type 100 now)" << endl;
cout << "(To review the action list please type 0)" << endl;
}
}
```