C++ Primer 4/e in 4.1. Arrays there is a beware:『Some compilers allow array assignment as a compiler extension. If you intend to run a given program on more than one compiler, it is usually a good idea to avoid using nonstandard compiler-specific features such as array assignment.』
Another a caution:『Unlike the vector type, there is no push_back or other operation to add elements to the array. Once we define an array, we cannot add elements to it.
If we must add elements to the array, then we must manage the memory ourselves. We have to ask the system for new storage to hold the larger array and copy the existing elements into that new storage. We’ll see how to do so in Section 4.3.1 (p. 134).』
Chinese Translation is said:『有些編譯器允許array賦值操作,視為一種編譯器擴充功能。如果你希望程式通過一個以上的編譯器,最好避免使用非標準的編譯器特有性質,例如這裡所說的array賦值操作。
不同於vector,array並沒有push_back()或其他用來增添元素的操作。一旦我們定義了一個array,就不能再擴展它。
如果一定要為array添加元素(空間),就必須自我管理記憶體。我們必須向系統要求新空間以存放較大的array,然後複製現有元素到新空間去。』
At first array has not push_back supported it is not very convenient.
achi's Blog
Monday, June 30, 2008
Sunday, June 29, 2008
Iterators and Iterator Types
C++ Primer 4/e 3.4. Introducing Iterators ther is a terminology:『When first encountered, the nomenclature around iterators can be confusing. In part the confusion arises because the same term, iterator, is used to refer to two things. We speak generally of the concept of an iterator, and we speak specifically of a concrete iterator type defined by a container, such as vector.
What’s important to understand is that there is a collection of types that serve as iterators. These types are related conceptually. We refer to a type as an iterator if it supports a certain set of actions. Those actions let us navigate among the elements of a container and let us access the value of those elements.
Each container class defines its own iterator type that can be used to access the elements in the container. That is, each container defines a type named iterator, and that type supports the actions of an (conceptual) iterator.』
Chinese translationis said:『第一次遭遇術語iterators時可能會感到困惑。部份原因是同一個術語iterator可用來指兩件事。可以指一般的iterator概念,也可以指某容器(例如vector)所定義的具象iterator型別。
一件必須瞭解的重要事情是,有一大群型別可作為iterators使用。這些型別在概念上彼此關聯。當一個型別支援某「特定動作集」時我們稱它為iterator;它讓我們得以尋訪容器元素並存取元素值。
每個做為容器的class都定義有自己的iterator型別,可用來存取容器元素。也就是說每個容器都定意有一個名為iterator的型別,該型別支援(概念上的)iterator所能採取的動作。』
What is concrete ?I don't understand.
achi's Blog
What’s important to understand is that there is a collection of types that serve as iterators. These types are related conceptually. We refer to a type as an iterator if it supports a certain set of actions. Those actions let us navigate among the elements of a container and let us access the value of those elements.
Each container class defines its own iterator type that can be used to access the elements in the container. That is, each container defines a type named iterator, and that type supports the actions of an (conceptual) iterator.』
Chinese translationis said:『第一次遭遇術語iterators時可能會感到困惑。部份原因是同一個術語iterator可用來指兩件事。可以指一般的iterator概念,也可以指某容器(例如vector
一件必須瞭解的重要事情是,有一大群型別可作為iterators使用。這些型別在概念上彼此關聯。當一個型別支援某「特定動作集」時我們稱它為iterator;它讓我們得以尋訪容器元素並存取元素值。
每個做為容器的class都定義有自己的iterator型別,可用來存取容器元素。也就是說每個容器都定意有一個名為iterator的型別,該型別支援(概念上的)iterator所能採取的動作。』
What is concrete ?I don't understand.
achi's Blog
Thursday, June 26, 2008
Only Subscript Elements that Are Known to Exist!
C++ Primer 4/e in 3.4. Introducing Iterators there is a warning:『It is crucially important to understand that we may use the subscript operator, (the [] operator), to fetch only elements that actually exist. For example,
vector ivec; // empty vector
cout <<> ivec2(10); // vector with 10 elements
cout << ivec[10]; //error
Chinese translation is said:『我們只能以subscript運算子([])取出實際存在的元素。這一點十分重要。例如:
vector ivec; // 空的 vector
cout <<> ivec2(10); // vector 內含 10 個元素
cout << ivec[10]; // 錯誤: ivec 的元素編號是0到9
擷取不存在的元素會造成執行期錯誤。編譯器並不保證能偵測出大部分此類錯誤。這個程式的執行結果無法確定,因為「擷取不存在元素」是一種不明確的行為,其結果視編譯器而不同,但幾乎可以確定會在執行期出現某種有趣的錯誤。
這個警告亦可套用於任何使用下標的時候,例如對string或(很快會看到)對內建的array取下標。
不幸的是,企圖以下標存取不存在的元素是極常見且致命的編程錯誤。所謂緩衝區上限溢位(buffer overflow)錯誤就是以下標存取不確定元素的結果。這種臭蟲是形成PC程式及其他應用程式安全問題的最常見原因。』
This is a good warning, especially when we from VB microsoft's series to C series using array often mistake the array start from 0 or 1 .
achi's Blog
vector
cout <<> ivec2(10); // vector with 10 elements
cout << ivec[10]; //error
Chinese translation is said:『我們只能以subscript運算子([])取出實際存在的元素。這一點十分重要。例如:
vector
cout <<> ivec2(10); // vector 內含 10 個元素
cout << ivec[10]; // 錯誤: ivec 的元素編號是0到9
擷取不存在的元素會造成執行期錯誤。編譯器並不保證能偵測出大部分此類錯誤。這個程式的執行結果無法確定,因為「擷取不存在元素」是一種不明確的行為,其結果視編譯器而不同,但幾乎可以確定會在執行期出現某種有趣的錯誤。
這個警告亦可套用於任何使用下標的時候,例如對string或(很快會看到)對內建的array取下標。
不幸的是,企圖以下標存取不存在的元素是極常見且致命的編程錯誤。所謂緩衝區上限溢位(buffer overflow)錯誤就是以下標存取不確定元素的結果。這種臭蟲是形成PC程式及其他應用程式安全問題的最常見原因。』
This is a good warning, especially when we from VB microsoft's series to C series using array often mistake the array start from 0 or 1 .
achi's Blog
Safe, Generic Programming
C++ Primer 4/e in vector there is a key concept:『Programmers coming to C++ from C or Java might be surprised that our loop used != rather than < to test the index against the size of the vector. C programmers are probably also suprised that we call the size member in the for rather than calling it once before the loop and remembering its value.
C++ programmers tend to write loops using != in preference to Part II.
Calling size rather than remembering its value is similarly unnecessary in this case but again reflects a good habit. In C++, data structures such as vector can grow dynamically. Our loop only reads elements; it does not add them. However, a loop could easily add new elements. If the loop did add elements, then testing a saved value of size would failour loop would not account for the newly added elements. Because a loop might add elements, we tend to write our loops to test the current size on each pass rather than store a copy of what the size was when we entered the loop.
As we’ll see in Chapter 7, in C++ functions can be declared to be inline. When it can do so, the compiler will expand the code for an inline function directly rather than actually making a function call. Tiny library functions such as size are almost surely defined to be inline, so we expect that there is little run-time cost in making this call on each trip through the loop.』
Chinese translation is said:『C或Java程式員可能會對「在迴圈內使用 != 而非 < 來測試索引值和vector大小」感到驚訝。C程式員可能也會對「在for內呼叫size()而非在迴圈前呼叫一次並記住其值」感到驚訝。
C++程式員習慣上傾向使用 != 而較不喜歡使用 < 來寫迴圈。倒是沒有特別理由一定要選用某個運算子。第Ⅱ篇談到泛型編程(generic programming)後讀者就會瞭解這個習慣的緣由。
「呼叫size()而非記住其值」在這裡同樣也非必要,但再次反映一個良好的習慣。在C++,vector這一類資料結構可以動態成長。雖然此處的迴圈只讀取元素,並沒有增加元素,然而迴圈內的確輕易可以加入元素。果真如此,那麼測試前儲存size就會出錯,迴圈將因此不處理新加入的元素。由於迴圈可能加入元素,所以我們傾向每次測試當下的size,而非紀錄進入的迴圈前的size。
如同第7章即將看到,C++函式可宣告為inline。一旦如此,編譯器會把inline函式的程式碼當下展開,而非產生一個函式呼叫。小型函式如size()幾乎肯定會被定義為inline,所以可預期「每個迭代都呼叫一次」只帶來很小的執行期成本。』
Using != is very suprised to me.And I generally used to remember size unless the array or object will increase or decrease as the author said.I will not use remember size.But if size use the method of the inline and the runtime cost is very low.Then changing the habit actually may be not bad.
achi's Blog
C++ programmers tend to write loops using != in preference to Part II.
Calling size rather than remembering its value is similarly unnecessary in this case but again reflects a good habit. In C++, data structures such as vector can grow dynamically. Our loop only reads elements; it does not add them. However, a loop could easily add new elements. If the loop did add elements, then testing a saved value of size would failour loop would not account for the newly added elements. Because a loop might add elements, we tend to write our loops to test the current size on each pass rather than store a copy of what the size was when we entered the loop.
As we’ll see in Chapter 7, in C++ functions can be declared to be inline. When it can do so, the compiler will expand the code for an inline function directly rather than actually making a function call. Tiny library functions such as size are almost surely defined to be inline, so we expect that there is little run-time cost in making this call on each trip through the loop.』
Chinese translation is said:『C或Java程式員可能會對「在迴圈內使用 != 而非 < 來測試索引值和vector大小」感到驚訝。C程式員可能也會對「在for內呼叫size()而非在迴圈前呼叫一次並記住其值」感到驚訝。
C++程式員習慣上傾向使用 != 而較不喜歡使用 < 來寫迴圈。倒是沒有特別理由一定要選用某個運算子。第Ⅱ篇談到泛型編程(generic programming)後讀者就會瞭解這個習慣的緣由。
「呼叫size()而非記住其值」在這裡同樣也非必要,但再次反映一個良好的習慣。在C++,vector這一類資料結構可以動態成長。雖然此處的迴圈只讀取元素,並沒有增加元素,然而迴圈內的確輕易可以加入元素。果真如此,那麼測試前儲存size就會出錯,迴圈將因此不處理新加入的元素。由於迴圈可能加入元素,所以我們傾向每次測試當下的size,而非紀錄進入的迴圈前的size。
如同第7章即將看到,C++函式可宣告為inline。一旦如此,編譯器會把inline函式的程式碼當下展開,而非產生一個函式呼叫。小型函式如size()幾乎肯定會被定義為inline,所以可預期「每個迭代都呼叫一次」只帶來很小的執行期成本。』
Using != is very suprised to me.And I generally used to remember size unless the array or object will increase or decrease as the author said.I will not use remember size.But if size use the method of the inline and the runtime cost is very low.Then changing the habit actually may be not bad.
achi's Blog
Tuesday, June 24, 2008
vectorS Grow Dynamically
C++ Primer 4/e in 3.3. Library vector Type there is a key concept:『A central property of vectors (and the other library containers) is that they are required to be implemented so that it is efficient to add elements to them at run time. Because vectors grow efficiently, it is usually best to let the vector grow by adding elements to it dynamically as the element values are known.
As we’ll see in Chapter 4, this behavior is distinctly different from that of built-in arrays in C and for that matter in most other languages. In particular, readers accustomed to using C or Java might expect that because vector elements are stored contiguously, it would be best to preallocate the vector at its expected size. In fact, the contrary is the case, for reasons we’ll explore in Chapter 9.』
and a beware :『Although we can preallocate a given number of elements in a vector, it is usually more efficient to define an empty vector and add elements to it (as we’ll learn how to do shortly).』
Chinese translation is said:『vectors(及其他程式庫容器)的一個重要特性是,它們必須能夠在執行期高效地被添加元素。由於vectors能夠高效成長,所以通常最好在元素值已知時才加入元素,讓vector自己動態成長。
一如我們將在第4章所見,這個行為和C內建的arrays以及大部分其他語言的類似東西十分不同。尤其是習慣使用C或Java的讀者,或許會認為vector的元素是連續存放,因此先把vector配置為某預期大小是最好的方法。事實上相反,第9章會探討理由。』
這個當心是這樣寫的:『雖然我們可以為vector預先配置已知個數的元素,但通常定義一個空的vector然後加入元素較有效率。我們很快就會學到如何這麼做。
Before wrote C codes I was unable to solve unknown size array.I need to change using malloc() to allocate memory.Also I thinks it is veryl trouble.And later I use PHP and there is no limits the size.So C++ has the concept of the vector template.It's very coll.I like it.
achi's Blog
As we’ll see in Chapter 4, this behavior is distinctly different from that of built-in arrays in C and for that matter in most other languages. In particular, readers accustomed to using C or Java might expect that because vector elements are stored contiguously, it would be best to preallocate the vector at its expected size. In fact, the contrary is the case, for reasons we’ll explore in Chapter 9.』
and a beware :『Although we can preallocate a given number of elements in a vector, it is usually more efficient to define an empty vector and add elements to it (as we’ll learn how to do shortly).』
Chinese translation is said:『vectors(及其他程式庫容器)的一個重要特性是,它們必須能夠在執行期高效地被添加元素。由於vectors能夠高效成長,所以通常最好在元素值已知時才加入元素,讓vector自己動態成長。
一如我們將在第4章所見,這個行為和C內建的arrays以及大部分其他語言的類似東西十分不同。尤其是習慣使用C或Java的讀者,或許會認為vector的元素是連續存放,因此先把vector配置為某預期大小是最好的方法。事實上相反,第9章會探討理由。』
這個當心是這樣寫的:『雖然我們可以為vector預先配置已知個數的元素,但通常定義一個空的vector然後加入元素較有效率。我們很快就會學到如何這麼做。
Before wrote C codes I was unable to solve unknown size array.I need to change using malloc() to allocate memory.Also I thinks it is veryl trouble.And later I use PHP and there is no limits the size.So C++ has the concept of the vector template.It's very coll.I like it.
achi's Blog
string::size_type
C++ Primer 4/e in 3.2.3. Operations on strings there is a best practice :『Any variable used to store the result from the string size operation ought to be of type string::size_type. It is particularly important not to assign the return from size to an int.』
Chinese translation is said :『任何用以存放string size()返回值的變數都應該是string::size_type型別。千萬別把size()返回值賦予一個int變數,切記。』
I record in my blog then I don't forget.
achi's Blog
Chinese translation is said :『任何用以存放string size()返回值的變數都應該是string::size_type型別。千萬別把size()返回值賦予一個int變數,切記。』
I record in my blog then I don't forget.
achi's Blog
Subscribe to:
Posts (Atom)