用对对填充向量未按预期工作
Filling vector-of-pairs with pair-of-pair not working as expected
我有以下代码填充包含一对对的向量。
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;
}
请参考此链接
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 在c++中用vector填充一个简单的动态数组
- 如何使用用户输入在C++中正确填充2D数组
- 导入库可以跨dll版本工作吗
- 如何找到大小'x'数组是否完全填充,在C++?
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 通过for循环使用用户输入填充列表
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- sizeof 函数如何在带和不带位字段的结构上工作?(填充)
- 用对对填充向量未按预期工作
- 在 C++ 中使用递归填充 D2 数组,有时工作正常,其他时会引发异常
- 为什么我的 CUDA 代码无法正常工作,无法零填充大型矩阵
- 用不重复的随机数填充向量.为什么不工作