swap technique

typedef std::vector<int> intvector;
 
intvector vec = intvector(100);
// -> vec.size = 100, vec.capacity = 100
 
vec.clear();
// -> vec.size = 0, vec.capacity = 100
 
intvector().swap(vec);
// -> vec.size = 0, vec.capacity = 0
 
vec.reserve(100);
vec.push_back(0);
// -> vec.size = 1, vec.capacity = 100
 
intvector(vec).swap(vec);
// -> vec.size = 1, vec.capacity = 1

C++のキャスト

reinterpret_cast

C言語のようなデータ変換をしないキャスト
(e.g. int → int* on 32bitsystem)

static_cast

C言語のような暗黙の型変換(データ変換)をするキャスト
(e.g. float → int)

const_cast

constを外すだけキャスト
(e.g. const int → int)

dynamic_cast

ダイナミックキャスト
(e.g. 親クラス→子クラスへの型保証キャスト)

reverse_iterator -> iterator

// 最後の要素を削除
container.erase((++reverse_iterator).base());
c_cplusplus.txt · 最終更新: 2010/01/22 14:47 by fistfvck