I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that Python's NumPy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that Python's NumPy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that Python's NumPy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
Multiply vector elements by a scalar value using STL & Templatesand templates
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that python's numpyPython's NumPy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
Edit: The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
Multiply vector elements by a scalar value using STL & Templates
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that python's numpy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
Edit: The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}
Multiply vector elements by a scalar value using STL and templates
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that Python's NumPy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that python's numpy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
Edit: The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that python's numpy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
I wrote a small scientific simulation where I wanted to manipulate vectors with the same kind of functionality that python's numpy vectors use. Namely, I wanted to be able to add vectors together, multiply by constants, etc. I have need for both vectors of type int and floating point, so I wanted to template the overloading. As an example (and for something to review), I overloaded scalar multiplication as:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
for(int i=0;i<A.size();++i)
R[i] = c*A[i];
return R;
}
I would appreciate all criticism relevant to code, style, flow. My project is in C++11, so in theory I could use the newer range-based for loops for readability (I do in other parts of my code). Additionally, I think I could rewrite this using std::transform
, but I'm not sure if it would help improve the efficiency or the readability of my code.
Edit: The STL algorithm way I hinted at looks like this:
template <class T, class Q>
vector <T> operator*(const Q c, const vector<T> &A) {
vector <T> R(A.size());
std::transform(A.begin(), A.end(), R.begin(),
std::bind1st(std::multiplies<T>(),c));
return R;
}