C++ wymazać element wektorowy według wartości, a nie według pozycji? [duplikat]

to pytanie ma już odpowiedzi tutaj : Jak usunąć element z wektora stl o określonej wartości? (11 odpowiedzi) Zamknięty 4 lata temu .
vector<int> myVector;

I powiedzmy, że wartości w wektorze są takie (w tej kolejności):

5 9 2 8 0 7

Gdybym chciał usunąć element, który zawiera wartość "8", myślę, że zrobiłbym to:

myVector.erase(myVector.begin()+4);

Ponieważ to by wymazało czwarty element. Ale czy jest jakiś sposób, aby usunąć element oparty na wartości "8"? Like:

myVector.eraseElementWhoseValueIs(8);

Czy po prostu muszę przejść przez wszystkie elementy wektorowe i przetestować ich wartości?

Author: Guy Avraham, 2010-08-02

4 answers

A może std::remove() zamiast:

#include <algorithm>
...
vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end());
Ta kombinacja jest również znana jako idiom erase-remove.
 467
Author: Georg Fritzsche,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-24 19:57:17

Możesz użyć std::find, Aby uzyskać iterator do wartości:

#include <algorithm>
std::vector<int>::iterator position = std::find(myVector.begin(), myVector.end(), 8);
if (position != myVector.end()) // == myVector.end() means the element was not found
    myVector.erase(position);
 115
Author: zneak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-15 16:20:39

Nie możesz tego zrobić bezpośrednio. Musisz użyć algorytmu std::remove, aby przenieść element do usunięcia na koniec wektora, a następnie użyć funkcji erase. Coś w stylu: myVector.erase(std::remove(myVector.begin(), myVector.end(), 8), myVec.end());. Zobacz to wymazywanie elementów z wektora Po Więcej Szczegółów.

 13
Author: Naveen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:33

Eric Niebler pracuje nad propozycją zakresu i niektóre z przykładów pokazują, jak usunąć pewne elementy. Usunięcie 8. Tworzy nowy wektor.

#include <iostream>
#include <range/v3/all.hpp>

int main(int argc, char const *argv[])
{
    std::vector<int> vi{2,4,6,8,10};
    for (auto& i : vi) {
        std::cout << i << std::endl;
    }
    std::cout << "-----" << std::endl;
    std::vector<int> vim = vi | ranges::view::remove_if([](int i){return i == 8;});
    for (auto& i : vim) {
        std::cout << i << std::endl;
    }
    return 0;
}

Wyjścia

2
4
6
8
10
-----
2
4
6
10

 1
Author: kometen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-02 11:21:13