用对对填充向量未按预期工作

Filling vector-of-pairs with pair-of-pair not working as expected

本文关键字:工作 填充 向量      更新时间:2023-10-16

我有以下代码填充包含一对对的向量。

std::vector<std::pair<double, std::pair<int, int>>> vec;
int x=100, y=10, z=20;
vec.push_back(std::make_pair((double)x,std::make_pair(y,z)));
for(int i=0;i<vec.size();i++){
std::cout<<"x: "<<vec[i].first<<"n";
std::cout<<"y: "<<vec[i].second.first<<"n";
std::cout<<"z: "<<vec[i].second.second<<"n";
}

输出:

x: 0
y: 0
z: 0

为什么不按如下方式打印?

x: 100.0
y: 10
z: 20

它按预期工作。使用 G++ 编译器进行测试。 未观察到任何问题。

#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
std::vector<std::pair<double, std::pair<int, int>>> vec;
int x = 100, y = 10, z = 20;
vec.push_back(std::make_pair((double)x, std::make_pair(y, z)));
for (int i = 0; i < vec.size(); i++) {
std::cout << "x: " << vec[i].first << "n";
std::cout << "y: " << vec[i].second.first << "n";
std::cout << "z: " << vec[i].second.second << "n";
}
return 0;
}

请参考此链接