私が遭遇する一般的な設計上の問題は、2 つの変数を一緒にバンドルすると、それらを意味のある方法で参照できなくなることです。
std::pair<int,int> cords;
cord.first = 0; //is .first the x or y coordinate?
cord.second = 0; //is .second the x or y coordinate?
代わりに基本的な構造体を作成することを検討しましたが、次のような多くの利点が失われますstd::pair
。
- make_pair
- 非メンバーのオーバーロードされた演算子
- スワップ
- 得る
- 等
データ メンバーの名前を変更したり、別の識別子を提供したりする方法はありfirst
ますか?second
を受け入れるすべての関数を活用したいと考えてstd::pair
い
ましたが、次の方法でそれらを使用できるようにし たいと考えていました。
std::pair<int,int> cords;
//special magic to get an alternative name of access for each data member.
//.first and .second each have an alternative name.
cords.x = 1;
assert(cords.x == cords.first);