分享到plurk 分享到twitter 分享到facebook

版本 ed6c03f5d4aed15353e9dc537a041913a04d022b

acm/course/Vector

Changes from ed6c03f5d4aed15353e9dc537a041913a04d022b to current

Vector
========
Introduction
--------
Vector是c++中陣列的替代型態,可以自主控制需要的記憶體。
Vector可以任意增加陣列長度及資料的數量,也可任意插入或刪除指定位置的資料。

基礎運用
--------
使用vector需要加入標頭檔<vector>
使用vector需要加入標頭檔vector

ex:
```c
#include <vector>
```
建立新vector的語法

宣告新vector的語法
```c
vector<type> vector_name(amount,element);

```
ex:
```c
vector<int> acm(3,4);
vector<float> csie(5);
vector<float> csie(5);//未指定數字會補 0
```
嘗試印出兩vector內容

output:
```c
acm : 4 4 4
csie : 0 0 0 0 0 
```
也可以直接建立空白的vector

ex:
```c
vector<int> acm;
```
此外也可以使用陣列為基礎來建構vector,將陣列原有的資料放入vector中

若要讀取資料的話需要先定義iterator
如此的指定方式,需要提供的參數為起始位址與結束位址

ex:
```c
vactor <int>::iterator it;
vactor <float>::iterator it2;
int array[5]={1,2,3,4,5};
vector<int> ncku(array,array+5);//1 2 3 4 5 
vector<int> csie(array+1,array+4);//2 3 4
```

An fully executable program example :
Member functions
----------------------------

### operator[]
中括號[]的用法與array相同,指的是vector中的指定項元素
```c
#include <iostream>
#include <map>
#include <string>
int array[5]={1,2,3,4,5};
vector<int> ncku(array,array+5);
cout << ncku[2] << endl; 
```
output:
```c
3
```
### iterator迭代器/.begin() /.end()
若要讀取資料的話也可以定義iterator來進行進一步的操作
```c
vector <int>::iterator it;
vector <float>::iterator it2;
```
.begin()會將iterator指向第一筆資料

.end()會將iterator指向最後一筆資料的下一個位子

完整程式碼範例

ex:
```c
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    map<int, string> m;
    map<int, string>::iterator it;
vector<int> acm(3,4);
vector<int>::iterator it;

void vector_print(vector<int> v){

	cout << "The vector contains these elements : " << endl;
	for(it = v.begin(); it != v.end(); it++){
		cout << *it << " ";
	}//prints the vector from the first element to the last element
	cout << endl;
}

int main(){
    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;
    vector_print(acm);
    return 0;
}
```

Output:
```c
The vector contains these elements :
4 4 4

![](/map1.png)
```

Member functions
----------------------------
### .push_back()/.pop_back()
.push_back()會將小括號內的資料加入vector尾端

### .begin() / .end()
而.pop_back則會刪去最後面的資料

>.begin()是map第一筆資料的iterator
ex:
```c
char ar[5]={'a','b','c','d','e'};
vector<char> csie(ar,ar+5);
vector<char>::iterator it;
vector_print(csie);

>.end()是最後一筆資料的iterator
csie.push_back('f');
vector_print(csie);

### .size()
csie.push_back('g');
vector_print(csie);

>可以用來檢測當前map裡有幾筆資料
csie.pop_back();
vector_print(csie);

例如
csie.pop_back();
vector_print(csie);
```

output:
```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;
    }
a b c d e
a b c d e f
a b c d e f g
a b c d e f 
a b c d e
```

![](/map2.png)
### .size()

.size會回傳vector大小
```c
vector<int> acm(5);
cout << acm.size() << endl;
```
output:
```c
5
```
### .clear() / .empty()

>.clear()是用來清空map裡所有資料的一個member function
.clear()會刪除vector內的所有元素,clear後的vector大小會為0

>.empty()是用來察看map裡的資料是否是空的的member function (returns boolean)
而.empty()則會測試vector是否為空的,是則回傳ture,否則回傳false

注意:empty只會測試是否為空,並不會清空vector內資料
```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)
vector<int> acm;

### .find()
acm.push_back(100);
acm.push_back(200);
acm.push_back(300);
cout << "the original size is : " 
     << acm.size() 
     << endl;

.find()是用來搜尋某筆map資料的iterator
acm.clear();	
cout << "after cleaning, the size is : "
     << acm.size() 
     << endl;

在取得該筆map資料的iterator後可對其進行修改value
if(acm.empty()==true){
    cout << "the vector is empty!"
	 << endl;
}

但是key因為是read-only所以無法對齊進行修改 
```
output:
```c
the original size is : 3
after cleaning, the size is : 0
the vector is empty!
```
### .insert()
.insert()可以指定元素插入vector的位置與數量

>格式: it = map.find(key);
有三種常見的插入方式

Code example:
1.插入單個數字
```c
vector.insert(position,element);
```
2.填入多個相同數字
```c
vector.insert(position,amount,element);
```
3.將另一獨立vector或陣列插入舊有vector
```c
vector.insert(position,input start,input end);
```
完整程式碼範例

ex:
```c
#include <iostream>
#include <map>
#include <string>

#include <vector>
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);
vector<int>::iterator it;

Erase by key
void vector_print(vector<int> v){

>map.erase(key);
    for(it = v.begin(); it != v.end(); it++){
	cout << *it << " ";
    }
    cout << endl;
}
int main(){
    vector<int> ncku;
        
    ncku.push_back(108);
    it=ncku.begin();
    ncku.insert(it,107);
    vector_print(ncku);

>Code example:
    ncku.insert(ncku.begin(),2,106);
    vector_print(ncku);

```c
#include <iostream>
#include <map>
#include <string>
    vector<int> csie(3,105);
    ncku.insert(ncku.begin(),csie.begin()+1,csie.end());
    vector_print(ncku);

using namespace std;
    int array[]={108,109,120};
    it=ncku.end();
    ncku.insert(it,array,array+3);
    vector_print(ncku);

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;
    }
    return 0;
}
```

![](/map5.png)

A long code for you to look and execute for review(?)
--------
output:
```c
#include <iostream>
#include <map>
#include <string>

using namespace std;
107 108 
106 106 107 108 
105 105 106 106 107 108 
105 105 106 106 107 108 108 109 120
```

map<int, string> m;
map<int, string>::iterator it;
### .erase()
.erase()可以刪除單個元素,也可以刪除給定範圍內的所有資料

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;
}
用法分別為
```c
vector.erase(position);
```
以及
```c
vector.erase(erase start,erase end);
```
ex:
```c
int array[]={0,1,2,3,4,5,6,7,8,9,10};
vector<int> csie(array,array+11);

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;
csie.erase(csie.begin()+6);//0 1 2 3 4 5 7 8 9 10
csie.erase(csie.begin()+6,csie.end());//0 1 2 3 4 5
```
### .assign()
使用assign對vector賦值的話會將vector內原有的所有資料覆蓋

    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;
    }
}
assign的用法與宣告時類似,但沒有只給一個參數(amount)的用法
```c
vector.assign(amount,element);
vector.assign(input array start,input array end);
```
ex:
```c
vector<int> ncku(3,2016);//2016 2016 2016
ncku.assign(5,100);//100 100 100 100 100
int array[5]={1,2,3,4,5};
ncku.assign(array,array+5);//1 2 3 4 5
```