Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
UppHub
Status & Roadmap
FAQ
Authors & License
Forums
Funding U++
Search on this site











SourceForge.net Logo

SourceForge.net Logo

GitHub Logo

Discord Logo

Vector

 

template <class T>

class Vector : public MoveableAndDeepCopyOption< Vector<T> >

T

Type of elements stored in Vector. T is required to be moveable and must have either deep copy constructor, pick constructor or default constructor.

The most effective flavor of basic random access container. Its features are derived from fact that it is implemented as simple C-like vector of elements.

Disadvantage of Vector is common requirement for elements to be stored in it - they must be moveable. Another disadvantage is fact that many operations invalidate references (that means C++ references and pointers) to elements.

Like any other NTL container, Vector is moveable type with pick and optional deep copy transfer semantics. Calling methods of picked Vector is logic error with exception of

 

void operator=(pick_ Vectorv)

void operator<<=(const Vector& v) (defined using DeepCopyOptionTemplate)

void Clear()

bool IsPicked() const

 

Optional deep copy is implemented through DeepCopyOptionTemplate macro.

 

Vector provides access to internal C-like vector of elements (either using Begin or even using direct cast operator) a can be used as buffer for C-like functions, although Buffer class might be more suitable in these cases.

 

 

Constructor Detail

 

Vector()

Default constructor. Constructs empty Vector.

 


 

explicit Vector(int n)

Creates Vector of n default constructed elements.

 


 

explicit Vector(int n, const T& init)

Creates Vector of n elements copy constructed as init.

 


 

Vector(pick_ Vector& v)

Pick constructor. Transfers source Vector in low constant time, but destroys it by picking.

v

Source Vector.

 


 

Vector(const Vector& v, int)

Optional deep copy constructor.

Requires T to have deep copy constructor or optional deep copy constructor.

v    Source Vector. v.

 


 

~Vector()

Destructor. Calls destructors of all elements in Vector.

 


 

Vector(std::initializer_list<Tinit)

C++ 11 initialization.

 

 

Public Member List

 

T& Add()

Adds new default constructed element to Vector.

Requires T to have default constructor.

Invalidates iterators and references to Vector.

Return value

Reference to newly added default constructed element.

 


 

template <class... ArgsT& Create(Args... args)

Adds new element to Vector using args as constructor parameters.

 


 

T& Add(const T& x)

Adds new element with specified value to Vector.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

x

The value that is copied to newly created element.

Return value

Reference to new element in Vector.

 


 

T& Add(T&& x)

Adds new element to Vector and picks value of parameter to it.

 


 

template <class... ArgsT& Create(Args&&... args)

Creates and adds a new element to the Array. args are forwarded to constructor.

 


 

void AddN(int n)

Adds specified number of default constructed elements to Vector.

Requires T to have default constructor.

Invalidates iterators and references to Vector.

n

Number of elements to add

 


 

const T& operator[](int iconst

Returns a reference to the element at specified position.

i

Position of element.

Return value

Constant reference to the element.

 


 

T& operator[](int i)

Returns a reference to the element at specified position.

i

Position of element.

Return value

Reference to the element.

 


 

const T& Get(int i, const T& defconst

T& Get(int i, T& def)

If i is valid index (it is >= 0 and < GetCount()), returns the reference to the element at i, otherwiser returns def.

 


 

int GetCount() const

Return the number of elements in Vector.

Return value

Actual number of elements.

 


 

bool IsEmpty() const

Tests whether Vector  is empty. Same as GetCount() == 0.

Return value

true if Vector is empty, false otherwise.

 


 

void Trim(int n)

Reduces number of elements in Vector to specified number. Required number must be less than or equal to actual number of elements in Vector. Capacity of Vector is however unchanged.

Invalidates iterators to Vector.

n

Required number of elements.

 


 

void SetCount(int n)

Changes count of elements in Vector to specified value. If required number of elements is greater than actual number, newly added elements are default constructed. If Vector has to increase capacity, the new capacity will exactly match required number of elements (unlike SetCountR).

Requires T to have default constructor.

Invalidates iterators and references to Vector.

n

Required number of elements.

 


 

void SetCount(int n, const T& init)

Changes count of elements in Vector to specified value. If required number of elements is greater than actual number, newly added elements are initialized to specified value using copy constructor. If Vector has to increase capacity, the new capacity will exactly match required number of elements (unlike SetCountR).

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

n

Required number of elements.

init

Initialization value of newly added elements.

 


 

void SetCountR(int n)

Changes count of elements in Vector to specified value. If required number of elements is greater than actual number, newly added elements are default constructed. If Vector has to increase capacity, the new capacity will be greater than required number of elements (unlike SetCount) to allow adding other elements without further increasing capacity.

Requires T to have default constructor.

Invalidates iterators and references to Vector.

n

Required number of elements.

 


 

void SetCountR(int n, const T& init)

Changes count of elements in Vector to specified value. If required number of elements is greater than actual number, newly added elements are initialized to specified value using copy constructor. If Vector has to increase capacity, the new capacity will be greater than required number of elements (unlike SetCount) to allow adding other elements without further increasing capacity.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

n

Required number of elements.

init

Initialization value of newly added elements.

 


 

void Clear()

Removes all elements from Vector. Capacity is also cleared to zero.

Invalidates iterators and references to Vector.

 


 

T& At(int i)

If specified position is lower than number of elements in Vector (i < GetCount()), returns reference to element at specified position. Otherwise increases number of elements in Vector to i + 1. Newly added elements are default constructed. If Vector has to increase capacity, the new capacity will be greater than required number of elements to allow adding other elements without further increasing capacity.

Requires T to have default constructor.

Invalidates iterators and references to Vector.

i

Position of required element.

Return value

Reference to required element.

 


 

T& At(int i, const T& x)

If specified position is lower than number of elements in Vector (i < GetCount()), returns reference to element at specified position. Otherwise increases number of elements in Vector to i + 1. Newly added elements are copy constructed from x. If Vector has to increase capacity, the new capacity will be greater than required number of elements to allow adding other elements without further increasing capacity.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

i

Position of required element.

x

Initialization value of newly added elements.

Return value

Reference to required element.

 


 

void Shrink()

Minimizes memory consumption of Vector by decreasing capacity to number of elements.

 


 

void Reserve(int n)

Reserves capacity. If required capacity is greater than current capacity, capacity is increased to the required value.

xtra

Required capacity.

 


 

int GetAlloc() const

Returns current capacity of Vector.

Return value

Capacity of Vector.

 


 

void Set(int i, const T& x, int count)

Sets requested number of elements starting at the position i to the specified value. If required number of elements exceeds existing elements of Vector then elements are added to Vector.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

i

Starting position.

x

Value.

count

Number of elements.

 


 

T& Set(int i, const T& x)

T& Set(int i, T&& x)

Sets the element at i to x (adds default constructed elements as necessary) and returns a reference to this element.

 


 

void Remove(int i, int count = 1)

Removes requested number of elements.

Invalidates iterators and references to Vector.

i

Position.

count

Number of elements to remove.

 


 

void Remove(const int *sorted_list, int n)

Removes multiple elements from Vector. Time of operation almost does not depend on number of elements.

Invalidates iterators and references to Vector.

sorted_list

Pointer to array of positions to remove. It must be sorted from lowest to greatest value.

n

Number of elements to remove.

 


 

void Remove(const Vector<int>& sorted_list)

Removes multiple elements from Vector. Same as Remove(sorted_list, sorted_list.GetCount()).

Invalidates iterators and references to Vector.

sorted_list

Sorted Vector of positions to remove.

 


 

template <class Conditionvoid RemoveIf(Condition c)

Removes all elements that satisfy predicate c. c is a function object (usually a lambda) that has a single int parameter representing the index of an element and returns true when the element at the  index is supposed to be removed.

 


 

void InsertN(int i, int count = 1)

Inserts a specified number of default constructed elements at a specified position.

Requires T to have default constructor.

Invalidates iterators and references to Vector.

i

Position.

count

Number of elements to insert.

 


 

T& Insert(int i)

Inserts one default constructed element at the specified position.

Requires T to have default constructor.

Invalidates iterators and references to Vector.

i

Position.

 


 

void Insert(int i, const T& x, int count)

Inserts a specified number of elements, setting them to a specified value.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

i

Position.

x

Value of inserted elements.

count

Number of elements to insert.

 


 

T& Insert(int i, const T& x)

Inserts element at position i setting its value to x and returns a reference to it.

 


 

template <class TT& Insert(int q, T&& x)

Inserts an element at i picking the content of x and returns a reference to this new element.

 


 

void Insert(int i, const Vector& x)

Inserts all elements from another Vector.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

i

Position.

x

Source Vector.

 


 

void Insert(int i, const Vector& x, int offset, int count)

Inserts a range of elements from another Vector.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

i

Insertion position.

x

Source Vector.

offset

Position of first element in source Vector to be inserted.

count

Number of elements to insert.

 


 

void Insert(int i, Vector&& x)

Inserts source Vector at specified position using pick transfer semantics. It is faster than deep copy insert, does not use deep copy constructor for T, but destroys source Vector.

 


 

void Insert(int i, std::initializer_list<Tinit)

Inserts C++11 style initialization list.

 


 

void Append(std::initializer_list<Tinit)

Appends C++11 style initialization list.

 


 

void InsertSplit(int i, Vector<T>& v, int from)

Insert the part of source vector v starting at element from till the end at position i and trims v at from, effectively splitting v into two parts.

 


 

void Append(const Vector& x)

Appends all elements of source Vector.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

x

Source Vector.

 


 

void Append(const Vector& x, int o, int c)

Appends a range of elements from source Vector.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

x

Source Vector.

o

Position of first element in source Vector to be inserted.

c

Number of elements to insert.

 


 

void Append(Vector&& x)

Appends source Vector using pick transfer semantics. It is faster than deep copy insert, does not use deep copy constructor for T, but destroys source Vector by picking.

 


 

void Swap(int i1, int i2)

Swaps elements at i1 and i2.

 


 

void Drop(int n = 1)

Drops specified number of last elements in the Vector (same as Trim(GetCount() - n)).

n

Number of elements.

 


 

T& Top()

Returns reference to the last element in the Vector.

Return value

Reference of last element in the Vector.

 


 

const T& Top() const

Returns constant reference to the last element in the Vector.

Return value

Reference of last element in the Vector.

 


 

T Pop()

Drops last element of Vector and returns its value.

Requires T to have deep copy constructor.

Return value

Value of dropped element.

 


 

operator T*()

Returns non-constant pointer to elements.

Return value

Pointer to elements.

 


 

operator const T*() const

Returns constant pointer to elements.

Return value

Pointer to elements.

 


 

Vector& operator<<(const T& x)

Operator equivalent of void Add(const T&x). By returning reference to to Vector allows adding more elements in single expression, thus e.g. allowing to construct temporary Vector as part of expression like Foo((Vector<int>() << 1 << 2 << 4)).

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

 


 

Vector& operator<<(T&& x)

Operator equivalent of void Add(TT&x). By returning reference to to Vector allows adding more elements in single expression, thus e.g. allowing to construct temporary Vector as part of expression like.

Requires T to have deep copy constructor.

Invalidates iterators and references to Vector.

 


 

String ToString() const

Converts container into String, mostly for debugging purposes.

 


 

bool operator==(const Vector<T>& bconst

bool operator!=(const Vector<T>& bconst

Compares two containers for (in)equality, using T::operator==.

 


 

int Compare(const Vector<T>& bconst

bool operator<=(const Vector<T>& bconst

bool operator>=(const Vector<T>& bconst

bool operator<(const Vector<T>& bconst

bool operator>(const Vector<T>& bconst

Lexicographically compares two containers, using SgnCompare for elements.

 


 

Vector& operator|(pick_ T& x)

Operator replacement of void AddPick(pick_ T&x). By returning reference to to Vector allows adding more elements in single expression, thus e.g. allowing to construct temporary Vector as part of expression.

Requires T to have pick constructor.

Invalidates iterators and references to Vector.

x

Source instance of T that is to be picked.

Return value

Reference to Vector (*this).

 


 

void Serialize(Stream& s)

Serializes content of Vector to/from Stream. Works only if NTL is used as part of UPP.

Requires T to have serialization operator defined.

s

Target/source stream.

 


 

void operator=(pick_ Vector& v)

Pick operator. Transfers source Vector in low constant time, but destroys it by picking.

v

Source Vector.

 


 

bool IsPicked() const

Returns true if Vector is in picked state.

Return value

true if Vector is in picked state, false otherwise.

 


 

friend T& operator<<=(T& dest, const T& src)

Optional deep copy operator. Defined using DeepCopyOptionTemplate.

Requires T to have deep copy operator or optional deep copy operator.

v

Source Vector.

 


 

typedef T ValueType

Typedef of T for use in templated algorithms.

 


 

typedef T *Iterator

Iterator type. Iterator is guaranteed to be of T* type.

 


 

typedef const T *ConstIterator

Constant iterator type. Iterator is guaranteed to be of const T* type.

 


 

ConstIterator Begin() const

Returns constant iterator to the first element in Vector.

Return value

Iterator.

 


 

ConstIterator End() const

Returns constant iterator to the position just beyond the last element in Vector.

Return value

Iterator.

 


 

ConstIterator GetIter(int iconst

Returns constant iterator to the element at specified position. Same as Begin() + i. Benefit of this methods is that in debug mode pos is range checked.

i

Required position.

Return value

Iterator.

 


 

Iterator Begin()

Returns non-constant iterator to the first element in Vector.

Return value

Iterator.

 


 

Iterator End()

Returns non-constant iterator to the position just beyond the last element in Vector.

Return value

Iterator.

 


 

Iterator GetIter(int i)

Returns non-constant iterator to the element at specified position. Same as Begin() + pos. Benefit of this methods is that in debug mode pos is range checked.

i

Required position.

Return value

Iterator.

 


 

friend void Swap(Vector& a, Vector& b)

Specialization of generic Swap for Vector. Swaps Vector in simple constant time operation.

a

First Vector to swap.

b

Second Vector to swap.

 

 

Do you want to contribute?