C++17 templated single file library that represents mathematical vector
To use this library you must have a C++ compiler that supports C++17 or later.
To use this library in your project, simply include the vector.hpp header file in your code and instantiate the Vector template with the desired type, like so:
#include "vector.hpp"
// Create a vector of 3 components with elements of type double
Vector<double> v(3);- Create an empty vector
Vector() = default;- Create a vector of a given size
Vector(std::size_t);- Create a vector from a pre-existing array
Vector(const std::unique_ptr<T[]>&);- Move constructor
Vector(Vector&&);- Access individual elements of the vector using the [] operator
T& operator[](std::size_t);
const T& operator[](std::size_t) const;- Get a pointer to the first element of the vector using the begin() iterator
T* begin();
const T* begin() const;- Get a pointer past the last element of the vector using the end() iterator
T* end();
const T* end() const;- Compare two vectors for equality or inequality using the == and != operators
bool operator==(const Vector&) const;
bool operator!=(const Vector&) const;- Compare two vectors (std::less, std::greater, std::less_equal, std::greater_equal)
friend bool operator<(const Vector<A>&, const Vector<A>&);
friend bool operator>(const Vector<A>&, const Vector<A>&);
friend bool operator<=(const Vector<A>&, const Vector<A>&);
friend bool operator>=(const Vector<A>&, const Vector<A>&);- Get size of the vector
std::size_t size() const;- Find maximal element(-s) of the vector
std::variant<T, std::vector<std::size_t>> max() const;- Find minimal element(-s) of the vector
std::variant<T, std::vector<std::size_t>> min() const;- Get subvector of the vector between two given indices
Vector subvec(std::size_t, std::size_t) const;- Clear vector
void clear();- Resize vector to a gived size and initialize the new elements to desired value
void resize(std::size_t size, const T& default_value);- Insert methods
// Insert a single value at the specified position in the vector
void insert(std::size_t pos, const T& value);
// Insert multiple copies of a value at the specified position in the vector
void insert(std::size_t pos, std::size_t count, const T& value);
// Insert a range of elements, specified by iterators [first, last], at the specified position in the vector
void insert(std::size_t pos, InputIt first, InputIt last);
// Insert elements from an initializer list at the specified position in the vector
void insert(std::size_t pos, std::initializer_list<T> ilist);- Remove value at index
void erase(std::size_t pos);- Remove value in a range
void erase(std::size_t first, std::size_t last);- Concatenate two vectors
friend Vector concat(const Vector&, const Vector&);- Sum of the elements
T sum() const;- Product of the elements
T product() const;- Multiply a vector by a scalar
Vector& operator*=(T);- Add or subtract two vectors
Vector& operator+(const Vector&);
Vector& operator-(const Vector&);- Calculate the magnitude (length) of a vector
T magnitude() const;- Calculate the mean of a vector
T mean() const;- Calculate the median of a vector
T median() const;- Calculate the dot product of two vectors
friend T dot_product(const Vector<A>&);- Calculate the cross product of two vectors
friend Vector<A> cross_product(const Vector<A>&, const Vector<A>&);- Normalize the vector to have a magnitude of 1
void normalize();- Stream the vector to an output stream using the << operator
friend std::ostream& operator<<(std::ostream&, Vector<A> const&);- Overloaded std::swap
- Also you can use std::sort & range-based loops with it
#include "vector.hpp"
#include <iostream>
int main() {
Vector<double> v1(3);
v1[0] = 1.0;
v1[1] = 2.0;
v1[2] = 3.0;
Vector<double> v2(3);
v2[0] = 4.0;
v2[1] = 5.0;
v2[2] = 6.0;
double dot = dot_product(v1, v2);
std::cout << dot << std::endl; // Prints 32
auto v4 = cross_product(v1, v2);
std::cout << v4 << std::endl; // Prints [-3, 6, -3]
return 0;
}This project is licensed under the MIT License. See the LICENSE file for details.