Monday, June 30, 2008

Arrays Are Fixed Size

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

No comments: