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
Thursday, June 26, 2008
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
Monday, June 23, 2008
Library string Type and String Literals
C++ Primer 4/e in 3.2. Library string Type there is a caution:『For historical reasons, and for compatibility with C, character string literals are not the same type as the standard library string type. This fact can cause confusion and is important to keep in mind when using a string literal or the string data type.』
Chinese translation is said:『由於歷史因素和C相容性,字元字串字面常數(character string literals)的型別和C++標準庫的string型別並不相同。這可能會造成困惑。在使用字串字面常數或string型別時,這是一件必須放在心上的重要事情。』
Mm,I am familiar with the usage of the C syntaxs. When I change to use C++, I truly not too understand the differences between them.
achi's Blog
Chinese translation is said:『由於歷史因素和C相容性,字元字串字面常數(character string literals)的型別和C++標準庫的string型別並不相同。這可能會造成困惑。在使用字串字面常數或string型別時,這是一件必須放在心上的重要事情。』
Mm,I am familiar with the usage of the C syntaxs. When I change to use C++, I truly not too understand the differences between them.
achi's Blog
Sunday, June 22, 2008
Application.DisplayAlerts = False
In general using PHP COM examples, we usually touch the using of the "application->DisplayAlerts = 0;".But it can't solve the problem of the below picture.

Because our program read the sheet,and the sheet read from another sheet or another files, The excel default auto Updating Links, under tools, select options, go to the "Edit" tab and uncheck the "Ask to update automatic links". But it is inconvenient if we can set the option in our codes.http://www.excelforum.com/showthread.php?p=1933697#post1933697 therer is a post about the properAskToUpdateLinks.I test in my PHP code.And I found it is very useful.
achi's Blog
Because our program read the sheet,and the sheet read from another sheet or another files, The excel default auto Updating Links, under tools, select options, go to the "Edit" tab and uncheck the "Ask to update automatic links". But it is inconvenient if we can set the option in our codes.http://www.excelforum.com/showthread.php?p=1933697#post1933697 therer is a post about the properAskToUpdateLinks.I test in my PHP code.And I found it is very useful.
achi's Blog
Subscribe to:
Posts (Atom)