我正在尝试编写一些代码,在for循环中,我首先在多维向量中推回一个向量,然后在下面的行中从左边插入相同的向量。
但是我从编译器收到错误。
这是我所指代码的一部分。
for(int i2=0;i2<pow(2,NG-4)-1;i2+=2){
newTree.push_back(ROW);
newTree.insert(newTree[i2+1].begin(),ROW.at(0),ROW.end()+1);
}
我正在尝试将向量ROW从第i2行的右侧添加到多维向量newTree。然后我想在左边的第i2 + 1行插入ROW。
有关如何修复它的任何想法?或者更好的想法如何做到这一点?
由于
答案 0 :(得分:0)
对于初学者来说,如果这是您的确切代码,则此行不符合您的预期:
2^(NG-4)-1
那将给你一个2位和(NG-4)的按位XOR,然后减去1.所以如果NG-4 = 5(例如),2 ^(NG-4)= 7.我想你的意思它要做的是:
pow(2, NG - 4) - 1
听起来你想要ROW
插入一次,然后反向插入一次?既然如此,这将有效:
newTree.push_back(ROW);
vector<whateverTypeYouAreUsing> REVROW(ROW.rbegin((), ROW.rend());
newTree.push_back(REVROW);
如果您希望以交替的方式插入向量矢量:
vector<int> v1 {5, 5, 5, 5};
vector<int> v2 {1, 2};
vector<vector<int>> v3;
for (int i = 0; i < v1.size(); i++)
{
vector<int> t;
if (i % 2)
{
copy(v2.begin(), v2.end(), back_inserter<vector<int>>(t));
t.push_back(v1[i]);
}
else
{
t.push_back(v1[i]);
copy(v2.begin(), v2.end(), back_inserter<vector<int>>(t));
}
v3.push_back(t);
}
你也可以在没有循环的情况下完成它,但为了清楚起见,我将把循环留在那里。
对于通用版本:
class MyTree
{
public:
void Initialize(const std::vector<int>& v)
{
for (int i = 0; i < v.size(); i++)
{
std::vector<int> t(1, v[i]);
m_Tree.push_back(t);
}
}
void AddVector(const std::vector<int>& v)
{
for (int i = 0; i < m_Tree.size(); i++)
{
if (i % 2)
{
std::copy(v.begin(), v.end(), std::front_inserter<deque<int>>(m_Tree));
}
else
{
std::copy(v.begin(), v.end(), std::back_inserter<deque<int>>(m_Tree));
}
}
}
std::vector<std::deque<int>> m_Tree;
};
答案 1 :(得分:0)
我解决了“没有匹配的呼叫功能”问题。
现在我得到了一个我根本不理解的错误。
/usr/include/c++/4.3/bits/stl_iterator.h: In member function 'std::front_insert_iterator<_Container>& std::front_insert_iterator<_Container>::operator=(typename _Container::const_reference) [with _Container = std::vector<int, std::allocator<int> >]':
/usr/include/c++/4.3/bits/stl_algobase.h:342: instantiated from 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = int*, _OI = std::front_insert_iterator<std::vector<int, std::allocator<int> > >]'
/usr/include/c++/4.3/bits/stl_algobase.h:396: instantiated from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false, _II = int*, _OI = std::front_insert_iterator<std::vector<int, std::allocator<int> > >]'
/usr/include/c++/4.3/bits/stl_algobase.h:435: instantiated from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false, _II = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _OI = std::front_insert_iterator<std::vector<int, std::allocator<int> > >]'
/usr/include/c++/4.3/bits/stl_algobase.h:466: instantiated from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _OI = std::front_insert_iterator<std::vector<int, std::allocator<int> > >]'
我的新代码是:
vector<int> PI_comb(vector<int> Tree, vector<int> legs, int pi){
vector<int> rooted(NG-1);
for(int i=0;i<NG-1;i++)rooted.at(i)=Tree.at(i);
vector< vector<int> > L;
L.resize(legs.size());
for(int azz=0;azz<legs.size();azz++)L[azz].resize(legs.at(azz));
for(int rho=0;rho<legs.size();rho++){
for(int i=0;i<legs[rho];i++)L[rho][i]=rooted.at(i);
}
vector< vector<int> > newTree/*(std::pow(2,NG-3),vector<int>(NG-1))*/;
for(int cc=0;cc<legs.size();cc++){
if(legs.at(cc)==2){
vector<int> ROW(legs.at(cc));
for(int argh=0;argh<legs.at(cc);argh++)ROW.at(argh)=L[cc][argh];
vector<int> REVROW(ROW.rbegin(), ROW.rend());
for(int i2=0;i2<(std::pow(2,NG-4)-1);i2++){
if (i2%2==0)
{
if (i2==0||i2==4||i2==8)
std::copy(ROW.begin(),ROW.end(),std::back_inserter<vector<int> >(newTree.at(i2)));
else
std::copy(REVROW.begin(),REVROW.end(),std::back_inserter<vector<int> >(newTree.at(i2)));
}
else
{
if (i2==1||i2==5||i2==9)
/*LINE 156*/std::copy(ROW.begin(),ROW.end(),std::front_inserter<vector<int> >(newTree.at(i2)));
else
std::copy(REVROW.begin(),REVROW.end(),std::front_inserter<vector<int> >(newTree.at(i2)));
}
}
for(int i2=std::pow(2,NG-4);i2<(std::pow(2,NG-3)-1);i2+=2){
if(cc>0&&legs.at(cc-1)==2){
if (i2%2==0)
{
if (i2==0||i2==4||i2==8)
std::copy(REVROW.begin(),REVROW.end(),std::back_inserter<vector<int> >(newTree.at(i2)));
else
std::copy(ROW.begin(),ROW.end(),std::back_inserter<vector<int> >(newTree.at(i2)));
}
else
{
if (i2==1||i2==5||i2==9)
std::copy(REVROW.begin(),REVROW.end(),std::front_inserter<vector<int> >(newTree.at(i2)));
else
std::copy(ROW.begin(),ROW.end(),std::front_inserter<vector<int> >(newTree.at(i2)));
}
}
else{
if (i2%2==0)
{
if (i2==0||i2==4||i2==8)
std::copy(ROW.begin(),ROW.end(),std::back_inserter<vector<int> >(newTree.at(i2)));
else
std::copy(REVROW.begin(),REVROW.end(),std::back_inserter<vector<int> >(newTree.at(i2)));
}
else
{
if (i2==1||i2==5||i2==9)
std::copy(ROW.begin(),ROW.end(),std::front_inserter<vector<int> >(newTree.at(i2)));
else
std::copy(REVROW.begin(),REVROW.end(),std::front_inserter<vector<int> >(newTree.at(i2)));
}
}
}
}
else if(legs.at(cc)==1){
vector<int> ROW(legs.at(cc));
for(int argh=0;argh<legs.at(cc);argh++)ROW.at(argh)=L[cc][argh];
if(cc!=legs.size()-1){
for(int i2=0;i2<(std::pow(2,NG-3)-1);i2+=2){
if (i2%2==0)
{
std::copy(ROW.begin(),ROW.end(),std::back_inserter<vector<int> >(newTree.at(i2)));
}
else
{
std::copy(ROW.begin(),ROW.end(),std::front_inserter<vector<int> >(newTree.at(i2)));
}
}
}
else if(cc==legs.size()-1){
for(int i2=0;i2<std::pow(2,NG-3);i2++)newTree.push_back(ROW);
}
}
}
//RETURN
if(pi<=std::pow(2,NG-3)){
for(int ag=0;ag<NG-1;ag++)
Tree.at(ag)=newTree[pi][ag];
}
else if(pi>std::pow(2,NG-3)){
vector<int> row(NG-1);
for(int a=0;a<NG-1;a++)row.at(a)=newTree[pi-std::pow(2,NG-3)][a];
vector<int> REVrow(row.rbegin(), row.rend());
for(int a=0;a<NG-1;a++)Tree.at(a)=REVrow.at(a);
}
return(Tree);
}
这只是一个基本上根据某种特殊模式改变矢量树内元素顺序的函数。 我认为现在它会做我想做的事情。但我不明白这些错误。
好的,我解决了。 vector没有定义front_inserter。
我不得不使用:
copy(ROW.begin(),ROW.end(),inserter(newTree[i2],newTree[i2].begin()));