append()

Data / Array Functions

Description

Expands a std::vector by one element and adds value to the end.

Syntax

std::vector<T> append(std::vector<T> arr, T val)

Parameters

NameTypeDescription
arrstd::vectorthe vector to expand
valTthe value to add

Returns

std::vector

Related

Under the Hood

From Processing.h:

void append(int v) { data.push_back(v); } void push(int v) { append(v); void append(const std::vector<int>& values) { for (int v : values) append(v); } void append(const std::vector<int>& values) { for (int v : values) append(v); void append(const IntList& list) { // Snapshot first: if 'list' is THIS SAME object (e.g. self-aliasing // call list.append(list)), iterating list.data directly while // append(v) grows this->data via push_back can reallocate the // underlying buffer mid-loop, invalidating the range-for's // captured begin()/end() iterators -- a use-after-free. Copying // values out first makes append(self) safe and correct. // (Found by stress-testing; confirmed via AddressSanitizer.) std::vector<int> snapshot = list.data; for (int v : snapshot) append(v); } for (int v : snapshot) append(v); void append(float v) { data.push_back(v); } void push(float v) { append(v); void append(const std::vector<float>& values) { for (float v : values) append(v); } void append(const std::vector<float>& values) { for (float v : values) append(v); void append(const FloatList& list) { // Snapshot first -- see IntList::append(const IntList&) comment; // protects against self-aliasing (list.append(list)) reallocating // mid-iteration and invalidating the iterators we're reading from. std::vector<float> snapshot = list.data; for (float v : snapshot) append(v); } for (float v : snapshot) append(v); void append(const std::string& v) { data.push_back(v); } void push(const std::string& v) { append(v); void append(const std::vector<std::string>& values) { for (auto& v : values) append(v); } void append(const std::vector<std::string>& values) { for (auto& v : values) append(v); void append(const StringList& list) { // Snapshot first -- see IntList::append(const IntList&) comment; // protects against self-aliasing (list.append(list)) reallocating // mid-iteration and invalidating the iterators we're reading from. std::vector<std::string> snapshot = list.data; for (auto& v : snapshot) append(v); } for (auto& v : snapshot) append(v);