View source code
Display the source code in std/array.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla 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 using local clone.

std.array.Appender/appender - multiple declarations

Function appender

Convenience function that returns a RefAppender instance initialized with arrayPtr. Don't use null for the array pointer, use the other version of appender instead.

RefAppender!(E[]) appender(P, E) (
  P arrayPtr
);

Example

int[] a = [1, 2];
auto app2 = appender(&a);
writeln(app2[]); // [1, 2]
writeln(a); // [1, 2]
app2 ~= 3;
app2 ~= [4, 5, 6];
writeln(app2[]); // [1, 2, 3, 4, 5, 6]
writeln(a); // [1, 2, 3, 4, 5, 6]

app2.reserve(5);
assert(app2.capacity >= 5);

Function appender

Convenience function that returns an Appender instance, optionally initialized with array.

Appender!A appender(A)()
if (isDynamicArray!A);

Appender!(E[]) appender(A, E) (
  auto ref A array
);

Example

auto w = appender!string;
// pre-allocate space for at least 10 elements (this avoids costly reallocations)
w.reserve(10);
assert(w.capacity >= 10);

w.put('a'); // single elements
w.put("bc"); // multiple elements

// use the append syntax
w ~= 'd';
w ~= "ef";

writeln(w[]); // "abcdef"

Struct Appender

Implements an output range that appends data to an array. This is recommended over array ~= data when appending many elements because it is more efficient. Appender maintains its own array metadata locally, so it can avoid the performance hit of looking up slice capacity for each append.

struct Appender(A)
  
if (isDynamicArray!A);

Constructors

NameDescription
this (arr) Constructs an Appender with a given array. Note that this does not copy the data. If the array has a larger capacity as determined by arr.capacity, it will be used by the appender. After initializing an appender on an array, appending to the original array will reallocate.

Properties

NameTypeDescription
capacity[get] size_t
data[get] inout(T)[]Use opSlice() from now on.
opSlice[get] inout(T)[]

Methods

NameDescription
clear () Clears the managed array. This allows the elements of the array to be reused for appending.
put (item) Appends item to the managed array. Performs encoding for char types if A is a differently typed char array.
put (items) Appends an entire range to the managed array. Performs encoding for char elements if A is a differently typed char array.
reserve (newCapacity) Reserve at least newCapacity elements for appending. Note that more elements may be reserved than requested. If newCapacity <= capacity, then nothing is done.
shrinkTo (newlength) Shrinks the managed array to the given length.
toString () Gives a string in the form of Appender!(A)(data).

Aliases

NameDescription
opOpAssign Appends to the managed array.

Parameters

NameDescription
A the array type to simulate.

See Also

appender

Example

auto app = appender!string();
string b = "abcdefg";
foreach (char c; b)
    app.put(c);
writeln(app[]); // "abcdefg"

int[] a = [ 1, 2 ];
auto app2 = appender(a);
app2.put(3);
app2.put([ 4, 5, 6 ]);
writeln(app2[]); // [1, 2, 3, 4, 5, 6]

Authors

Andrei Alexandrescu and Jonathan M Davis

License

Boost License 1.0.