Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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
Boost 如何在不影响插入顺序的情况下替换多索引容器中的项?_Boost_Boost Multi Index - Fatal编程技术网

Boost 如何在不影响插入顺序的情况下替换多索引容器中的项?

Boost 如何在不影响插入顺序的情况下替换多索引容器中的项?,boost,boost-multi-index,Boost,Boost Multi Index,我有一个包含四个索引的多索引容器。其中一个索引是随机访问索引,用于维护插入顺序。当容器元素上的属性在外部更新时,我希望相关索引得到更新。但是,我想保留插入顺序 我想知道如果替换有问题的值,插入顺序是否会受到影响。如果没有,我如何才能做到这一点?我希望modify()在这里工作。让我尝试一下,并展示一个示例: #include <boost/multi_index_container.hpp> #include <boost/multi_index/random_access_i

我有一个包含四个索引的多索引容器。其中一个索引是随机访问索引,用于维护插入顺序。当容器元素上的属性在外部更新时,我希望相关索引得到更新。但是,我想保留插入顺序

我想知道如果替换有问题的值,插入顺序是否会受到影响。如果没有,我如何才能做到这一点?

我希望modify()在这里工作。让我尝试一下,并展示一个示例:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <iostream>

struct Data
{
    int i;
    std::string index1;

    friend std::ostream& operator<<(std::ostream& os, Data const& v) {
        return os << "{ " << v.i << ", '" << v.index1 << "' }";
    }

};

namespace bmi = boost::multi_index;

using Table = bmi::multi_index_container<
    Data,
    bmi::indexed_by<
        bmi::random_access<>,
        bmi::ordered_unique<bmi::member<Data, std::string, &Data::index1> >
    > >;

static std::ostream& operator<<(std::ostream& os, Table const& table) {
    os << "insertion order: "; for(auto const& element : table) os << element << "; ";
    os << "\nsecondary index: ";for(auto const& element : table.get<1>()) os << element << "; ";
    return os << "\n";
}

int main()
{
    Table table {
        { 42, "aap" },
        { 43, "noot" },
        { 44, "mies" } 
    };

    std::cout << "Before:\n" << table << "\n";

    table.modify(table.begin(),  [](Data& v) { v.i *= v.i; });
    std::cout << "Edit 1:\n" << table << "\n";

    table.modify(table.begin()+2, [](Data& v) { v.index1 = "touched"; });
    std::cout << "Edit 2:\n" << table << "\n";
}
这可能是你想要的


看吧,你很快!这回答了我的问题。贝丹克@MM顺便说一下,
replace
似乎也保留了插入顺序。注意,您可能需要
将另一个索引中的迭代器投影到(主)随机访问索引中。带有
项目的样本
-ion:
Before:
insertion order: { 42, 'aap' }; { 43, 'noot' }; { 44, 'mies' }; 
secondary index: { 42, 'aap' }; { 44, 'mies' }; { 43, 'noot' }; 

Edit 1:
insertion order: { 1764, 'aap' }; { 43, 'noot' }; { 44, 'mies' }; 
secondary index: { 1764, 'aap' }; { 44, 'mies' }; { 43, 'noot' }; 

Edit 2:
insertion order: { 1764, 'aap' }; { 43, 'noot' }; { 44, 'touched' }; 
secondary index: { 1764, 'aap' }; { 43, 'noot' }; { 44, 'touched' };