简体   繁体   中英

Erasing an element from 2D vector c++

I have a unsymmetrical vector in 2D.

vector< vector<int> > Test

where Test =

         2 4 6 5 7 
         6 5 7 9 10
         5 9 10
         9 10

I am reading the row 1 and if any element of this is present in other rows then delete it. for eaxmple.. After reading row 1, i have to remove 6, 5, and 7 from other rows.

However, It is not working

Here is the code i am trying

Test[i].erase(Test[i].begin()+j);

where i = row and j is col.

My code is :

for (i =0; i < Test.size();i++)
        {
        for (j=0; j < Test[i].size();j++)
                {
                // removed repeated element
                if (i >0)
                        {
                        Test[i].erase(Test[i].begin() +j);
                        }
                }
        }

What exactly do you think is Test[i].begin()+j ? Is ist a set of elements you want to erase? I don't think so. It should be just an iterator, that points to a single element , but you want to delete all elements , that are already in your datastructure.

If I understood, what you want to do, try:

for(int j = 0; j < Test.size(); j++){            //iterate over other rows
    if(j == i)
        continue;
    for(int k = 0; k < Test[j].size(); k++){     //iterate over elements of the rows
        int elementToRemove = (Test[j])[k];
        vector<int>::iterator it = Test[i].begin();
        while (it != Test[i].end()) {            //iterate over row i
           if((*it) == elementToRemove){         //erase the element if it matches the actual
               it = Test[i].erase(it);    
           }else{
               it++;
           }
        }
    }
}

You could execute the code for every possible i. Maybe start form i = 0 to n . If I refer to your code, that you added put the code above in between your

for (i =0; i < Test.size();i++){
    //my code here...
}

Edit: Used iterator now to delete. The first version was not correct.

Edit2: Changed the index of the first loop and added continue statement.

Maybe it is nor very nice but it works

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() 
{
    std::vector<std::vector<int>> v =
    {
        { 2, 4, 6, 5, 7 }, 
        { 6, 5, 7, 9, 10 },
        { 5, 9, 10 },
        { 9, 10 }
    };

    for ( const auto &row : v )
    {
        for ( int x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }

    std::cout << std::endl;

    if ( !v.empty() )
    {
        for ( auto it = std::next( v.begin() ); it != v.end(); ++it )
        {
            auto is_present = [&]( int x )
            {
                return std::find_if( v.begin(), it,
                    [x]( const std::vector<int> &v1 )
                    {
                        return std::find( v1.begin(), v1.end(), x ) != v1.end();
                    } ) != it; 
            };

            it->erase( std::remove_if( it->begin(), it->end(), is_present ), 
                       it->end() );
        }
    }


    for ( const auto &row : v )
    {
        for ( int x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }

    return 0;
}

The output is

2 4 6 5 7 
6 5 7 9 10 
5 9 10 
9 10 

2 4 6 5 7 
9 10 

You can place the values encountered in each row in a set, and then query every element in a new row for existence in that set. Such a function would look like this :

void RemoveRowDuplicates(vector<vector<int>> &v)
{
    std::set<int> vals;

    for(auto &vec : v)
    {
        vec.erase(remove_if(vec.begin(), vec.end(), [&](int k){
            return vals.find(k) != vals.end();
        }), vec.end());
        vals.insert(vec.begin(), vec.end());
    }
}

This works for me:

int i = 0;
for ( int j = i+1; j < Test.size(); ++j )
{
   for ( int k = 0; k < Test[i].size(); ++k )
   {
      std::vector<int>::iterator iter = Test[j].begin();
      std::vector<int>::iterator end = Test[j].end();
      for ( ; iter != end; )
      {
         if ( *iter == Test[i][k] )
         {
            iter = Test[j].erase(iter);
         }
         else
         {
            ++iter;
         }
      }
   }
}

Consider the following 2D vector

myVector=

1 2 3 4 5 -6

6 7 8 -9

8 -1 -2 1 0

Suppose we want to delete the element myVector[row][column] for appropriate row and column indices.

Look at the following code:

void delete_element(vector<int>& temp, col) 
  {
     temp.erase(temp.begin()+col);
  }

int main()
 {
  //Assume that the vector 'myVector' is already present.
    cin>>row>>column;
    delete_element(myVector[row],column);
 } 

What we basically do is,we get the row and column of the element to be deleted. Now as this 2D vector is a vector of vectors, we pass the vector(the row containing the element to be deleted) and the column as parameters to a function. Note that the row-vector is passed as a reference ('&' in vector parameter). Now the problem becomes quite as simple as deleting an element from a 1D vector.

Hope this helps!

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