View source code
Display the source code in std/array.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aGitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

std.array.assocArray - multiple declarations

Function assocArray

Creates an associative array from a range of key/value tuples.

auto assocArray(Range)(
  Range r
)
if (isInputRange!Range);

Parameters

NameDescription
r An input range of Tuple!(Key, Value).

Duplicates

Associative arrays have unique keys. For any duplicate key in r, the result will contain the corresponding value from the last occurrence of that key in r.

Returns

A newly allocated associative array, or a null associative array reference when given an empty range. The type is Value[Key].

Example

import std.typecons : tuple;

auto b = assocArray([ tuple("foo", "bar"), tuple("baz", "quux") ]);
static assert(is(typeof(b) == string[string]));
writeln(b); // ["foo":"bar", "baz":"quux"]

Function assocArray

Creates an associative array from a range of keys and a range of values.

auto assocArray(Keys, Values)(
  Keys keys,
  Values values
)
if (isInputRange!Values && isInputRange!Keys);

If given two ranges of unequal lengths after the elements of the shorter are exhausted, the remaining elements of the longer will not be considered.

Parameters

NameDescription
keys An input range of keys
values An input range of values

Duplicates

Associative arrays have unique keys. For any key with duplicates in keys, the result will have the corresponding value for the last occurrence of that key in keys.

Returns

A newly allocated associative array, or a null associative array reference when given empty ranges.

Example

import std.range.primitives : autodecodeStrings;
import std.range : repeat;

static if (autodecodeStrings)
    alias achar = dchar;
else
    alias achar = immutable(char);

auto c = assocArray("ABCD", true.repeat);
static assert(is(typeof(c) == bool[achar]));
bool[achar] expected = ['D':true, 'A':true, 'B':true, 'C':true];
writeln(c); // expected

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.