import std.container;// Construct a red-black tree and an array both containing the values 1, 2, 3.// RedBlackTree should typically be allocated using `new`RedBlackTree!int rbTree =new RedBlackTree!int(1, 2, 3);// But `new` should not be used with ArrayArray!int array = Array!int(1, 2, 3);// `make` hides the differencesRedBlackTree!int rbTree2 = make!(RedBlackTree!int)(1, 2, 3);Array!int array2 = make!(Array!int)(1, 2, 3);assert(rbTree == rbTree2);assert(array == array2);
import std.container;auto rbTree = make!RedBlackTree(1, 2, 3);// RedBlackTree!intauto array = make!Array("1","2","3");// Array!string
import std.algorithm, std.container, std.range;Array!int originalArray = make!(Array!int)(1, 2, 3);Array!int secondArray = originalArray;assert(equal(originalArray[], secondArray[]));// changing one instance changes the other one as well!originalArray[0] = 12;assert(secondArray[0] == 12);// secondArray now refers to an independent copy of originalArraysecondArray = originalArray.dup;secondArray[0] = 1;// assert that originalArray has not been affectedassert(originalArray[0] == 12);
import std.container;RedBlackTree!int rbTree;rbTree.insert(5);// null pointer dereferenceUsing an uninitialized struct-based container will work, because the structintializes itself upon use; however, up to this point the container will nothave an identity and assignment does not create two references to the samedata.
import std.container;// create an uninitialized arrayArray!int array1;// array2 does _not_ refer to array1Array!int array2 = array1;array2.insertBack(42);// thus array1 will not be affectedassert(array1.empty);// after initialization reference semantics work as expectedarray1 = array2;// now affects array2 as wellarray1.removeBack();assert(array2.empty);
import std.container, std.range;auto arrOfArrs = make!Array(generate!(() => make!(Array!int)).take(10));
import std.algorithm.comparison : equal;import std.algorithm.searching : find;import std.container;import std.range : take;auto array = make!Array(1, 2, 3);// `find` returns an Array!int.Range advanced to the element "2"array.linearRemove(array[].find(2));assert(array[].equal([1]));array = make!Array(1, 2, 3);// the range given to `linearRemove` is a Take!(Array!int.Range)// spanning just the element "2"array.linearRemove(array[].find(2).take(1));assert(array[].equal([1, 3]));
import std.algorithm.comparison : equal;import std.container;import std.range : iota;auto array = make!Array(1, 2);// the range type returned by `iota` is completely unrelated to Array,// which is fine for Array.insertBack:array.insertBack(iota(3, 10));assert(array[].equal([1, 2, 3, 4, 5, 6, 7, 8, 9]));
| Syntax | Ο(·) | Description |
|---|---|---|
| C(x) | nx | Creates a container of typeC from either another container or a range. The created container must not be a null reference even if x is empty. |
| c.dup | nc | Returns a duplicate of the container. |
| c ~ x | nc + nx | Returns the concatenation ofc andr.x may be a single element or an input range. |
| x ~ c | nc + nx | Returns the concatenation ofx andc.x may be a single element or an input range type. |
| Iteration | ||
| c.Range | The primary range type associated with the container. | |
| c[] | log nc | Returns a range iterating over the entire container, in a container-defined order. |
| c[a .. b] | log nc | Fetches a portion of the container from keya to keyb. |
| Capacity | ||
| c.empty | 1 | Returnstrue if the container has no elements,false otherwise. |
| c.length | log nc | Returns the number of elements in the container. |
| c.length = n | nc + n | Forces the number of elements in the container ton. If the container ends up growing, the added elements are initialized in a container-dependent manner (usually withT.init). |
| c.capacity | log nc | Returns the maximum number of elements that can be stored in the container without triggering a reallocation. |
| c.reserve(x) | nc | Forcescapacity to at leastx without reducing it. |
| Access | ||
| c.front | log nc | Returns the first element of the container, in a container-defined order. |
| c.moveFront | log nc | Destructively reads and returns the first element of the container. The slot is not removed from the container; it is left initialized withT.init. This routine need not be defined if front returns aref. |
| c.front = v | log nc | Assignsv to the first element of the container. |
| c.back | log nc | Returns the last element of the container, in a container-defined order. |
| c.moveBack | log nc | Destructively reads and returns the last element of the container. The slot is not removed from the container; it is left initialized withT.init. This routine need not be defined if front returns aref. |
| c.back = v | log nc | Assignsv to the last element of the container. |
| c[x] | log nc | Provides indexed access into the container. The index type is container-defined. A container may define several index types (and consequently overloaded indexing). |
| c.moveAt(x) | log nc | Destructively reads and returns the value at positionx. The slot is not removed from the container; it is left initialized with T.init. |
| c[x] = v | log nc | Sets element at specified index into the container. |
| c[x]op= v | log nc | Performs read-modify-write operation at specified index into the container. |
| Operations | ||
| e in c | log nc | Returns nonzero if e is found inc. |
| c.lowerBound(v) | log nc | Returns a range of all elements strictly less thanv. |
| c.upperBound(v) | log nc | Returns a range of all elements strictly greater thanv. |
| c.equalRange(v) | log nc | Returns a range of all elements inc that are equal tov. |
| Modifiers | ||
| c ~= x | nc + nx | Appendsx toc.x may be a single element or an input range type. |
| c.clear() | nc | Removes all elements inc. |
| c.insert(x) | nx * log nc | Insertsx inc at a position (or positions) chosen byc. |
| c.stableInsert(x) | nx * log nc | Same asc.insert(x), but is guaranteed to not invalidate any ranges. |
| c.linearInsert(v) | nc | Same asc.insert(v) but relaxes complexity to linear. |
| c.stableLinearInsert(v) | nc | Same asc.stableInsert(v) but relaxes complexity to linear. |
| c.removeAny() | log nc | Removes some element fromc and returns it. |
| c.stableRemoveAny() | log nc | Same asc.removeAny(), but is guaranteed to not invalidate any iterators. |
| c.insertFront(v) | log nc | Insertsv at the front ofc. |
| c.stableInsertFront(v) | log nc | Same asc.insertFront(v), but guarantees no ranges will be invalidated. |
| c.insertBack(v) | log nc | Insertsv at the back ofc. |
| c.stableInsertBack(v) | log nc | Same asc.insertBack(v), but guarantees no ranges will be invalidated. |
| c.removeFront() | log nc | Removes the element at the front ofc. |
| c.stableRemoveFront() | log nc | Same asc.removeFront(), but guarantees no ranges will be invalidated. |
| c.removeBack() | log nc | Removes the value at the back ofc. |
| c.stableRemoveBack() | log nc | Same asc.removeBack(), but guarantees no ranges will be invalidated. |
| c.remove(r) | nr * log nc | Removes ranger fromc. |
| c.stableRemove(r) | nr * log nc | Same asc.remove(r), but guarantees iterators are not invalidated. |
| c.linearRemove(r) | nc | Removes ranger fromc. |
| c.stableLinearRemove(r) | nc | Same asc.linearRemove(r), but guarantees iterators are not invalidated. |
| c.removeKey(k) | log nc | Removes an element fromc by using its keyk. The key's type is defined by the container. |
Sourcestd/container/package.d