C++使用 <algorithm> 和 <functional>从矢量中删除双精度

C++ remove doubles from an vector using <algorithm> and <functional>

本文关键字:gt lt 删除 双精度 使用 algorithm C++ functional      更新时间:2023-10-16

对于学校作业,我需要使用 <algorithm><functional> 来删除向量的所有双元素。这就是我想出的,但它有一个错误。

我做错了什么?

严重性代码说明项目文件行抑制状态 错误 C3867 'std::vector<std::string,std::allocator><_Ty>>::size':非标准语法;使用"&"创建指向成员Opdracht5 c:\users\mike\documents\Visual Studio 2015\projects\opdracht5\opdracht5\main.cpp 38

bool IsEqual(string s, string s2)
{
    if (s == s2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main() {
     vector<string> coulours2 = { "red", "green", "blue", "orange", "purple", "orange", "black", "green" };
     vector<string> removedDoubles;
     removedDoubles.resize(coulours2.size());
     vector<string>::iterator it;
     it = unique_copy(removedDoubles.begin(), it, removedDoubles.begin(), IsEqual);
     removedDoubles.resize(distance(removedDoubles.begin(), it));
     return 0;
}

找到了遮阳篷。

 vector<string> coulours2 = { "red", "green", "blue", "orange", "purple", "orange", "black", "green" };
    sort(coulours2.begin(), coulours2.end());
    coulours2.erase(unique(coulours2.begin(), coulours2.end()), coulours2.end());