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

醉酒当歌 提交于 2019-12-24 00:46:08

问题


I have a vector templated with a 'float2, unsigned int'-pair like:

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

And then I'm trying to add such a pair to the vector:

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

This is how I expect it should work, though when I try to compile it I get the error:

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**

What is the correct way to add this data type to my pair holding vector?

The float2 type is defined as:

typedef float        float2[2];

回答1:


C++ arrays decay to pointers on almost every use. Change float2:

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

Or, if you don't have C++11 yet, you can use boost::array.




回答2:


This doesn't work because arrays can't be copied like regular values.

Ideally you would use something else -- maybe a std::array or have your float2 inside a struct.

If you absolutely have to have a pair of this type, then you can do this:

unsigned int j = 0;
float2 ab = {1.0, 2.0};

std::pair<float2,int> new_element;
new_element.first[0] = ab[0];
new_element.first[1] = ab[1];
new_element.second = j;

myVec.push_back(new_element);

If you have to do this a lot, you can make a function. Here is a complete example:

#include <vector>
#include <utility>

typedef float float2[2];

std::pair<float2,unsigned int>
  make_pair(const float2 &first,unsigned int second)
{
  std::pair<float2,unsigned int> result;
  result.first[0] = first[0];
  result.first[1] = first[1];
  result.second = second;
  return result;
}

int main(int,char**)
{
  unsigned int j = 0;
  float2 ab = {1.0, 2.0};
  std::vector<std::pair<float2, unsigned int> > myVec;
  myVec.push_back(make_pair(ab, j));
  return 0;
}


来源:https://stackoverflow.com/questions/18814929/stdmake-pair-with-float-array-float2-unsigned-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!