c++ - std::make_pair with float array (float2, unsigned int)

标签 c++ arrays std-pair

我有一个带有“float2, unsigned int”对的 vector 模板,例如:

std::vector<std::pair<float2, unsigned int>> myVec;

然后我尝试将这样的一对添加到 vector 中:

unsigned int j = 0;
float2 ab = {1.0, 2.0};
myVec.push_back(std::make_pair(ab, j));

这是我期望的工作方式,但当我尝试编译它时出现错误:

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(163): error C2536: 'std::_Pair_base<_Ty1,_Ty2>::std::_Pair_base<_Ty1,_Ty2>::first' : cannot specify explicit initializer for arrays
1>          with
1>          [
1>              _Ty1=float2 ,
1>              _Ty2=unsigned int
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(166) : see declaration of 'std::_Pair_base<_Ty1,_Ty2>::first'
1>          with
1>          [
1>              _Ty1=float2 ,
1>              _Ty2=unsigned int
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(247) : see reference to function template instantiation 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base<float(&)[2],unsigned int&>(_Other1,_Other2)' being compiled
1>          with
1>          [
1>              _Ty1=float2 ,
1>              _Ty2=unsigned int,
1>              _Other1=float (&)[2],
1>              _Other2=unsigned int &
1>          ]
1>          myTest.cpp(257) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<float2(&),unsigned int&>(_Other1,_Other2)' being compiled
1>          with
1>          [
1>              _Ty1=float2,
1>              _Ty2=unsigned int,
1>              _Other1=float2 (&),
1>              _Other2=unsigned int &
1>          ]**strong text**

将此数据类型添加到我的对保持 vector 中的正确方法是什么?

float2 类型定义为:

typedef float        float2[2];

最佳答案

C++ 数组几乎在每次使用时都会衰减为指针。更改 float2:

typedef std::array<float, 2> float2;

或者,如果您还没有 C++11,您可以使用 boost::array

关于c++ - std::make_pair with float array (float2, unsigned int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18814929/

相关文章:

c++ - 按字符串对 std::vector<std::pair<std::string,bool>> 进行排序?

c++ - 遍历对列表的 vector

c++ - StringCchCat 用于在 VC++ 中连接整数和字符串

c++ - 需要帮助理解这个简短的 C++ 程序及其漏洞

c++ - 指向 C 字符串的指针?

c++ - 修正剪线算法的代码

java - Java 中使用 BigDecimal 的二维数组

c - 对数组进行排序的有效方法(排序方法必须是从数组中选取一个元素并将其放置在数组的其他位置)

c++ - 任何 STL 数据结构,如 pair 提供三个项目(类型)而不是两个?

java - 字典序最小排列,使得所有相邻字母都不同