C++是唯一的,并且设置不起作用

C++ unique and set not working

本文关键字:设置 不起作用 唯一 C++      更新时间:2023-10-16

unique不起作用,请提供帮助其显示的编译器错误

我试着把它做成

set<str> se(ss.begin(), ss.end());
ss.assign(se.begin(), se.end());

我也试过了,它还显示了编译器错误

是因为bool sortByString()

我在一个页面上看到了这段代码,它有助于对向量类对象进行排序如果有其他方法,请帮助

#include <cmath>
#include<set>
#include <cstdio> 
#include <vector> 
#include <iostream> 
#include<string> 
#include <algorithm>
using namespace std; 
int n;
class str
{
    public:
    string a;
    void in(string s)
    {
        a=s; 
    }
    string get(){
        return a;
    }
    void out()
    {
        cout<<a;
    }
};

bool sortByString(str &t1, str &t2)
{
    return t1.get() < t2.get();
}

string d(vector<str> a) 
{
    string s;

    for(int i=0;i<n;i++)
        s.append(a[i].get());
    return s;
}

int main() {
    string s,sub; 
    cin >> s;
    int length = s.length();
    int i, k = 0, c;
    vector<str> ss;
    str a;
    n = length*((length + 1) / 2); 

    k = 0;

    for (c = 0; c < length; c++)
    {
        for (i =length-c;i>=1; i--)
        {

            a.in(s.substr(c,i));
            ss.push_back(a);
        }
    }
    s=" ";
    ss.erase(unique(ss.begin(),ss.end()),ss.end());  /*code giving compiler error pls help*/
      s=d(ss);
    cout<<s;
    return 0; 
}

您在str.std:中缺少operator==:unique需要此运算符。

class str
{
    //....
    bool operator==(const str& rop) const {
        return a == rop.a;
    }
};
bool sortByString(str &t1, str &t2)
bool sortByString(const str &t1, const str &t2)

至少对于g++。我想VS会编译任何一个。