使用多维矢量插入

Insert with multidimensional vector

本文关键字:插入      更新时间:2023-10-16

我正在尝试编写一些代码,在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添加到多维向量newTree中,从右边的i2行开始。然后我想在i2+1行从左边插入ROW。

你知道怎么修吗?或者有什么更好的方法吗?

感谢

对于初学者来说,如果这是你的确切代码,那么这一行不会做你认为它会做的事情:

2^(NG-4)-1

这将给你一个与2和(NG-4)的逐位异或,然后是子行为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;
};

我解决了"调用没有匹配函数"的问题。

现在我犯了一个我根本不理解的错误。

/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);
}

这只是一个函数,它基本上根据一些特殊的模式来改变向量树中元素的顺序。我想现在它会做我想做的事……但我不理解那些错误。

好的,我解决了。向量没有定义front_inserter。

我不得不使用:

copy(ROW.begin(),ROW.end(),inserter(newTree[i2],newTree[i2].begin()));