Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何求有序非唯一boost多指标集某指标域的唯一值_C++_Boost_Boost Multi Index - Fatal编程技术网

C++ 如何求有序非唯一boost多指标集某指标域的唯一值

C++ 如何求有序非唯一boost多指标集某指标域的唯一值,c++,boost,boost-multi-index,C++,Boost,Boost Multi Index,我制作了几个索引的boost多索引集,其中大多数是有序的非唯一的 现在我需要在集合上使用迭代器对有序非唯一索引进行for循环,但是我需要跳过具有相同非唯一值的结构 换句话说,我只需要使用这个有序非唯一键的唯一值来循环 有没有办法只识别非唯一键中的唯一值,并使用该唯一值进行循环 我没有把代码,因为它是一般的方法,我正在寻找 我知道std::unique,但我不想制作另一个容器,因为在制作容器的过程中,原始容器正在被修改,这使我很难进行同步 您可以使用容器的上限函数迭代容器。下面是一个工作示例: s

我制作了几个索引的boost多索引集,其中大多数是有序的非唯一的

现在我需要在集合上使用迭代器对有序非唯一索引进行for循环,但是我需要跳过具有相同非唯一值的结构

换句话说,我只需要使用这个有序非唯一键的唯一值来循环

有没有办法只识别非唯一键中的唯一值,并使用该唯一值进行循环

我没有把代码,因为它是一般的方法,我正在寻找


我知道std::unique,但我不想制作另一个容器,因为在制作容器的过程中,原始容器正在被修改,这使我很难进行同步

您可以使用容器的
上限
函数迭代容器。下面是一个工作示例:

struct MyStruct {
    int group_id, id;
};

struct ByGroupID {};

using StructContainer = boost::multi_index_container<
    MyStruct,
    boost::multi_index::indexed_by<
        boost::multi_index::ordered_non_unique<
            boost::multi_index::tag<ByGroupID>,
            boost::multi_index::member<MyStruct, int, &MyStruct::group_id>
        >
    >
>;

int main() {
    StructContainer my_structs;

    my_structs.insert({1, 1});
    my_structs.insert({1, 2});
    my_structs.insert({1, 3});
    my_structs.insert({2, 4});
    my_structs.insert({2, 5});
    my_structs.insert({3, 6});

    auto &by_group_id = my_structs.get<ByGroupID>();

    for (auto it = by_group_id.begin(); it != by_group_id.end();) {
        std::cout << it->group_id << "\n";
        it = by_group_id.upper_bound(it->group_id);
    }
}
struct MyStruct{
int group_id,id;
};
结构ByGroupID{};
使用StructContainer=boost::多索引容器<
我的结构,
boost::多索引::按索引索引<
boost::多索引::有序非唯一<
boost::multi_index::tag,
boost::multi_index::member
>
>
>;
int main(){
结构容器我的结构;
my_structs.insert({1,1});
my_structs.insert({1,2});
my_structs.insert({1,3});
my_structs.insert({2,4});
我的结构插入({2,5});
my_structs.insert({3,6});
auto&by_group_id=my_structs.get();
for(自动it=by_group_id.begin();it!=by_group_id.end();){
std::cout group_id group_id);
}
}
它输出1、2、3