C++ queue<pair<int, int>>에서 queue.front()로 두 개의 인자를 추출하는 방법

오현진·2024년 7월 13일

C++ 

목록 보기
24/26
  1. C++17 구조체 바인딩 (Structured Binding):
auto [x, y] = q.front();
q.pop();
  1. std::tie 사용 (C++11 이상):
int x, y;
std::tie(x, y) = q.front();
q.pop();
  1. 직접 접근 (std::pair의 멤버 함수 사용):
int x = q.front().first;
int y = q.front().second;
q.pop();
  1. 참조로 분해:
const auto& [x, y] = q.front();
q.pop();
  1. 임시 변수 사용:
auto temp = q.front();
int x = temp.first;
int y = temp.second;
q.pop();
  1. 포인터로 접근 (드물게 사용):
int x = (&q.front())->first;
int y = (&q.front())->second;
q.pop();

0개의 댓글