Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
g++ -g bug
#include <vector> #include <assert.h> class Vector { public: Vector(std::size_t n) : elems(n) { } // construct vector using initializer list Vector(std::initializer_list<double> init) { for (auto i : init) elems.push_back(i); } double &operator[](std::size_t i) { return elems[i]; } double operator[](std::size_t i) const { return elems[i]; } std::size_t size() const { return elems.size(); } private: std::vector<double> elems; }; template <typename E1, typename E2> class VectorSum { const E1 &_u; const E2 &_v; public: VectorSum(const E1 &u, const E2 &v) : _u(u), _v(v) { assert(u.size() == v.size()); } double operator[](std::size_t i) const { return _u[i] + _v[i]; } std::size_t size() const { return _v.size(); } }; template <typename E1, typename E2> const VectorSum<E1, E2> operator + (const E1 &u, const E2 &v) { return VectorSum<E1, E2>(u, v); } int main() { Vector v0 = {23.4, 12.5, 144.56, 90.56}; Vector v1 = {67.12, 34.8, 90.34, 89.30}; Vector v2 = {34.90, 111.9, 45.12, 90.5}; auto sum = v0 + v1 + v2; Vector v3(v0.size()); v3[0] = sum[0]; }
run
|
edit
|
history
|
help
0
Array Subset of another array
temp_new
Bank System
Tower of hanoi
map::swap()_30-Seconds-of-C++
k-tree 431 C
project
Matrix spiral print
container access all elements
CyclicExpression Checker