Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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++ 多索引容器和虚拟列表控件_C++_User Interface_Boost_Boost Multi Index - Fatal编程技术网

C++ 多索引容器和虚拟列表控件

C++ 多索引容器和虚拟列表控件,c++,user-interface,boost,boost-multi-index,C++,User Interface,Boost,Boost Multi Index,我在使用multi_index_容器时遇到了困难 这基本上就是我需要的: 虚拟列表控件 以多种方式对项目进行排序(因此我想使用multi_index_容器) 根据项目在列表控件中的排序和显示方式,随机访问项目 保留项目的原始插入/记录顺序 在列表控件中移动项目、插入和删除项目(并相应地更新容器) 到目前为止,我一直在同步使用多个向量/映射(显示映射到真实数据的列表位置等),所以最好移动到多索引容器 我曾尝试编写我的原始代码的最低版本(或尝试编写它) 非常感谢您在这方面的帮助!:) #包括 #

我在使用multi_index_容器时遇到了困难

这基本上就是我需要的:

  • 虚拟列表控件
  • 以多种方式对项目进行排序(因此我想使用multi_index_容器)
  • 根据项目在列表控件中的排序和显示方式,随机访问项目
  • 保留项目的原始插入/记录顺序
  • 在列表控件中移动项目、插入和删除项目(并相应地更新容器)
到目前为止,我一直在同步使用多个向量/映射(显示映射到真实数据的列表位置等),所以最好移动到多索引容器

我曾尝试编写我的原始代码的最低版本(或尝试编写它)

非常感谢您在这方面的帮助!:)

#包括
#包括
#包括
#包括
#包括
#包括
命名空间bmi=boost::multi_索引;
typedef无符号整数单元;
结构项
{
公众:
物料(标准::wstring名称,单位数量):名称(名称),数量(数量){
std::wstring名称;
单位数量;
//这里有更多的成员,如项目类型、类别等
};
结构记录顺序;
结构名称和顺序;
结构数量/订单;
typedef bmi::多索引容器<
项目(t),
bmi::按索引索引<
bmi::随机访问,
bmi::已测序,
bmi::有序非唯一<
标签,
bmi::成员
>,
bmi::有序非唯一<
标签,
bmi::成员
>
>
>物品容器;
无效填充容器(ItemContainer和container)
{
容器。向后推(项目t(L“鲑鱼卷”,3));
容器。推回(项目(L“中国可乐”,0));
容器。推回(第1项“挪威帽”);
容器。推回(项目(L“像新袜子一样”,3));
容器。推回(第4项“空瓶”);
容器。向后推(项目(1“领带不错”);
/*分类:
中国可乐
空瓶子
我喜欢新袜子
漂亮的领带
挪威帽
三文鱼卷
*/
}
int wmain(int argc,wchar_t*argv[])
{
物品容器;
人口容器(容器);
//按项目名称对要显示给用户的项目进行排序
//请看我对此的评论
重新排列(container.get().begin());
对于(ItemContainer::iterator it=container.begin(),end=container.end();it!=end;++it)
{

std::wcout Name我想我现在正在使用
reaginate()了解一些东西
。我已经更新了我的代码。也许我有点错了。我已经更新了我的代码。你需要一个数据库。你可以用
多索引
缝合一些东西,但是你最好用一些排序嵌入式sql数据库,比如。谢谢Dani。一个成熟的数据库引擎对我来说似乎有点难,但排序会很容易。我需要保持e数据库始终与Item\t对象同步。我需要对项目进行排序的数据将与Item\t对象(因此我将有副本)一起存在于数据库中。但是,我正在考虑这一点。我的意思是保留指向Item\t对象的指针以及(副本)我需要排序的数据在内存数据库中。我还需要保留一个容器,用于将列表位置映射到Item_t对象(提高性能)。您认为如何?如果这是严格在内存中,您可以将指针保留在数据库中(64位数字),而不是Item_t的副本。其余的设置很简单。
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>

#include <cstring>

namespace bmi = boost::multi_index;
typedef unsigned int uint;

struct Item_t
{
public:
    Item_t(std::wstring name, uint quantity) : Name(name), Quantity(Quantity) {}

    std::wstring Name;
    uint Quantity;
    // more members here, such as item type, category, etc
};

struct record_order;
struct name_order;
struct quantity_order;

typedef bmi::multi_index_container<
    Item_t,
    bmi::indexed_by<
        bmi::random_access<>,
        bmi::sequenced<bmi::tag<record_order>>,
        bmi::ordered_non_unique<
            bmi::tag<name_order>,
            bmi::member<Item_t, std::wstring, &Item_t::Name>
        >,
        bmi::ordered_non_unique<
            bmi::tag<quantity_order>,
            bmi::member<Item_t, uint, &Item_t::Quantity>
        >
    >
> ItemContainer;

void populateContainer(ItemContainer& container)
{
    container.push_back(Item_t(L"Salmon roll", 3));
    container.push_back(Item_t(L"Chinese cola", 0));
    container.push_back(Item_t(L"Norwegian cap", 1));
    container.push_back(Item_t(L"Like-new socks", 3));
    container.push_back(Item_t(L"Empty bottle", 4));
    container.push_back(Item_t(L"Nice tie", 1));
    /* sorted:
        Chinese cola
        Empty bottle
        Like-new socks
        Nice tie
        Norwegian cap
        Salmon roll
    */
}

int wmain(int argc, wchar_t* argv[])
{
    ItemContainer container;
    populateContainer(container);

    // sort items to be displayed to the user, by Item_t::Name
    // Please see my comment regarding this
    container.rearrange(container.get<name_order>().begin());

    for (ItemContainer::iterator it = container.begin(), end_ = container.end(); it != end_; ++it)
    {
        std::wcout << it->Name << std::endl;
    }

    std::wcout << std::endl;

    {
        // get Item_t from container where position in displayed list (after sort) is 5
        const Item_t& item = *(container.begin() + 5);
        // "Salmon roll"
        std::wcout << item.Name << std::endl;
    }

    {
        // TODO: insert and sort automatically: Item_t(L"Another useless thing", 1)
        const Item_t& item = *(container.begin() + 5);
        // Need this to be "Norwegian cap"
        std::wcout << item.Name << std::endl;
    }

    return 0;
}