gpt4 book ai didi

c++ - 初始化 std::pair, 3>>

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:49 25 4
gpt4 key购买 nike

谁能建议以下代码中 std::vector::push_back 调用中 std::make_pair 调用的正确语法:

#include <array>
#include <vector>
#include <utility>

int main()
{
typedef std::pair<double, double> PairType;
std::vector<std::pair<double, std::array<PairType, 3> > > myVector;

double Key = 0.0;
PairType Pair1 = std::make_pair(1.0, 2.0);
PairType Pair2 = std::make_pair(3.0, 4.0);
PairType Pair3 = std::make_pair(5.0, 6.0);

myVector.push_back(std::make_pair(Key, { Pair1, Pair2, Pair3 } )); // Syntax Error

return 0;
}

编译器(MS VS2015.2)无法确定std::make_pair调用中第二个参数的类型,这是可以理解的,但我不知道如何启发它。

最佳答案

看起来编译器无法确定 { Pair1, Pair2, Pair3 } 是三对的 std::array。明确指定类型应该有效:

myVector.push_back(std::make_pair(Key, std::array<PairType,3>{ Pair1, Pair2, Pair3 } ));

Demo.

关于c++ - 初始化 std::pair<double, std::array<std::pair<double, double>, 3>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39995343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com