C++ C++;设置唯一性和顺序

C++ C++;设置唯一性和顺序,c++,sorting,set,compare,unique,C++,Sorting,Set,Compare,Unique,我想在集合中创建唯一和顺序 在下面的代码中,我希望a是唯一的,并按b和c排序。 因此,没有相同的foo.a和订单依据foo.b和foo.c 我该怎么做 struct Foo { int a, b, c; Foo(int a, int b, int c) : a(a), b(b), c(c) {} } struct FooComp { bool operator() (const Foo& f, const Foo& s) const {

我想在
集合
中创建唯一和顺序

在下面的代码中,我希望a是唯一的,并按b和c排序。 因此,没有相同的
foo.a
和订单依据
foo.b
foo.c

我该怎么做

struct Foo {
    int a, b, c;
    Foo(int a, int b, int c) : a(a), b(b), c(c) {}
}

struct FooComp {
    bool operator() (const Foo& f, const Foo& s) const {
        if (f.pattern == s.pattern) {
            return false;
        }
        if (f.start == s.start) {
            return f.length < s.length;
        }
        return f.start < s.start;
    }
}
structfoo{
INTA、b、c;
Foo(inta,intb,intc):a(a),b(b),c(c){
}
结构FooComp{
布尔运算符()(常量Foo&f,常量Foo&s)常量{
如果(f.pattern==s.pattern){
返回false;
}
如果(f.start==s.start){
返回f.length

还是使用其他STL或数据结构?

使用标准库集是不可能的

比较运算符与排序紧密耦合

虽然从性能上看,这是一个稍差的解决方案,但您可以拥有一个包含所有对象的集合,只使用以下命令按“a”排序:

struct Foo {
    int a, b, c;
    Foo(int a, int b, int c) : a(a), b(b), c(c) {}
    bool operator<(const Foo& rhs) const {
        return a < rhs.a;
    }
    friend ostream& operator<<(ostream&, const Foo&);
};
structfoo{
INTA、b、c;
Foo(inta,intb,intc):a(a),b(b),c(c){

bool运算符这在使用标准库集时是不可能的

比较运算符与排序紧密耦合

虽然从性能上看,这是一个稍差的解决方案,但您可以拥有一个包含所有对象的集合,只使用以下命令按“a”排序:

struct Foo {
    int a, b, c;
    Foo(int a, int b, int c) : a(a), b(b), c(c) {}
    bool operator<(const Foo& rhs) const {
        return a < rhs.a;
    }
    friend ostream& operator<<(ostream&, const Foo&);
};
structfoo{
INTA、b、c;
Foo(inta,intb,intc):a(a),b(b),c(c){

bool操作符在boost中有一个现成的库,用于存储这种东西,称为boost.multi_index

它允许声明满足多个索引及其约束的容器

这有点过时,可能需要一些爱,但它确实起到了作用

您可以从以下内容开始:

struct Foo {
    int a, b, c;
    Foo(int a, int b, int c) : a(a), b(b), c(c) {}
};

#include <tuple>
#include <type_traits>
#include <utility>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>


struct get_a
{
    using result_type = int const&;
    result_type operator()(Foo const& l) const {
        return l.a;
    }
};

struct get_bc
{
    using result_type = std::tuple<int const&, int const&>;

    result_type operator()(Foo const& l) const {
        return std::tie(l.b, l.c);
    }
};

namespace foo {
    using namespace boost;
    using namespace boost::multi_index;

    struct by_a {};
    struct by_bc {};

    using FooContainer = 
multi_index_container
<
    Foo,
    indexed_by
    <
        ordered_unique<tag<by_a>, get_a>,
        ordered_non_unique<tag<by_bc>, get_bc>
    >
>;
}

int main()
{
    foo::FooContainer foos;

    foos.insert(Foo{ 1, 2,3 });
    foos.insert(Foo{ 2, 2,4 });

}
structfoo{
INTA、b、c;
Foo(inta,intb,intc):a(a),b(b),c(c){
};
#包括
#包括
#包括
#包括
#包括
#包括
结构获取
{
使用result_type=int const&;
结果类型运算符()(Foo const&l)const{
返回洛杉矶;
}
};
结构get\u bc
{
使用result_type=std::tuple;
结果类型运算符()(Foo const&l)const{
返回标准::tie(l.b,l.c);
}
};
名称空间foo{
使用名称空间boost;
使用名称空间boost::multi_索引;
_a{}的结构;
_bc{}的结构;
使用FooContainer=
多索引容器
<
福,
索引
<
秩序独特,
有序非唯一
>
>;
}
int main()
{
foo::foo容器foos;
插入(Foo{1,2,3});
插入(Foo{2,2,4});
}

在boost中有一个现成的库,用于存储此类内容,称为boost.multi_index

它允许声明满足多个索引及其约束的容器

这有点过时,可能需要一些爱,但它确实起到了作用

您可以从以下内容开始:

struct Foo {
    int a, b, c;
    Foo(int a, int b, int c) : a(a), b(b), c(c) {}
};

#include <tuple>
#include <type_traits>
#include <utility>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>


struct get_a
{
    using result_type = int const&;
    result_type operator()(Foo const& l) const {
        return l.a;
    }
};

struct get_bc
{
    using result_type = std::tuple<int const&, int const&>;

    result_type operator()(Foo const& l) const {
        return std::tie(l.b, l.c);
    }
};

namespace foo {
    using namespace boost;
    using namespace boost::multi_index;

    struct by_a {};
    struct by_bc {};

    using FooContainer = 
multi_index_container
<
    Foo,
    indexed_by
    <
        ordered_unique<tag<by_a>, get_a>,
        ordered_non_unique<tag<by_bc>, get_bc>
    >
>;
}

int main()
{
    foo::FooContainer foos;

    foos.insert(Foo{ 1, 2,3 });
    foos.insert(Foo{ 2, 2,4 });

}
structfoo{
INTA、b、c;
Foo(inta,intb,intc):a(a),b(b),c(c){
};
#包括
#包括
#包括
#包括
#包括
#包括
结构获取
{
使用result_type=int const&;
结果类型运算符()(Foo const&l)const{
返回洛杉矶;
}
};
结构get\u bc
{
使用result_type=std::tuple;
结果类型运算符()(Foo const&l)const{
返回标准::tie(l.b,l.c);
}
};
名称空间foo{
使用名称空间boost;
使用名称空间boost::multi_索引;
_a{}的结构;
_bc{}的结构;
使用FooContainer=
多索引容器
<
福,
索引
<
秩序独特,
有序非唯一
>
>;
}
int main()
{
foo::foo容器foos;
插入(Foo{1,2,3});
插入(Foo{2,2,4});
}

我考虑使用map,
a
作为键,
(b,c)
作为值。但是,map不是为这种情况设计的(我想)。我考虑使用map,
a
作为键,
(b,c)
作为值。但是,map不是为这种情况设计的(我想)。这不符合我的想法。在这段代码中(),我希望是1,2,3,3,13,5,2,但结果是1,2,3,3,1,3,5,2,1,5,7如果您能告诉我们更多关于如何使用它的信息,也许我可以帮助更多…此解决方案可能缺乏性能,但没有功能。这并不像我想的那样工作。在这段代码中(),我希望是1,2,3,3,13,5,2,但结果是1,2,3,3,1,3,5,2,1,5,7如果你能告诉我们更多关于如何使用它的信息,也许我能帮上更多的忙…这个解决方案可能缺乏性能,但没有功能。哇,非常感谢,但我的环境不允许使用boost库。哇,非常感谢,b我的环境不允许使用ut boost库。