简体   繁体   中英

Insert with multidimensional vector

I am trying to write some code where, inside a for loop, I first push back a vector inside a multidimensional vector, and then insert the same vector from the left in the row below.

However I get a error from the compiler.

This is the part of the code I am referring to.

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

I am trying to add the vector ROW to the multidimensional vector newTree from the right at the line i2. Then I want to insert ROW from the left at line i2+1.

Any idea on how to fix it? Or any better idea on how to do it?

Thanks

For starters, if that is your exact code, this line doesn't do what you think it does:

2^(NG-4)-1

That will give you a bitwise XOR with 2 and (NG-4) and then subract 1. So if NG-4 = 5 (for example), 2^(NG-4) = 7. I think what you meant for it to do is:

pow(2, NG - 4) - 1

It sounds like you want ROW inserted once, and then it's reverse inserted once? That being the case, this would work:

newTree.push_back(ROW);
vector<whateverTypeYouAreUsing> REVROW(ROW.rbegin((), ROW.rend());
newTree.push_back(REVROW);

If you are looking to insert into a vector of vectors in alternating fashion:

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

You could also do it without the loop, but for clarity I'll leave the loop there.

For a generalized version:

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

I solved the "no matching function for call" problem.

Now I get this error that I don't understand at all.

/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> > >]'

My new code is:

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

}

This is just a function that basically changes the order of the elements inside the vector Tree according to some special pattern. I think that now it would do what I want it to do..but I don't understand those errors.

Ok, I solved it. vector doesn't have front_inserter defined.

I had to use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM