STL Container Requirements

Does the standard some_container<T>::value_type be T ?

I ask because I am considering various approaches for implementing an STL-compatible 2d dynamic array. One of them is to have a 2Darray<T>::value_type be 2Darray_row<T> or something like this, where the array will be iterated as a set of strings (a bit simplified. My actual implementation allows iteration in three directions)

+4
source share
3 answers

Container requirements are a little funny in the sense that they are not actually used by any general algorithm. In this sense, it does not really matter.

However, the requirements are on the interface for the containers, not on how the container is actually instantiated. Even classes without templates can meet different requirements and, in fact, do. The requirement is that value_type present; that it is defined is entirely dependent on the implementation of the container.

+5
source

Table 96 in section 23.2.1 of the standard (C ++ 11) requires that the container class X contain objects of type T order to return T for X::value_type .

So, if your some_container stores objects of type T , then value_type should be T

+2
source

Either have a nested container (so colArray<rowArray<T> > ), or have one package ( 2dArray<T> ), but do not try to mix them. The nested approach allows you to use STL to the end ( vector<vector<T> > ), but it can be confusing and does not allow the use of column iterators, etc. that seem to you.

This SO answer answers using ublas , while another suggests using Boost multi-arrays .

Generally, go to the STL or Boost option if you can. You are unlikely to write anything yourself.

0
source

Source: https://habr.com/ru/post/1445487/


All Articles