0
2

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 5 years have passed since last update.

C++ ベクトルの書き方

Last updated at Posted at 2019-05-08

1-3次元配列のベクトルによる書き方

1, 2, 3次元をそれぞれ vec, mat, tns と書く。

vecmac.cpp
template<typename T> using vec = std::vector<T>;
template<typename T> using mat = std::vector<std::vector<T> >;
template<typename T> using tns = std::vector<std::vector<std::vector<T> > >;

初期化

1次元の場合

vecinit.cpp
std::vector<int> a = {0, 10, 5}; // c++11
vec<double> res(n, 0.0); // initialize a vector with 0.0
vec<X> res(n); // initialize a vector with n elements

複数次元(3次元 x, y, z)の場合

tnsinit.cpp
 tns<double> alpha = vec<vec<vec<double>>>(x, vec<vec<double>>(y, vec<double>(z, 0.0)));

要素の追加と削除

ソートされたベクトル v に順序を保ったまま、要素の削除と追加を行う例

  vector<int>::iterator p;
  vector<int> v, arr;
  sort(v.begin(), v.end());
  for (int i = 0; i < n - d; i++) {
    // delete arr[i] from v
    p = lower_bound(v.begin(), v.end(), arr[i]);
    v.erase(p);
    // insert arr[i+d] into v
    p = lower_bound(v.begin(), v.end(), arr[i + d]);
    trail.insert(p, arr[i + d]);
  }

p = lower_bound(v.begin(), v.end(), a) は指定された値 a 以上の最初の要素を指すイテレータが入る。

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?