Dev:Source/Architecture/Vector Usage

提供: wiki
< Dev:Source‎ | Architecture
2018年6月29日 (金) 02:47時点におけるYamyam (トーク | 投稿記録)による版 (1版 をインポートしました)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

The template-class std::vector can be great in simplifying and standardizing storage classes across programs, but a lot of caution needs to be taken when using it. Please follow these guidelines to make clean, efficient and bugless code:


The following code can cause exceptions to be thrown:

&( vector::iterator::end() )
    • This is illegal because you are trying to dereference a null pointer. One common error for new programmers is to assume that end() returns the last pointer in the sequence. However, it doesn't. It returns the last pointer in the sequence plus one ( + 1 ). To dereference the last pointer of the sequence, use this instead:


&( (vector::iterator::end() - 1) )
vector::iterator::erase()
    • This is ok, except it often causes issues when used in an iteration sequence or for loop. The reason for this is because erase() not only clears the value at the current location, but it also deletes the pointer to the value. In an iteration sequence, the program then tries to increment a null pointer, throwing an exception. Instead, use this code:


vector::iterator::clear()