0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vectorと単項の演算を作ってみた

Posted at

0-indexedのものを1-indexedにして出力したいなぁってなったときにわざわざループを回して+1したりvalarrayにおきかえたりするのはひと手間がかかると感じたので演算子を作った

operator+=

template <typename T>
void operator+=(vector<T> &vs, T x){
  for(T &v:vs) v += x;
}

他の四則演算もほとんど同じ

operator+

template <typename T>
vector<T> operator+(const vector<T> &vs, T x){
  vector<T> res=vs;
  for(T &v:res) v += x;
  return res;
}

他の四則演算もほとんど同じ

完成形

丁寧に書いたもの
template <typename T>
void operator+=(vector<T> &vs, T x){
  for(T &v:vs) v += x;
}

template <typename T>
void operator-=(vector<T> &vs, T x){
  for(T &v:vs) v -= x;
}

template <typename T>
void operator*=(vector<T> &vs, T x){
  for(T &v:vs) v *= x;
}

template <typename T>
void operator/=(vector<T> &vs, T x){
  for(T &v:vs) v /= x;
}

template <typename T>
void operator%=(vector<T> &vs, T x){
  for(T &v:vs) v %= x;
}

template <typename T>
vector<T> operator+(const vector<T> &vs, T x){
  vector<T> res=vs;
  for(T &v:res) v += x;
  return res;
}

template <typename T>
vector<T> operator-(const vector<T> &vs, T x){
  vector<T> res=vs;
  for(T &v:res) v -= x;
  return res;
}

template <typename T>
vector<T> operator*(const vector<T> &vs, T x){
  vector<T> res=vs;
  for(T &v:res) v *= x;
  return res;
}

template <typename T>
vector<T> operator/(const vector<T> &vs, T x){
  vector<T> res=vs;
  for(T &v:res) v /= x;
  return res;
}

template <typename T>
vector<T> operator%(const vector<T> &vs, T x){
  vector<T> res=vs;
  for(T &v:res) v %= x;
  return res;
}
1行ずつにまとめたもの
template <typename T> void operator+=(vector<T> &vs, T x) { for ( T &v : vs ) v += x; }
template <typename T> void operator-=(vector<T> &vs, T x) { for ( T &v : vs ) v -= x; }
template <typename T> void operator*=(vector<T> &vs, T x) { for ( T &v : vs ) v *= x; }
template <typename T> void operator/=(vector<T> &vs, T x) { for ( T &v : vs ) v /= x; }
template <typename T> void operator%=(vector<T> &vs, T x) { for ( T &v : vs ) v %= x; }
template <typename T> vector<T> operator+(const vector<T> &vs, T x) { vector<T> res = vs; for ( T &v : res ) v += x; return res; }
template <typename T> vector<T> operator-(const vector<T> &vs, T x) { vector<T> res = vs; for ( T &v : res ) v -= x; return res; }
template <typename T> vector<T> operator*(const vector<T> &vs, T x) { vector<T> res = vs; for ( T &v : res ) v *= x; return res; }
template <typename T> vector<T> operator/(const vector<T> &vs, T x) { vector<T> res = vs; for ( T &v : res ) v /= x; return res; }
template <typename T> vector<T> operator%(const vector<T> &vs, T x) { vector<T> res = vs; for ( T &v : res ) v %= x; return res; }

あとがき

これでほんの少し便利になったと思う

参考

  • [http://tech.nitoyon.com/ja/blog/2007/12/11/boost-asign/:title]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?