STL问题:想要一个映射和一个结构中的序列 [英] STL Question: want a map and a sequence in 1 construct

查看:86
本文介绍了STL问题:想要一个映射和一个结构中的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
有时候,您需要一个有序的序列(例如矢量),但又需要一种快速找到项目(例如地图或集合)的方法.
是否有对偶构造一种MAP_SEQUENCE ...

我问我真正要查看的是具有2个键的数据:
1是顺序顺序
2其他顺序,例如说姓名

场景:

假设您正在实现一个代表表的数据库对象类.
您的概念是表具有一组键.
每个键都有一组有序的字段.
字段具有:
名字
值(可能是变体)

在Key类中,字段顺序很重要,还需要按名称在关键字中查找字段以获取/设置其值.

这就是我到目前为止所拥有的:即2个容器,数据的向量
以及将字段名称映射到seq容器中的字段索引的映射.
但它似乎具有冗余性.


类密钥
{
公众:
...
//通过名称或索引访问运算符以访问字段数据
_variant_t& ;;运算符[](短字段);
_variant_t& ;;运算符[](int字段);
const _variant_t& ;;运算符[](const char *字段)const;
const _variant_t& ;;运算符[](短字段)const;
...

受保护的:
//按键顺序中的字段
std :: vector< _variant_t>数据;

//不区分大小写的字段名映射到数据向量序列中索引到数据项的索引
std :: map< CString,简称,CompareNoCase> field_map;

//关键信息-从数据库模式中收集
const Key_Info * key_info;

CString key_name; //键名
...
};



是否有1个可以同时完成这两项工作的容器-
提供一个快速查找键(字段名或索引)的方法?

hi!
there are times when you need an ordered sequence like a vector, but also a quick way of finding an item (like a map, or set).
Is there a dual contruct a sort of MAP_SEQUENCE ...

I quess really I am looking at is data with 2 keys:
1 is the sequence order
2 the other order like say name

SCENARIO:

Say you are implement a database object class that represents a table.
You have the concept that the table has a set of keys.
Each key has an ordered set of fields.
A field has:
a name
a value ( variant perhaps)

In the Key class, the field order matters, and also there is a need to look up the field by name in the key to get/set its value.

this is what I have so far: i.e 2 containers, a vector of the data
and a map mapping field name to field index in the the seq container.
But it seems to have redundancy.


class Key
{
public:
...
// access operators to the field data by name or index
_variant_t&; operator [] ( short field);
_variant_t&; operator [] ( int field);
const _variant_t &; operator [] ( const char* field) const;
const _variant_t &; operator [] ( short field) const;
...

protected:
// Fields in key sequence
std::vector<_variant_t> data;

// case insensitve map of field name to index in the data vector sequence to data item
std::map< CString, short, CompareNoCase> field_map;

// key information - gleaned from the database schema
const Key_Info* key_info;

CString key_name; // key name
...
};



Is there 1 container that would do both jobs -
provide a quick lookup for either key ( field name or index)?

推荐答案

我没有用过,但是您可能想看一下增强MultiIndex [
I haven''t used it, but you might want to take a look at Boost MultiIndex[^]


您始终可以使用sort函数到排序 [ STL函数对象 [
You can always use the sort function to sort[^] on any field whenever needed.
Here is another reference - STL Function objects[^]


这篇关于STL问题:想要一个映射和一个结构中的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆