简体   繁体   English

如何对STL容器的元素强制执行主要键约束,例如行为

[英]How to enforce a primay key constraint like behavior for elements of an STL container

Only one element in the container can have the key property similar to how rdbms will not allow you to declare more than one primary key on a table. 容器中只有一个元素可以具有key属性,类似于rdbms不允许您在一个表上声明多个主键的方式。 Example follows using a vector ( please consider using any other container ( std or boost ) that can accomplish the task elegantly. 下面是使用向量的示例(请考虑使用可以完美完成任务的任何其他容器( stdboost )。

struct Element
{
  std::wstring val_;
  bool key_;
};
std::vector<Element> v;
Element e1;
e1.val_ = L"Jupiter";
e1.key_ = false;
v.push_back(e1);

Element e2;
e2.val_ = L"Mars";
e2.key_ = true;
v.push_back(e2);

Element e3;
e3.val_ = L"Venus";
e3.key_ = false;
v.push_back(e3);

Element e4;
e4.val_ = L"Venus";
e4.key_ = false;
v.push_back(e4);

Key requirement is that if for example an attempt is made to make e3.key_ = true an exception should be thrown because e2 ("Mars") already plays that role 关键要求是,例如,如果尝试使e3.key_ = true ,则应引发异常,因为e2(“火星”)已经扮演了该角色

Note that duplicates are allowed in this container. 请注意,此容器中允许重复。

If you only want to allow one of them to be a key, I would store the indication of the key separately from the individual items: 如果您只想允许其中一个作为键,那么我会将键的指示与各个项目分开存储:

struct column_set {
    std::vector<std::wstring> elements;
    size_t key;
};

column_set v;
v.elements.push_back(L"Jupiter");
v.elements.push_back(L"Mars");
v.elements.push_back(L"Venus");
v.elements.push_back(L"Venus");
v.key = 1;

If you want to, it should be fairly easy to write a little front-end code that acts like an array of bool that will return true for an index equal to the value you've give for key , and false for any other value. 如果愿意,编写一些类似于bool数组的前端代码应该相当容易,对于等于您为key给出的索引,返回true ,对于其他任何值返回false

Based on your description, it sounds like you are attempting to make pre-existing STL classes handle higher-level application logic for you and that's just not going to happen (at least not cleanly or elegantly). 根据您的描述,听起来您正在尝试使预先存在的STL类为您处理更高级别的应用程序逻辑,而这只会发生(至少不会干净或优雅)。

My only suggestion would be something similar to what Jerry Coffin suggested. 我唯一的建议是类似于杰里·科芬(Jerry Coffin)的建议。

Create your own class that represents your data set. 创建代表您的数据集的自己的类。 Have that class encapsulate a vector (or map, list...) of your columns but provide a very clean public interface to the rest of the world. 让该类封装您列的向量(或地图,列表...),但为世界其他地方提供一个非常干净的公共接口。 Nothing outside of your class should ever have to know that inside there's a vector. 课堂外的任何人都不必知道里面有一个向量。 Other code only talks to public functions like AddColumn, DeleteColumn, SetKey. 其他代码仅与诸如AddColumn,DeleteColumn,SetKey之类的公共函数对话。 So essentially you are creating your own custom container which uses STL under the hood but extends it to provide the functionality you need. 因此,实质上,您是在创建自己的自定义容器,该容器在幕后使用STL,但对其进行扩展以提供所需的功能。

As far as enforcement of the key, leave it up to your class to handle the enforcement internally. 至于密钥的实施,则由您的班级负责内部处理。 And just like Jerry said, instead of storing 49 false's and one true, it might be easier to just have a separate member variable sitting next to the vector that would simply indicate which of the entries happens to be the key. 就像Je​​rry所说的那样,与其存储49个false和一个true,不如在向量旁边放置一个单独的成员变量(可能只是表明哪个条目恰好是键)更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM