Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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.

deleted 10 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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.

added 333 characters in body
Source Link
Hooked
  • 227
  • 1
  • 3
  • 9

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;
}
Source Link
Hooked
  • 227
  • 1
  • 3
  • 9
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /