C ++从向量图中获取向量
c++ get a vector from a map of vectors
我有一张矢量图
map< int, vector<float> > hit = getAlignedHits();
我想获取与特定键配对的向量,例如:
vector<float> vec;
vec = hit[1];
我得到的错误是:
candidate function not
viable: no known conversion from vector<float, allocator<float>>
to const vector<double, allocator<double>>
for
1st argument
vector& operator=(const vector& __x);
我在下面试过,没用:
&vec = hit[1];
error: expression is not assignable
下面我也试过了,没用:
map< int, vector<float> >::iterator itr;
itr = hit.find(1);
&vec = itr->second;
error: expression is not assignable
有谁知道为什么这些不起作用,以及如何从地图中获取矢量?
提前致谢
编辑:
这是 getAlignedHits 所做的以及我在那里使用的变量:
const int NLayer = 6;, vector<float> *hit_position; double alignmentpar[NLayer];
map< int, vector<float> > getAlignedHits(){
double newpos;
for (int i=0; i<NLayer; i++) {
vector<float> bla;
bla.clear();
hit[i] = bla;
}
for (unsigned int ihit=0; ihit<layerID->size(); ihit++) {
newpos = hit_position->at(ihit) - alignmentpar[layerID->at(ihit)];
hit[layerID->at(ihit)].push_back(newpos);
}
}
当然,
&vec = ... <something>
将不起作用,因为您不能将某些内容分配给变量的地址,但是:
candidate function not viable: no known conversion from 'vector>' to 'const vector>' for 1st argument vector& operator=(const vector& __x);
给我的感觉是您正在尝试在 const
函数中或在应用了常量的对象上进行此赋值。请分享更多 "original" 代码,以便我们发现错误。
以下适合我
std::vector<float> & h0 = hit[0];
或更简单
auto & h0 = hit[0];
一个完整的例子
#include <map>
#include <vector>
#include <iostream>
int main()
{
std::map< int, std::vector<float> > hit { { 0, {} } };
std::vector<float> & h0a = hit[0];
auto & h0b = hit[0];
h0a.push_back(2.3);
std::cout << hit[0].front() << std::endl;
std::cout << h0a.front() << std::endl;
std::cout << h0b.front() << std::endl;
return 0;
}
我有一张矢量图
map< int, vector<float> > hit = getAlignedHits();
我想获取与特定键配对的向量,例如:
vector<float> vec;
vec = hit[1];
我得到的错误是:
candidate function not viable: no known conversion from
vector<float, allocator<float>>
toconst vector<double, allocator<double>>
for 1st argumentvector& operator=(const vector& __x);
我在下面试过,没用:
&vec = hit[1];
error: expression is not assignable
下面我也试过了,没用:
map< int, vector<float> >::iterator itr;
itr = hit.find(1);
&vec = itr->second;
error: expression is not assignable
有谁知道为什么这些不起作用,以及如何从地图中获取矢量?
提前致谢
编辑: 这是 getAlignedHits 所做的以及我在那里使用的变量:
const int NLayer = 6;, vector<float> *hit_position; double alignmentpar[NLayer];
map< int, vector<float> > getAlignedHits(){
double newpos;
for (int i=0; i<NLayer; i++) {
vector<float> bla;
bla.clear();
hit[i] = bla;
}
for (unsigned int ihit=0; ihit<layerID->size(); ihit++) {
newpos = hit_position->at(ihit) - alignmentpar[layerID->at(ihit)];
hit[layerID->at(ihit)].push_back(newpos);
}
}
当然,
&vec = ... <something>
将不起作用,因为您不能将某些内容分配给变量的地址,但是:
candidate function not viable: no known conversion from 'vector>' to 'const vector>' for 1st argument vector& operator=(const vector& __x);
给我的感觉是您正在尝试在 const
函数中或在应用了常量的对象上进行此赋值。请分享更多 "original" 代码,以便我们发现错误。
以下适合我
std::vector<float> & h0 = hit[0];
或更简单
auto & h0 = hit[0];
一个完整的例子
#include <map>
#include <vector>
#include <iostream>
int main()
{
std::map< int, std::vector<float> > hit { { 0, {} } };
std::vector<float> & h0a = hit[0];
auto & h0b = hit[0];
h0a.push_back(2.3);
std::cout << hit[0].front() << std::endl;
std::cout << h0a.front() << std::endl;
std::cout << h0b.front() << std::endl;
return 0;
}