删除被引用的引用向量的重复

时间:2017-08-08 03:00:04

标签: 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;
}

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

2 个答案:

答案 0 :(得分:2)

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

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());

}

答案 1 :(得分:1)

发现错误只需更改以下行

  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
        auto last = unique(prefixClosureBank.begin(),prefixClosureBank.end());
        prefixClosureBank.erase(last, prefixClosureBank.end());
        prefixClosureBank.shrink_to_fit();
    }