2013-07-09 11 views
0

我有一个向量向量。我想阅读它的第一个组件,并且如果它小于一个特定的值,或者它比另一个值大,就从vector中删除它。我怎样才能做到这一点? 我的代码是:来自两个不同文件的关联值

int d = sum_et.size();     
vector <float>sum_et          
vector <float>sum_ieta_iphi; 
vector <vector<float> >v; 
sum_et.push_back(energySum); 
sum_ieta_iphi[0]=energySum; 
sum_ieta_iphi[1]=ieta; 
sum_ieta_iphi[2]=iphi; 
v.push_back(sum_ieta_iphi); 
float max,min; 
max=sum_et[(int)(19/20*d)]; 
min=sum_et[(int)(d/20)]; 


for (int i=0;i<v.size();i++){ 
/* line 312 */ if (v[i[0][0][0]]<min || v[i[0][0][0]]>max){ 
/* line 313 */  v.erase(v[i]); 
    } 
} 

我得到这些错误:

Analysis.cc:312:16: error: invalid types 'int[int]' for array subscript 
Analysis.cc:312:37: error: invalid types 'int[int]' for array subscript 
Analysis.cc:313:14: error: no matching function for call to 'std::vector<std::vector<float> >::erase(std::vector<float>&)' 
+0

具体哪行'312'和'313'? – trojanfoe

+0

if()和v.erase的行 – Lunatica

回答

0

你在这里做一些奇怪的事情:

v[i[0][0][0]] 

特别i是一个普通的int,这给错误对于“数组subscrit的无效类型”行312。

除此之外,你有很多[0]这个工作正常,我认为。

+0

我知道...但我不知道如何访问矢量的矢量组件... – Lunatica

+0

'v [x] [y]'会给你是第x行,第y列。 –

+0

确定它删除if条件中的错误... 现在你知道我可以如何删除此元素的矢量? – Lunatica

2

问题是你索引的东西(整数变量i)不是一个向量。


还有更好的方法来做到这一点。请记住,C++在标准库中有许多很好的algorithms,例如std::copy_if,如果谓词为真,则从一个集合复制到另一个集合。

这可以用来矢量超过自身复制:

std::copy_if(std::begin(v), std::end(v), std::begin(v), 
      [v, min, max](const vector<float>& value) 
      { return v[value[0]] >= min && v[value[0]] <= max; }); 
0

有两个问题在你的代码:i是一个整数,所以i[0][0][0]是无稽之谈。 另外,函数erase需要一个迭代器,而不是一个值。 (cf the reference

如果我没有弄错,你想检查每个子向量的第一个元素,如果它匹配条件,删除子向量。

正如alexisdm在评论中指出的那样,我建议的for循环首先忽略了元素。它应该是:

// We get an iterator on the first vector 
vector<vector<float> >::iterator it = v.begin(); 
while(it != v.end()) 
{ 
    // We check the first element of the subvector pointed by the iterator 
    if ((*it).at(0) < min || (*it).at(0) > max) 
    { 
     // We erase the subvector of v and erase returns 
     // an iterator to the next element 
     it = v.erase(it); 
    } 
    else 
    { 
     // We go to next element 
     it++; 
    } 
} 

第一不工作循环是:

// We get an iterator on the first vector 
for(vector<vector<float> >::iterator it = v.begin(); it != v.end(); ++it) 
{ 
    // We check the first element of the subvector pointed by the iterator 
    if ((*it).at(0) < min || (*it).at(0) > max) 
    { 
     // We erase the subvector of v 
     it = v.erase(it); 
    } 
} 
+0

mmm我想检查我的子向量的第一个元素的所有值...我写的是正确的? 我在添加代码cicle如下所示: 对(INT I = 0; I max){ for(vector > :: iterator it = v.begin();它!= v。结束(); ++(it it){ \t if((* it).at(0) max){ \t it = erase(it); \t} \t} }} } 但 我有这样的错误: 擦除”在此范围 – Lunatica

+0

对不起,我不能写清楚模式的代码未声明。我希望你能理解... – Lunatica

+0

我建议的循环是代替循环,而不是内部。我很抱歉,这是'v.erase'而不是'era​​se'。我编辑了我的答案 – Levans

相关问题