SimpleCxxLib
packageclass Vector<ValueType>
vector
type, but is simpler both
to use and to implement.
Constructor | |
Vector(n, value) | Initializes a new vector. |
Methods | |
Adds a new value to the end of this vector. | |
Removes all elements from this vector. | |
Returns true if the two vectors contain the same elements in the same order. | |
Returns the element at the specified index in this vector. | |
Inserts the element into this vector before the specified index. | |
Returns true if this vector contains no elements. | |
Calls the specified function on each element of the vector in ascending index order. | |
Removes the element at the specified index from this vector. | |
Replaces the element at the specified index in this vector with a new value. | |
Returns the number of elements in this vector. | |
Rearranges the elements in this vector into sorted order. | |
Returns a new vector containing elements from a sub-range of this vector. | |
Converts the vector to a printable string representation. | |
Operators | |
Iterates through the elements in a vector in order of ascending index. | |
Overloads [] to select elements from this vector. | |
Concatenates two vectors. | |
Adds all of the elements from v2 (or the single specified value) to v1 . | |
Returns true if vec1 and vec2 contain the same elements. | |
Returns true if vec1 and vec2 are different. | |
Outputs the contents of the vector to the given output stream. | |
Reads the contents of the given input stream into the vector. |
Vector(); Vector(int n, ValueType value = ValueType());
n
elements, each of which is initialized to value
;
if value
is missing, the elements are initialized
to the default value for the type.
Usage:
Vector<ValueType> vec; Vector<ValueType> vec(n, value); Vector<ValueType> vec = { value1, value2, value3 };
int size() const;
Usage:
int nElems = vec.size();
void sort();
Usage:
vec.sort();
bool isEmpty() const;
true
if this vector contains no elements.
Usage:
if (vec.isEmpty()) ...
void mapAll(void (*fn)(ValueType)) const; void mapAll(void (*fn)(const ValueType &)) const; void mapAll(FunctorType fn) const;
Usage:
vec.mapAll(fn);
void clear();
Usage:
vec.clear();
bool equals(const Vector& v) const;
true
if the two vectors contain exactly the same element values in the same order. Identical in behavior to the ==
operator.
Usage:
if (vec1.equals(vec2)) ...
const ValueType & get(int index) const;
Usage:
ValueType val = vec.get(index);
void set(int index, const ValueType & value);
Usage:
vec.set(index, value);
void insert(int index, ValueType value); void insertAt(int index, ValueType value);
insertAt
.
Usage:
vec.insert(0, value);
void remove(int index); void removeAt(int index);
removeAt
.
Usage:
vec.remove(index);
void add(ValueType value); void push_back(ValueType value);
vector
class in the Standard Template Library,
this method is also called push_back
.
Usage:
vec.add(value);
Vector subList(int start, int length) const; Vector subList(int start) const;
Usage:
Vector<ValueType> sub = vec.subList(start, length);
string toString();
Usage:
string str = vec.toString();
for (ValueType elem : vec) for (ValueType & elem : vec)
Usage:
for (ValueType elem : vec) { cout << elem << endl; } for (ValueType & elem : vec) { // if reference type, elements are mutable elem *= 2; }
ValueType & operator[](int index); const ValueType & operator[](int index) const;
[]
to select elements from this vector.
This extension enables the use of traditional array notation to
get or set individual elements. This method signals an error if
the index is outside the array range. The file supports two
versions of this operator, one for const
vectors and
one for mutable vectors.
Usage:
vec[index]
Vector operator+(const Vector & v2) const;
Usage:
v1 + v2
Vector & operator+=(const Vector & v2); Vector & operator+=(const ValueType & value);
v2
(or the single
specified value) to v1
. As a convenience, the
Vector
package also overloads the comma operator so
that it is possible to initialize a vector like this:
Vector<int> digits; digits += 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;
Usage:
v1 += v2; v1 += value;
ostream& operator<<(const Vector& vec);Outputs the contents of
vec
to the given output stream.
The output is in the form {value1, value2, value3}
where elements are listed in order of ascending index.
Usage:
cout << vec << endl;
istream& operator>>(Vector& vec);Reads the contents of the given input stream into
vec
. Any previous
contents of the vector are replaced.
The input is expected to be in the form {value1, value2, value3}
where elements are listed in order of ascending index. If unable to read a proper
vector from the stream, the operation results in a stream fail state.
Usage:
if (infile >> vec) ...