What is the qualification of a class in C ++ to become a container?

I am new to C ++ programming and came across the term containers with examples like vector , deque , map , etc.

What should be the minimum requirements that a class must fulfill to call container in C ++?

+7
c ++ containers
source share
3 answers

I'll start with the Range concept.

A range has only two methods: start and end. They both return iterators of the same type (note: there are suggestions allowing the end to return Sentinel).

Iterators are considered understandable to the reader.

The high-quality range can also expose empty, size, front, back and operator [] (especially for random access).

For a for(:) loop, you can qualify as a Range , being a raw C array having begin() and end() methods or having free functions in the same namespace as your type, which takes your type as one argument (and return iterative stuff). Starting with this post, the only thing used in the standard that uses ranges is for(:) loops. It can be argued that this answer is the only practical definition of the concept of Range in C ++.


Next, the container.

A container is a range of at least forward iterators (input and output ranges are usually not called Containers) to which its elements belong. Serial and associative containers are different animals, and both are defined in the standard.

Containers in the standard have a set of typedefs - value type, iterator, const iterator. Most also have a dispenser (other than an array). They are empty, and most of them have a size (except forward_list).

Containers can be constructed using 2 input or redirect iterators to a compatible value type and from the list of initializers.

Serial containers have push and emplace back (except for the forward list) (and some have emplace / push front), and also insert and replace on the iterator (or after for the forward list).

Associative containers have a key type. Many of them are containers with vapors. Stored data is usually partially const (the โ€œkeyโ€ part of the data, whether it is a key or full field in the case of set s). They insert and place with and without tips - they manage their own order. They also have .find and .count .

There are no functions dependent on Container-ness in the std library. And there is an active suggestion that the Container and Range are formalized as a concept in C ++ 17. The actual technical definition of Container meets the standard if you need to accurately create a real container; however, as a rule, you really need a Range with tools for editing it and mechanics of ownership. The container concept, in my experience, is mainly there to simplify the definition of behavior in the standard.

After adding something like Ranges-v3, the Range and Container concepts will be real things that exist in the code, and there may be algorithms that depend on exactly these functions. Prior to this, they are ad-hoc concepts more than anything else.

+11
source share

The absolute minimum requirement must be for the container to have a constant iterator class associated with it. Although the generator satisfies this requirement. Therefore, it should be that there is a constant iterator and that the specified container has initial and final values โ€‹โ€‹of the constant type of the iterator.

+2
source share

C ++ Concepts: Container

A container is an object used to store other objects and to manage the memory used by the objects contained in it.

http://en.cppreference.com/w/cpp/concept/Container

A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which provides greater flexibility in the types supported as elements.

http://www.cplusplus.com/reference/stl/

Given these definitions, I think we can say that containers should be able to store an arbitrary number of elements (although the number may be a compile-time constant). The container has the objects that it contains, including allocating space for them on the heap or the stack (for the array container). Therefore, the programmer does not need new or delete (allocate or free) memory for objects.

The following containers can be found in the STL: array, deque, vector, set, map, stack, queue, list, unordered_map, unordered_set

A container usually allows you to access (or index) the elements it contains, although some of them only allow access to one or more elements (for example, a queue or stack). The container will provide methods for adding or removing objects or for finding an object.

Requirements:

  • A number of objects must be executed
  • The objects that it has are of an arbitrary type (although perhaps it should satisfy certain requirements, for example, sort)

Possible functions

  • While some containers are aware of the dispenser, this is optional.
  • A container can contain more than one type of object (for example, a map, although it can be assumed that a map contains pairs of objects)
  • While containers can be iterable, this is not required, for example. queues or stacks.

Classes that are containers

  • std :: string : this is a character set. Although it is intended for characters or wide characters, it is a SequenceContainer

Some classes that are not considered containers:

  • std :: unique_ptr, std :: shared_ptr : while these types have a concept of ownership, they control only one object, so they are not a collection of objects
  • std :: tuple, std :: pair : while a tuple can contain an arbitrary number of objects, you must specify the type of each object, so it does not have the expected flexibility of a common container. A tuple can be more accurately classified as a type of structure.
+1
source share

All Articles