std::tie는 C++11에 도입된 유틸리티 함수로, 여러 개의 변수를 하나의 튜플(tuple)로 묶어주는 기능을 합니다. #include <iostream>
#include <tuple>
#include <queue>
int main() {
std::queue<std::pair<int, int>> virus_queue;
virus_queue.push({1, 2});
virus_queue.push({3, 4});
while (!virus_queue.empty()) {
int r, c;
std::tie(r, c) = virus_queue.front(); // 큐의 front 요소를 r과 c로 분리
virus_queue.pop();
std::cout << "r: " << r << ", c: " << c << std::endl;
}
return 0;
}
r: 1, c: 2
r: 3, c: 4
튜플에서 값 추출
std::tie는 튜플이나 페어에서 값을 추출할 때 사용됩니다.
#include <tuple>
#include <iostream>
int main() {
std::tuple<int, double, std::string> t = std::make_tuple(1, 2.5, "Hello");
int a;
double b;
std::string c;
std::tie(a, b, c) = t;
std::cout << "a: " << a << ", b: " << b << ", c: " << c << std::endl;
return 0;
}
리턴 값으로 사용
std::tie를 사용할 수 있습니다.
#include <tuple>
#include <iostream>
std::tuple<int, double, std::string> getValues() {
return std::make_tuple(42, 3.14, "example");
}
int main() {
int a;
double b;
std::string c;
std::tie(a, b, c) = getValues();
std::cout << "a: " << a << ", b: " << b << ", c: " << c << std::endl;
return 0;
}
무시할 값이 있을 때
std::ignore를 사용할 수 있습니다.
#include <tuple>
#include <iostream>
std::tuple<int, double, std::string> getValues() {
return std::make_tuple(42, 3.14, "example");
}
int main() {
int a;
std::string c;
std::tie(a, std::ignore, c) = getValues();
std::cout << "a: " << a << ", c: " << c << std::endl;
return 0;
}