It depends on what you mean by "valid operation". There's no %
operation defined by the Standard library for std::vector
s, but you are free to define your own operator overload for that. It's not a particularly good idea - if every library decided to do that then the operator use could clash (i.e. be ambiguous in certain contexts) - but in practice you'll likely get away with it. For a more structured approach, consider creating your own class sporting nifty operators, instead of modifying the behaviour for std::vector
.
The basics of overloading are just:
template <typename T>
std::vector<T> operator%(const std::vector<T>& lhs, const std::vector<T>& rhs)
{
// generate and return your vector, for example...
std::vector<T> result;
for (size_t i = 0; i < std::min(lhs.size(), rhs.size()); ++i)
result.push_back(rhs[i] ? lhs[i] % rhs[i] : 0);
return result;
}
Update - given your own Vec
class (and assuming you have a C++11 compiler /- you can search for how to enable C++11 features separately).
Vec operator%(const Vec& lhs, const Vec& rhs)
{
return { lhs.x % rhs.x, lhs.y % rhs.y, lhs.z % rhs.z };
}
For C++03, either add a constructor Vec(float ax, float ay, float az) : x(ax), y(ay), z(az) { }
and in ther operator return Vec(lhs.x % rhs.x, lhs.y % rhs.y, lhs.z % rhs.z);
or - without a constructor...
Vec result;
result.x = lhs.x % rhs.x;
result.y = lhs.y % rhs.y;
result.z = lhs.z % rhs.z;
return result;
Of course, the above implementations just assume you want to use mod on the correspondingly indexed elements... I have no idea what makes sense in your problem domain.