简体   繁体   English

从2D矢量c ++中删除元素

[英]Erasing an element from 2D vector c++

I have a unsymmetrical vector in 2D. 我在2D中有一个不对称的矢量。

vector< vector<int> > Test

where Test = 其中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. 我正在读取第1行,如果其他行中存在任何元素,则将其删除。 for eaxmple.. After reading row 1, i have to remove 6, 5, and 7 from other rows. 对于eaxmple ..在读取第1行后,我必须从其他行中删除6,5和7。

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. 其中i =行,j是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 ? 你认为Test[i].begin()+j究竟是什么? Is ist a set of elements you want to erase? ist是一组要删除的元素吗? 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. 您可以为每个可能的i执行代码。 Maybe start form i = 0 to n . 也许从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. Edit2:更改了第一个循环的索引并添加了continue语句。

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 考虑以下2D矢量

myVector= myVector =

1 2 3 4 5 -6 1 2 3 4 5 -6

6 7 8 -9 6 7 8 -9

8 -1 -2 1 0 8 -1 -2 1 0

Suppose we want to delete the element myVector[row][column] for appropriate row and column indices. 假设我们要删除元素myVector [row] [column]以获取适当的行和列索引。

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. 现在,由于这个2D向量是向量的向量,我们将向量(包含要删除的元素的行)和列作为参数传递给函数。 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. 现在问题变得非常简单,就像从1D向量中删除元素一样简单。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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