C++ 벡터의 쌍 (pair)

오현진·2024년 6월 14일

C++ 

목록 보기
5/26

std::pair는 두 개의 값을 함께 저장하는데 사용된다.
파이썬의 튜플과 유사.
벡터와 쌍을 함께 사용하면, (int, int) 형태의 2차원 좌표를 저장하는데 유용하다.

#include <vector>
#include <iostream>
#include <utility>

using namespace std;

int main() { 
    vector<pair<int, int>> points;

    points.push_back(make_pair(1, 2));
    points.push_back(make_pair(3, 4));
    points.push_back(make_pair(10, -1));

    for (const auto& point : points) {
        cout << point.first << ", " << point.second << endl;
    }

    return 0;
}
1, 2
3, 4
10, -1

0개의 댓글