Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to erase element in 2d vector?

Tags:

c++

I want to delete an element in a 2d vector, say [1][1] in Field, which is a 4x5 vector

I tried

Field.erase([1][1]);

I know that for a 1d vector, I would just do

Field.erase (Field.begin()+1)

but what about for 2d vectors?

like image 900
M K Avatar asked Oct 18 '11 00:10

M K


People also ask

How do I remove one element from a vector?

All the elements of the vector are removed using clear() function. erase() function, on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed.

How do I remove a string from a vector?

To remove a single copy of an element from a std::vector , you can use std::find and the std::vector 's erase member function like this: auto itr = std::find(v. begin(), v. end(), rnames); if (itr !=


1 Answers

Field[1].erase(Field[1].begin() + 1);

That's how you erase an element. But by your comments, that's not what you really want. What you actually want is to clear the value of the element to it's default value. For that:

Field[1][1] = Card();
like image 70
Benjamin Lindley Avatar answered Oct 21 '22 10:10

Benjamin Lindley