-3

I have 2 string vectors:

vector < string > animals = {"cat", "dog", "pig", "tiger", "monkey", "lion"}
vector < string > someAnimals = {"dog", "mouse", "snake", "monkey", "cat"}

How can I compare these 2 vectors and remove elements in someAnimals vector("mouse" and "snake") that aren't found in the animals Vector?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Jingkai
  • 13
  • 2
  • Possibly a dup of: https://stackoverflow.com/questions/14175858/c-subtract-vectors – Eljay Jan 27 '20 at 22:54
  • If I understand what you want, I think you should read [this post](https://stackoverflow.com/questions/19483663/vector-intersection-in-c). – Hyldrean Jan 27 '20 at 22:58
  • Does this answer your question? [C++: subtract vectors](https://stackoverflow.com/questions/14175858/c-subtract-vectors) – NicholasM Jan 27 '20 at 23:01
  • Sample code [here](https://en.cppreference.com/w/cpp/algorithm/set_intersection). – wally Jan 27 '20 at 23:02
  • 1
    Please show the work you've already written so far, and explain how exactly your program doesn't work or doesn't produce the expected results. You have to show your work first, and it must be a good-faith real attempt to implement your task and not a few token lines of code, before asking for help on stackoverflow.com. For more information, see [ask] questions, take the [tour], and read the [help]. – Sam Varshavchik Jan 27 '20 at 23:06
  • Have a look at [`std::remove_if()`](https://en.cppreference.com/w/cpp/algorithm/remove) – Remy Lebeau Jan 27 '20 at 23:06

1 Answers1

0

If you are not allowed to sort the vectors then you can use the following approach as it is shown in the demonstrative program below.

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

int main() 
{
    std::vector <std::string> animals = 
    {
        "cat", "dog", "pig", "tiger", "monkey", "lion"
    };

    std::vector <std::string> someAnimals = 
    {
        "dog", "mouse", "snake", "monkey", "cat"
    };

    auto not_present = [&animals]( const auto &s )
    {
        return 
        std::find( std::begin( animals ), std::end( animals ), s ) == std::end( animals );
    };

    someAnimals.erase( std::remove_if( std::begin( someAnimals ),
                                       std::end( someAnimals ),
                                       not_present ), std::end( someAnimals ) );

    for ( const auto &s : someAnimals )
    {
        std::cout << s << ' ';
    }
    std::cout << '\n';

    return 0;
}

The program output is

dog monkey cat 

Otherwise you can use std::binary_search for the sorted vectors as shown below.

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

int main() 
{
    std::vector <std::string> animals = 
    {
        "cat", "dog", "pig", "tiger", "monkey", "lion"
    };

    std::vector <std::string> someAnimals = 
    {
        "dog", "mouse", "snake", "monkey", "cat"
    };

    std::sort( std::begin( animals ), std::end( animals ) );
    std::sort( std::begin( someAnimals ), std::end( someAnimals ) );

    auto not_present = [&animals]( const auto &s )
    {
        return 
        not std::binary_search( std::begin( animals ), std::end( animals ), s );
    };

    someAnimals.erase( std::remove_if( std::begin( someAnimals ),
                                       std::end( someAnimals ),
                                       not_present ), std::end( someAnimals ) );

    for ( const auto &s : someAnimals )
    {
        std::cout << s << ' ';
    }
    std::cout << '\n';

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335