C++自修入門實境秀、C++ Primer 5版研讀秀 70/ ~v11關聯式容器 Defining an Associative Container、列出漢字詞語部件及其個數並汰重 Word VBA





11.2. Overview of the Associative Containers

7:24:25

1:30 關聯式容器,不管是有序無序的都能做一般容量的運算,如9.2. Container Library Overview 所示及Table 9.2. Container Operations所列

然而關聯式容器確實是沒有支援循序容器的下標(subscript)運算(由指定位置來存取元素的運算),如push_front和back這樣的都不支援。因為關聯式容器的元素都是依據鍵值(key)來存放的,這些藉由指定位置來存取元素的運算自然就沒有什麼意義了,因為要存取元素,在關聯式容器中並不是藉由位置來定位,而是藉由鍵值(key)來搜尋。

甚至,關聯式容器並沒有提供插入特定元素值及數量的運算和這類的建構器(constructor)。15:30

Moreover, the associative containers do not support the constructors or insert operations that take an element value and a count.

⑧找對主詞 代名詞that代的是the constructors or insert operations這兩個東東

19:00

Word VBA 查詢某漢字部件及部件數

44:00成功 https://www.facebook.com/oscarsun72/posts/2309411739169947

49:00

the associative containers provide some operations (Table 11.7 (p. 438)) and type aliases (Table 11.3 (p. 429)) that the sequential containers do not.

循序容器(sequential container)所無的運算

無序容器(unordered container)還提供了一些調整它們雜湊(hash)性能的運算,將在11.4(頁444)再討論

關聯式容器的迭代器是雙向迭代器(bidirectional iterator)

就是不能隨機存取(因為隨機存取與位置有關)而已

56:40

11.2.1. Defining an Associative Container

每種關聯式容器都定義了預設建構器(default constructor)這種建構器會創建一個所指定型別的空容器

在新標準下,也可以串列初始化(list initialization)關聯式容器的元素

map<string, size_t> word_count; // empty

// list initialization

set<string> exclude = {"the", "but", "and", "or", "an", "a",

"The", "But", "And", "Or", "An",

"A"};

// three elements; authors maps last name to first

map<string, string> authors = {{"Joyce", "James"},

{"Austen", "Jane"},

{"Dickens", "Charles"}};



「maps last name to first」意思就是由last name可以找到first

maps這裡是動詞 ⑦先抓動詞 指如地圖與地圖,對應、關聯起來(所以才叫作「關聯式」)

set的元素型別,就是它的鍵值(key)型別

For set , the element type is the key type.

1:8:17

1:9:10

Initializing a multimap or multiset

The keys in a map or a set must be unique; there can be only one element with a given key.

The multimap and multiset containers have no such restriction; there can be several elements with the same key.

可證「multi」在此是重複的意思

頁424

1:12:10

// define a vector with 20 elements, holding two copies of each number from 0 to 9

vector<int> ivec;

for (vector<int>::size_type i = 0; i != 10; ++i)

{

ivec.push_back(i);

ivec.push_back(i); // duplicate copies of each number

}

// iset holds unique elements from ivec; miset holds all 20 elements

set<int> iset(ivec.cbegin(), ivec.cend());//可見給它全部,重複的set在建構時就不捨棄了

multiset<int> miset(ivec.cbegin(), ivec.cend());

cout << ivec.size() << endl; // prints 20

cout << iset.size() << endl; // prints 10

cout << miset.size() << endl; // prints 20

1:22:58

練習11.5

map、set在元素值與型別不同

若要儲存一對關聯的資料,就用map;若只是要查詢以比對一組(set)特定的資料,則用set即可。

練習11.6

1:26:27

list和set都不能隨機存取(藉由位置來存取元素),但是list只能用迭代、巡覽方式,逐一清查其內的各個元素(只有在要插入或移除元素時可以隨機處置——由位置(迭代器)指定來處置);而set卻能透過鍵值(key)快速查找到符合特定鍵值的元素

如果不必逐一清查時,就用set(即可跳著、挑著檢查,只檢查所需要的);而須逐一比對時才用list。

練習11.7

9:21:04

9:57:00 成功

#include<iostream>

#include<iterator>

#include<vector>

#include<algorithm>

#include<map>

using namespace std;

int main() {

map<string, vector<string>> m;

//test text:

//Wells Oscar Wells joy Sun Oscar Washington Smith Sun June Wells Steve Washington Jack Sun Judy

//multimap<string, vector<string>> m;

istream_iterator<string>in(cin), end;

string lastName;

while (in!=end)

{

lastName = *in;

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

++in;

}

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

for (auto a :m)

{

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

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

cout << endl;

}

}



Word VBA

1:37:00 列出漢字部件數及其部件程式碼補訂 1:59:20 增訂為選取狀態時,可以將選取的漢字、漢詞、漢語,其部件總數及部件列出(須汰重)。

2:33:00 元素為Word Range的陣列

Function 某詞部件數查詢_汰重

3:50:00 大抵成功了

7:11:00真正成功了 7:27:00完全成功了 7:45:00 完全正確了 圓滿解決 7:56:00 再改寫,不要倚靠vba.InStr().用陣列儲存。成功了,還有bug. 8:7:00 8:59:05 完成了

列出漢字部件數及其部件;可以將選取的漢字詞句,其部件總數及部件列出(會汰重)

https://oscarsun72.blogspot.com/2019/10/word-vba.html


4:43:00

Working with Range Objects

Range object (Word)

Range.SetRange method (Word)

6:8:10 Range.Words property (Word)

6:4:30難得有我完全都會──沒有不會的說明文件

6:14:00

文件是否空白,是這樣判斷

ActiveDocument.Content.StoryLength = 1

是用StoryLength屬性來判斷的:

If the document header is empty, StoryLength returns 1 for the final paragraph mark.

6:24:30

Range.Duplicate property (Word)

Returns a read-only Range object that represents all the properties of the specified range.

和SetRange差別只在SetRange是影響原range而這個不會

The two range objects are merely "pointers" (of sorts) for the content in the Word document itself.(〈Make a copy of a Word Range object〉https://bytes.com/topic/visual-basic-net/answers/353479-make-copy-word-range-object



練習11.8

9:59:10

由練習11.3改寫

#include<fstream>

#include<iterator>

#include<map>

#include<vector>

using namespace std;

int main() {

//set<string>st{ "the","an","and","or" ,"be","at","if","to","of"};

vector<string>st{ "the","an","and","or" ,"be","at","if","to","of"};

ifstream fs("V:\\Programming\\C++\\Untitled-1.txt");

istream_iterator<string>in(fs), end;

map<string, string::size_type>m;

while (in != end)

{

//if (st.find(*in) == st.cend())

if (find(st.cbegin(),st.cend(),*in)==st.cend())

{

++m[*in];

}

++in;

}

ofstream ofs("V:\\Programming\\C++\\out.txt");

for (auto var : m)

ofs << var.first << " " << var.second << endl;

}

10:9:00

11.2.2. Requirements on Key Type

11.2.2對於鍵值型別的要求

鍵值型別的必要條件

留言

熱門文章