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

C++ 如何将find_first_not_of与字符串向量一起使用?,c++,stdvector,stl-algorithm,C++,Stdvector,Stl Algorithm,假设我有以下目标: vector<string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"}; 矢量数据={“12”、“12”、“12”、“12”、“13”、“14”、“15”、“15”、“15”、“18”}; 我试图在数据对象中找到第一个非重复项。 例如,data.find_first_not_of(data.at(0));如果数据仅为字符串类型(无容器),则这将起作用 如何使用vect

假设我有以下目标:

vector<string> data = {"12","12","12","12","13","14","15", "15", "15", "15", "18"};
矢量数据={“12”、“12”、“12”、“12”、“13”、“14”、“15”、“15”、“15”、“18”};
我试图在数据对象中找到第一个非重复项。 例如,data.find_first_not_of(data.at(0));如果数据仅为字符串类型(无容器),则这将起作用

如何使用vector类型的对象实现相同的功能

我查看了相邻的算法库find和find-if-u,但没有结果


非常感谢您的建议。

您发现相邻的
有什么问题?您应该能够将其与反向谓词一起使用:

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;
} 
std::矢量数据={“12”、“12”、“12”、“12”、“13”、“14”、“15”、“15”、“15”、“18”};
//如有必要,在此处对数据进行排序
auto-itr=std::相邻查找(data.cbegin(),data.cend(),std::不等于{});
如果(itr!=data.cend()){

std::cout由于您必须至少查看一次列表,并且您不知道何时何地会遇到重复的数字(如果有),解决此问题的一种方法是首先收集“统计数据”,然后根据您收集的数据确定第一个非重复数

下面是一个使用
std::无序\u映射的示例:

#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";
    }
#包括

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


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

数据总是被排序吗?您只是想要标题中的
向量
,还是任意的
向量
,或者
向量
,或者其他什么?这不是真的你的问题非常清楚。不幸的是,不是!我不能搞乱元素的顺序,它必须是原来的样子。@只不过是一个向量。在你给出的示例中,答案应该是什么?这与OP的结果相符,但与他们声称的不符。我没有使用not_equal_to作为谓词。我得到的结果只适用于两个条目。示例r报告该试样“00”、“00”、“00”、“11”的“所有元素相等”。这不是检查最后一个元素,知道为什么吗?@Xigma对我来说很好:我明白了!实际上是我的代码错误填充了数据对象。它没有使用最后一个元素。谢谢你提供了优雅的解决方案。谢谢你提供了这种方法。但是,我相信这对于我想要实现的目标来说是一种过分的手段。