stl 兼容容器 [英] stl compliant container

查看:29
本文介绍了stl 兼容容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇什么需要符合 stl 的容器(或符合 boost,我的理解是它们要么相同,要么非常相似).我见过一些人们称之为 stl 兼容的例子(例如,这个在 codeproject,显然是实际的 stl 容器),但我不确定我需要这些容器的哪些组件.

I've been curious as to what entails an stl-compliant container (or boost-compliant, my understanding is that their either the same, or very similar). I've seen a few examples of what people call stl-compliant (for example, this one over at codeproject, and obviously the actual stl containers), but I'm not exactly sure what components of those containers I need to have.

据我所知,我至少需要这些东西:

From what i could gather, I need at least these things:

  1. 符合 STL 的迭代器(当前的 stl 只使用双向或更高的迭代器,不知道这是一个要求还是偶然,仍在弄清楚什么是必要的被视为stl-兼容迭代器")

  1. STL-compliant Iterators (the current stl only uses bi-directional or higher iterators, don't know if this is a requirement or just happens-chance, still figuring out what's necessary to be considered a "stl-compliant iterator")

定义分配器的机制(默认为 std::allocator),以及正确使用它们(仍在尝试弄清楚最后一部分的含义)

Mechanism for defining allocators (default to std::allocator), as well as using them correctly (still trying to figure out what this last part means)

元编程的公共类型定义(指针类型、const 指针类型、引用类型、值类型、const 引用类型、差异类型,也许还有其他一些?).附带问题:什么是差异类型?

public typedefs for metaprogramming (pointer type, const pointer type, reference type, value type, const reference type, difference type, maybe some others?). Side question: What is a difference type?

'generic'(即使用元编程/模板使容器能够容纳几乎任何类型)

'generic' (i.e. uses metaprogramming/templates to make the container able to hold pretty much any type)

还有什么我遗漏的,或者更糟的是,上面列表中有什么错误(可能是常量正确性、线程安全、异常生成/处理等)?另外,是否有详细说明要求的规范文档(如果存在这种情况)?

Is there anything else I've missed, or worse, got wrong in the above list (perhaps things such as const-correctness, thread safety, exceptions generation/handling, etc.)? Also, is there a specifications doc somewhere detailing what is required, if such a thing even exists?

推荐答案

  1. 迭代器:标准库定义了迭代器类别.您希望提供对这些类别之一进行建模的迭代器.根据您的观点,istream_iteratorostream_iterator 允许流成为不提供双向迭代器的容器.

  1. Iterators: the standard library defines iterator categories. You want to supply iterators that model one of those categories. Depending on your viewpoint, the istream_iterator and ostream_iterator allow streams to be containers that don't supply bidirectional iterators.

基本上,您使用分配器的 allocate(n)n 对象分配空间,以及 deallocate(p, n)p 指向的 n 对象释放空间.您通常使用分配器的 constructdestroy 成员.

Basically, you use the allocator's allocate(n) to allocate space for n objects, and deallocate(p, n) to free the space for n objects pointed to by p. You normally don't use the allocator's construct or destroy members.

是的,还有其他一些(例如,关联容器定义了 key_type).difference_type 是一种可以表示两个指针之间差异的类型.它通常由分配器提供,因此容器将只有一个类似于以下内容的 typedef:

Yes, there are a few others (e.g., associative containers define a key_type). difference_type is a type that can represent the difference between two pointers. It's normally supplied by the allocator, so a container will just have a typedef something like:

typedef Allocator::difference_type difference_type;

  • 不一定(或通常)涉及任何元编程,只是(相当基本的)通用编程.即,您定义了一个模板,但不一定在编译时执行任何计算(这是元编程).

    最初被问到时的当前标准没有定义任何关于线程或线程安全的内容.您使用的任何线程支持都是完全独立的,您需要遵循它指定的规则(通常多个线程可以读取一个容器,但一个线程需要独占访问权限才能写入容器).

    The standard that was current when this was originally asked didn't define anything about threading or thread safety. Any threading support you used was entirely separate, and you needed to follow the rules it specified (usually that multiple threads could read a container, but a thread required exclusive access to write a container).

    从 C++11 开始,该标准确实增加了对线程的支持,包括关于线程安全、数据竞争等的定义.这指定允许并行读取,但写入需要独占.

    Starting with C++11, the standard did add support for threading, including definitions about thread safety, data races, and so on. This specifies that parallel reading is allowed, but writing needs to be exclusive.

    这篇关于stl 兼容容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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