C++自修入門實境秀、C++ Primer 5版研讀秀 71/ ~ v11關聯式容器~11.2.2. 11.2.2. Requirements...





11.2.2. Requirements on Key Type

11.2.2對於鍵值型別的要求

鍵值型別的必要條件

6:00 想作為鍵值鍵值就是元素的型別一定要自定義一個比對其元素的方法。該型別所構建的元素,要經過怎樣的方法才能做比對。

程式庫一般都是用鍵值型別的「<」運算子來定義鍵值作為元素間的比較

每個型別有每個型別對運算子的定義(如果有定義的話)

set關聯式容器,鍵值就是其元素型別

map則鍵值是其元素的第一型別、首型別、長型別

sort演算法也有類似的需求

頁425

Key Types for Ordered Containers

有序容器的鍵值型別

1:12:30 1:15:19

就像提供自己的比較式來取代原來演算法內定的比較式,我們也可以用自己對「<」運算子操作的方式來取代鍵值(key)型別制定的原方式

而我們自訂的運算:

The specified operation must define a strict weak ordering over the key type.

嚴格的弱次序(strict weak ordering)

→由下文 上下文(前後文) 判斷,應翻成:絕對的等差級別。weak就是「<」.

自訂的比較函式必須符合以下條件:

1.兩個鍵值間不可互為小於

2.如有兩個鍵值,彼此相持不下,那麼必然是旗鼓相當:

If there are two keys, and neither key is “less than” the other, then we’ll say that those keys are “equivalent.”

意思就是我們自訂的「<」一定要有不可逆轉、踰越的規矩、一致性,這就叫「strick」

鍵值相等,則容器平等待之

和iterator、argument 一樣,key也是用介係詞「to」:a key to a map ,作為map的一個key

因為這些keys是等值的,所以在map之中唯有一個元素能關聯關聯式容器(associative container)的關聯到這些同值的鍵值(key),且它們任何一個都可以用來存取那個關聯到它們的元素相應的「值 value」(即鍵值與值對組(key-value pair)的值)

只要一個型別定義了有序的正常表現的、有規則的、嚴謹的小於運算子「<」,就可以用來作為鍵值(型別)

Using a Comparison Function for the Key Type

1:44:17

為鍵值型別使用一個比較函式

The type of the operation that a container uses to organize its elements is part of the type of that container.

一個容器用來組織它元素所用上的運算,其所屬的型別,也是這個容器型別構成的一部分



頁426

1:58:00

因為函式本身不是型別,要把它作為型別來定義容器,就必須使用函式指標(function pointer),則用的是型別是指標此一複合型別(compound type):

the comparison type, which is a function pointer type (§ 6.7 , p. 247 ) that can point to compareIsbn .

multiset<Sales_data, decltype(compareIsbn) *> bookstore(compareIsbn);

2:5:20



複習前面的函式指標(function pointer)

2:14:00

4:37:38

練習11.9

#include<iostream>

#include<iterator>

#include<list>

#include<map>

using namespace std;

int main() {

map<string, list<string>>m;

istream_iterator<string>in(cin), end;

ostream_iterator<string>out(cout, ",");

string word;

while (in!=end)

{

word = *in;

m[word].push_back(*++in);

++in;

}

for (auto a : m)

{

cout << a.first <<":\t";

copy(a.second.cbegin(),a.second.cend(), out);

cout << endl;

}

}

5:18:50

練習11.10

5:19:30

list不支援隨機存取,所以不行

Table 9.1. Sequential Container Types

僅支援雙向的循序存取

循序就必然要與「位置」有關。而map是以鍵值來存取元素的,不是位置。

在實務上,最重要的是,定義了「行為正常」的 <運算子的一個型別可被當作一個鍵值使用。

6:26:10 6:37:00 list果然不行:

Severity Code Description

Error C2678 binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion) with [ _Ty=std::_List_iterator<std::_List_val<std::_List_simple_types<int>>> ]

如list中不能拿第1個迭代器跟第10個比較,它只能循序存取

#include<iostream>

#include<iterator>

#include<list>

#include<vector>

#include<map>

using namespace std;

int main() {

map<list<int>::iterator, int>m;

map<vector<int>::iterator, int>mv;

vector<int>vi; vector<int>::iterator viiter;

list<int>li; list<int>::iterator liiter;

istream_iterator<int>in(cin), end;

ostream_iterator<int>out(cout, ",");

string word;

while (in!=end)

{

vi.push_back(*in++);

//li.push_back(*++in);

}

//liiter = li.begin();

viiter = vi.begin();

while (viiter!=vi.end())

{

//m[liiter] = *liiter;

//++liiter;

mv[viiter] = *viiter;

++viiter;

}

for (auto a : mv)

{

cout << *a.first << ":\t";

*out++ = a.second;

cout << endl;

}

cout <<"-----------"<< endl;

*out++ = mv[viiter-4];

cout << endl;

}



Word VBA 列出漢字部件數及其部件程式碼補訂

5:36:10 Selection改用Range來寫

6:12:00 測試成功 https://snipsave.com/oscarsun72/#/snippet/aHBsXQKL7eWFFS6S4Y

Word VBA 選取區漢字排序

6:55:30 選取區漢字排序 1.依筆畫→部首(系統預設的排序)2.部首→筆畫 一樣用Range物件 ok 7:40:00 測試成功

選取區文字排序_依系統筆畫規則

https://snipsave.com/oscarsun72/#/snippet/QJJh5ASJTQfBeHtYIT

選取區漢字排序_依部首筆畫排序

https://snipsave.com/oscarsun72/#/snippet/vF4C9q8ahyARgTVF5Z

10:15 測試成功



練習11.11

6:53:24

留言

熱門文章