c++ - 删除引用调用 vector 的重复项

标签 c++ vector

<分区>

我正在测试以下代码,其输出必须是:

1264
1234
126
123
78
15
12
7
1

但是输出是:

1264
1234
126
123
78
15
12
7
1

1
1

很明显,我的 organizePrefixClosureBank 不起作用,但我找不到原因。

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

using namespace std;

vector<string> getPrefixClosure (string path)
{
    vector<string> output;
    for (int i=1; i<path.length()+1; i++)
    {
        output.push_back(path.substr(0,i));
    }
    return output;
}

vector<string> getAllPrefixClosures (vector<string> path)
{
    vector<string> output;
    for (int i=0; i<path.size(); i++)
    {
        vector<string> temp = getPrefixClosure(path[i]);
        output.insert(output.end(), temp.begin(), temp.end());
    }
    return output;
}

void organizePrefixClosureBank(vector<string>& prefixClosureBank)
{
    ////// first sort in ascending order
    sort(
                prefixClosureBank.begin(),
                prefixClosureBank.end(),
            [](const std::string& a, const std::string& b) {
                return a.size() < b.size() || a.size() == b.size() && a < b;
        });

        reverse(prefixClosureBank.begin(),prefixClosureBank.end());

    ////// then remove duplicates
        unique(prefixClosureBank.begin(),prefixClosureBank.end());
}

int main()
{
    vector<string> prefixClosureBank;

    vector<string> FPCollection;
    FPCollection.push_back("1234");
    FPCollection.push_back("15");
    FPCollection.push_back("1264");
    FPCollection.push_back("78");

//////////////////////////////////////////////////////////
    // sort the vector in ascending order
    sort(
            FPCollection.begin(),
            FPCollection.end(),
        [](const std::string& a, const std::string& b) {
            return a.size() < b.size() || a.size() == b.size() && a < b;
    });

    // now the reverse it to descending order

    reverse(FPCollection.begin(),FPCollection.end());
//////////////////////////////////////////////////////////

    prefixClosureBank = getAllPrefixClosures(FPCollection);
    organizePrefixClosureBank(prefixClosureBank);


        for (int i=0; i<prefixClosureBank.size(); i++)
            {
                cout << prefixClosureBank[i] << endl;
            }

    return 0;
}

你能给我一些建议来解决这个问题吗?!

最佳答案

首先删除重复项,然后才反转。也删除不必要的元素:

void organizePrefixClosureBank(vector<string>& prefixClosureBank)
{
    ////// first sort in ascending order
    sort(
                prefixClosureBank.begin(),
                prefixClosureBank.end(),
            [](const std::string& a, const std::string& b) {
                return a.size() < b.size() || (a.size() == b.size() && a < b);
        });
   prefixClosureBank.erase( unique( prefixClosureBank.begin(), prefixClosureBank.end() ), prefixClosureBank.end() );
   reverse(prefixClosureBank.begin(),prefixClosureBank.end());

}

关于c++ - 删除引用调用 vector 的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45558591/

相关文章:

c++ - 如何连接多个CString

c++ - 在Windows 10上构建并在Windows 7上运行时,为什么会出现 “Illegal Instruction”错误

c++ - Android on Samsung s5230 star (for C++)

r - 将向量转换为 data.frame,每个唯一值对应一列

java - 将 Vec4i 转换为 Java openCV

c++指向 vector 内部 float 的指针

android - Djinni 能否从 C++ 全局函数生成必要的代码?

c++ - 如何从 vector 中删除 shared_ptr

java - Java中如何按多个字段对对象进行排序?

c++ - vector 的 vector 作为高效的数据结构; std::vector<std::vector<someType>> 的替代品