std::tieとstd::minmaxを使う際の注意点
1
2
3
4
5
6
#include <bits/stdc++.h>
int main() {
int a = 2, b = 1;
std::tie(a, b) = std::minmax(a, b);
std::cout << a << " " << b << std::endl;
}
このコードを実行すると
1
1 1
が出力される。std::minmax
が参照を返すためだ。
1
2
3
4
5
6
#include <bits/stdc++.h>
int main() {
int a = 2, b = 1;
std::tie(a, b) = std::minmax({a, b});
std::cout << a << " " << b << std::endl;
}
などと修正すると
1
1 2
が出力される。
ついついやりがちなので注意
This post is licensed under CC BY 4.0 by the author.