如何将 find_first_not_of 与字符串向量一起使用?

How to use find_first_not_of with a vector of string?

假设我有以下对象:

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

我正在尝试查找数据对象中的第一个非重复条目。 例如,data.find_first_not_of(data.at(0));如果数据仅为字符串类型(无容器),这将起作用。

如何使用向量类型的对象实现相同的目的。

我从算法库中查看了adjacent_find和find_if_not,但没有用。

非常感谢您的建议。

您在使用 adjacent_find 时遇到了什么问题?您应该能够将其与逆谓词一起使用:

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

由于您必须至少遍历列表一次,并且您不知道何时何地会遇到重复的数字(如果有的话),解决此问题的一种方法是首先收集"statistics" 然后根据您收集的信息,您可以确定第一个非重复项。

这里是一个使用 std::unordered_map 的例子:

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

// struct to hold some information on the numbers
struct info
{
    std::string number;
    int count;
    int position;
    info(const std::string n, int c, int p) : number(n), count(c), position(p) {}
};

int main()
{

    std::vector<std::string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"};
    std::unordered_map<std::string, info> infoMap;
    std::vector<info> vInfo;
    int pos = 0;

    // loop for each data element
    std::for_each(data.begin(), data.end(), [&](const std::string& n) 
    { 
        // insert entry into the map
        auto pr = infoMap.insert(std::make_pair(n, info(n, 0, pos)));    

        // bump up the count for this entry.
        ++pr.first->second.count;

        // bump up the postion number
        ++pos;

    });

    // create a vector of the information with a count of 1 item.
    std::for_each(infoMap.begin(), infoMap.end(), [&](std::unordered_map<std::string, info>::value_type& vt) { if (vt.second.count == 1) vInfo.push_back(vt.second); });

    // sort this by position
    std::sort(vInfo.begin(), vInfo.end(), [&](const info& pr1, const info &pr2){return pr1.position < pr2.position; });

   // output the results
    if ( vInfo.empty() )
        std::cout << "All values are duplicated\n";
    else  
        std::cout << "The first number that isn't repeated is " << vInfo.front().number << "\n";
    }

Live Example

首先,我们只是简单地遍历向量中的所有条目,然后计算每个项目的计数。此外,我们将找到该项目的位置存储在原始列表中。

之后我们过滤掉计数恰好为1的那些并将它们复制到一个向量中。然后我们根据它们在原始列表中的位置对这个向量进行排序。