千慮一得齋OnLine_觀死書齋-Yahoo/Hexun/Blogger/sina/Xuite

觀死書齋暨Spread、和訊博客全文檢索

2019年9月27日 星期五

C++自修入門實境秀、C++ Primer 5版研讀秀 52/ ~ v.9 Sequential Containers 9.2. Contai...





頁321

8.3. string Streams

stringstream (sstream)和 fstream一樣定義了一些新的成員來處理與資料流(stream)相關的string,這些功能也是其他IO類別不能使用的

6:40

雖然fstream和sstream共享了iostream的介面,但它們彼此間並沒有其他特殊的關係



10:20

表8.5限定在stringstream裡面用的運算

表8.5 : stringstream限定的運算

sstream strm; strm是一個未繫結的stringstream。ssfream是定義在sstream標頭 中的型別之一。

sstream strm(s); strm是一個sstream,它存有string s的一個拷貝。這個建構器是 explicit 的(§ 7.5.4)。

strm.str() 回傳strm所持有的string的一個拷貝。

strm.str(s) 將string s拷貝到strm。回傳void。



14:00

8.3.1. Using an istringstream

如何使用 istringstream

以下即彙總類別(aggregate class)

We’ll start by defining a simple class to represent our input data:

// members are public by default; see § 7.2 (p. 268)

struct PersonInfo {

string name;

vector<string> phones;

};

32:00

頁322

37:00終於懂了什麼叫向量(矢量)!

向 量

矢 量

方向 容量、容積、體積、大小……

1:2:10

練習8.9

練習8.1

#include<string>

#include <iostream>

#include <sstream>

#include<vector>

using namespace std;



istream& func(istream& is) {

string i; vector<string>veci;

while (!is.fail()&&!is.eof()&&!is.bad())

{

is >> i;

if (!is.fail() &&!is.eof() && !is.bad()){

istringstream isstrm(i);

string j;

isstrm >> j;

veci.push_back(j);

}

}

for (string a : veci)

{

cout << a;

cout << endl;

}

cout<< cin.rdstate()<<endl;

cin.clear();

//cin.setstate();

return cin;

}

int main() {

istream& i = func(cin);

cout << i.rdstate() << endl;

}



練習8.10

#include <iostream>

#include <fstream>

#include <sstream>

#include<string>

#include<vector>

using namespace std;



istream& func(istream& is) {

string i; vector<string>veci;

while (!is.fail()&&!is.eof()&&!is.bad())

//while (getline(is, i))

{

getline(is, i);

veci.push_back(i);

}

for (string a : veci)

{

istringstream isstrm(a);

while (isstrm>>i)

{

cout << i;

cout << endl;

}

}

cout<< is.rdstate()<<endl;

is.clear();

//is.setstate();

return is;

}

int main() {

ifstream ifstrm("V:\\Programming\\C++\\new 2.h");

istream& i = func(ifstrm);

cout << i.rdstate() << endl;

}



1:59:40

練習8.11

參見練習8.13

#include <iostream>

#include<sstream>

#include<string>

#include<vector>

using namespace std;



struct PersonInfo {

string name;

vector<string>phones;

};

int main() {

string line, word; // will hold a line and word from input, respectively

vector<PersonInfo> people; // will hold all the records from the input

// read the input a line at a time until cin hits end-of-file (or another error)

istringstream record; // bind record to the line we just read

while (getline(cin, line)) {

PersonInfo info; // create an object to hold this record's data

record.str(line);

if (record.eof())//關鍵在這!因為record移位(shift)後在處理下一筆記錄(下一行)前未歸位

record.clear();

record >> info.name; // read the name

while (record >> word) // read the phone numbers

info.phones.push_back(word); // and store them

people.push_back(info); // append this record to people

}

}

2:25:30

練習8.12

string不必初始化,若用類別內初始器感覺多此一舉,而vector則不能初始化,因為不知一個人(一行、一筆記錄)有多少電話號碼

2:31:59

頁323

8.3.2. Using ostringstreams



2:48:30

練習8.13

參見練習8.11

#include <iostream>

#include <fstream>

#include<sstream>

#include<string>

#include<vector>

using namespace std;



struct PersonInfo {

string name;

vector<string>phones;

};

int main() {

string line, word; // will hold a line and word from input, respectively

vector<PersonInfo> people; // will hold all the records from the input

// read the input a line at a time until cin hits end-of-file (or another error)

istringstream record; // bind record to the line we just read

ifstream ifstrm("V:\\Programming\\C++\\new 2.h");//檔案編碼目前只支援 ANSI和Big5,還不支援UTF-8

while (getline(ifstrm, line)) {

PersonInfo info; // create an object to hold this record's data

record.str(line);

if (record.eof())//關鍵在這!因為record移位(shift)後在處理下一筆記錄(下一行)前未歸位

record.clear();

record >> info.name; // read the name

while (record >> word) // read the phone numbers

info.phones.push_back(word); // and store them

people.push_back(info); // append this record to people

}

}



練習8.14

聽實境秀說明,茲不贅。 3:0:00

頁324

Chapter Summary

3:1:10

Defined Terms

3:24:00

原來所謂的繼承,只是繼承了介面的部分:

Programming feature that lets a type inherit the interface of another type.

3:29:00

string streams define an overloaded member named str . Calling str with no arguments returns the string to which the string stream is attached. Calling it with a string attaches the string stream to a copy of that string .

原來用 str(string)函式傳入的引數string 是藉由複本來和stringstream作繫結(bind),而不是指定的string本身來作繫結

頁325

3:35:00

Chapter 9. Sequential Containers

循序容器

向量

4:10:00

關聯式容器(associative container)

頁326

4:21:30

9.1. Overview of the Sequential Containers

Table 9.1. Sequential Container Types

表9.1 :循序的容器型別

表9.1 :循序的容器型別

vector 大小有彈性的陣列:支援快速的隨機存取。插入或刪除不在後端的元素可 能會比較慢。

deque 有兩端開口的佇列(queue) 。支援快速的隨機存取。在前端或後端的插 入或删除也快速。

list 雙向連結串列(doubly linked list)。僅支援雙向的循序存取。在list 中任一點的插入或刪除都很快速。

forward_list 單向連結串列(singly linked list)。僅支援單向的循序存取。在串列中 任一點的插入與刪除都很快速。

array 大小固定的陣列。支援快速的隨機存取。無法新增或刪除元素。

string 一種特化的容器,類似vector,包含字元。快速的隨機存取。在後端的 插入與删除都很快速。

4:37:00

頁327

overhead

額外



4:49:00

expensive operation.

昂貴的作業

所費不貲的運算

這樣的運算付出的代價會不少

4:53:00

An array is a safer, easier-to-use alternative to built-in arrays.

這是的container的array,是程式庫型別的(library array s),不是內建型別(builtin type)的array

5:1:00

A forward_list is intended to be comparable to the best handwritten, singly linked list. Consequently, forward_list does not have the size operation because storing or computing its size would entail overhead compared to a handwritten list. For the other containers, size is guaranteed to be a fast, constant-time operation.

forward_list的設計目標是希望比得上最佳的手寫的單向鏈結串列。因此,forward_list並沒有size運算,因為相較於手寫的串列,儲存或計算大小會帶來不少額外負擔。對其他的容器而言,size保證會是快速、常數時間的運算。

5:9:00

Note:

For reasons we’ll explain in § 13.6 (p. 531), the new library containers are dramatically faster than in previous releases. The library containers almost certainly perform as well as (and usually better than) even the most carefully crafted alternatives. Modern C++ programs should use the library containers rather than more primitive structures like arrays.

現代的C++程式應該使用程式庫的容器,而非像陣列那樣較為原始的結構。

應該用程式庫型別的容器,而別再用傳統的陣列

Deciding Which Sequential Container to Use

循序容器(sequential container)的抉擇

Tip:

Ordinarily, it is best to use vector unless there is a good reason to prefer another container.

5:15:40

可見 list 和 forward_list是很佔用系統資源的。

頁328

library sort function

程式庫函式 sort

copy the list into a vector .

容器(container)是可以拷貝的

5:25:30

兩害相權取其輕:

What if the program needs random access and needs to insert and delete elements in the middle of the container? This decision will depend on the relative cost of accessing the elements in a list or forward_list versus the cost of inserting or deleting elements in a vector or deque. In general, the predominant operation of the application (whether it does more access or more insertion or deletion) will determine the choice of container type. In such cases, performance testing the application using both containers will probably be necessary.



5:33:00

進可攻退可守,腳踏兩條船──養成好習慣Best Practices:

If you’re not sure which container to use, write your code so that it uses only operations common to both vector s and list s: Use iterators, not subscripts, and avoid random access to elements. That way it will be easy to use either a vector or a list as necessary.

如果你不確定要用哪個容器,就將你的程式碼寫成僅使用vector和list所共通的運算:使用迭代器(iterators),而非下標(subscripts),並且避免對元素的隨機存取。如此一來,必要時要改用vector或list都會比較容易。



5:39:10

練習9.1

詳實境秀說明

5:48:05

9.2. Container Library Overview

無序容器(unordered container)

5:53:00

頁329

6:3:20

Constraints on Types That a Container Can Hold

容器和其元素型別的關係、限制、制約

6:10:00

Although we can store almost any type in a container, some container operations impose requirements of their own on the element type. We can define a container for a type that does not support an operation-specific requirement, but we can use an operation only if the element type meets that operation’s requirements.

雖然幾乎任何的型別都能儲存在一個容器中,某些容器的運算會在元素型別上設下它們自己的限制。我們可以為不符合某個運算限定需求的一個型別定義容器,但我們只能在符合運算 需求的元素型別上進行運算。

練習9.2

6:19:50

#include<deque>

#include<list>

using namespace std;

int main() {

list<deque<int>> lstdqi;

//用別名來定義

typedef deque<int> dqi;

list<dqi> lstdqi_alias;

}

沒有留言:

張貼留言