c++ pairの使い方を学んで2つの値を管理する
c++のstd::pairの使い方のメモ
std::pairの中身(utility classから参照)
template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair(const pair&) = default; pair(pair&&) = default; constexpr pair(); pair(const T1& x, const T2& y); // constexpr in C++14 template <class U, class V> pair(U&& x, V&& y); // constexpr in C++14 template <class U, class V> pair(const pair<U, V>& p); // constexpr in C++14 template <class U, class V> pair(pair<U, V>&& p); // constexpr in C++14 template <class... Args1, class... Args2> pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args); template <class U, class V> pair& operator=(const pair<U, V>& p); pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value && is_nothrow_move_assignable<T2>::value); template <class U, class V> pair& operator=(pair<U, V>&& p); void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> && is_nothrow_swappable_v<T2>); };
pairとはなにか?
文字通りセットにして使いたいような2つの値を保持することのできるオブジェクトのこと。
自分のケースですが、トランプでババ抜きしているときに、持っているカードとその並び順とか
ダイアのエースが手元の左から何番目にあるかなどを保持したいときなど
ヘルパー関数
関数名 | 説明 | 使用例 |
make_pair(_T1&& __t1, _T2&& __t2) | pairオブジェクト生成 | std::pair<String, int> mypair = make_pair("jiji", 4000); |
pairの使用例
// 生成 std::pair<string, int> mypair = make_pair("jiji", 4000); // アクセス方法 mypair.first = "exchange"; mypair.second = 2000;
pairの配列
// 配列 pair<bool,int>trump[52]; // アクセス trump[0].second