I need to have assignment of type std::vector = arma::vec, i.e. to assign vector from armadillo math library to a std::vector.
template <typename T>
std::vector<T>& std::vector<T>::operator=(arma::vec right) {
std::size_t s(right.size());
this->resize(s);
for (std::size_t i = 0; i < s; i++)
*this[i] = right(i);
return *this;
}
I tried this way, but the compiler is not agree:
error: invalid use of incomplete type ‘class std::vector<_RealType>’
Is it possible to overload the assignment operator for std::vector class?
Points to consider:
operator=
can be overloaded only as a member function.arma::vec
. Use them instead of trying to define an oprator=
overload.Example code that demonstrates how you can assign different kinds of containers to a std::vector
.
#include <iostream>
#include <vector>
#include <set>
int main()
{
std::vector<int> a;
int b[] = {1, 2, 3, 4, 5};
std::set<int> c{10, 20, 30, 40, 50};
a = {std::begin(b), std::end(b)};
for ( int el : a )
{
std::cout << el << " ";
}
std::cout << std::endl;
a.assign(std::begin(c), std::end(c));
for ( int el : a )
{
std::cout << el << " ";
}
std::cout << std::endl;
}
In your case, you can use either of the following:
std::vector<YourType> v;
v = {std::begin(right), std::end(right)};
v.assign(std::begin(right), std::end(right));