Search code examples
c++stdvectorstl-algorithm

How to use find_first_not_of with a vector of string?


Let's say I have the following object:

vector<string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"};

I'm trying to find the first non-repeating entry in the data object. For example, data.find_first_not_of(data.at(0)); this would work if data is of string type only (no container).

How can I achieve the same thing with an object of type vector.

I looked at adjacent_find and find_if_not from the algorithm library, but to no avail.

Your suggestions are much appreciated.


Solution

  • What problem did you have with adjacent_find? You should be able to use that with an inverse predicate:

    std::vector<std::string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"};
    
    // Sort data here if necessary
    
    auto itr = std::adjacent_find(data.cbegin(), data.cend(), std::not_equal_to<std::string>{});
    if (itr != data.cend()) {
        std::cout << "First mismatch: " << *itr << " " << *std::next(itr) << std::endl;
    } else {
        std::cout << "All elements equal" << std::endl;
    } 
    

    Wandbox