C++のコンテナ vector メモ

きっと,数学とか使うベクトルとおんなじ.
概念は.

オブジェクトを格納するときは,

が必要.

使えそうな関数.

size() ベクトルのサイズ
begin() 先頭を指す反復子
end() 末尾を指す反復子
push_back() 末尾に値を追加
insert() 指定したところに挿入
erase() 要素を削除

使い方メモ.

#include <iostream>
#include <vector>
using namespace std;

int main(){
  vector<int> v;

  cout << "# vector.size()" << endl;
  cout << "\tVector size:" << v.size() << endl;

  cout << "--------------------" << endl;

  cout << "# vector.push_back()" << endl;
  for(int i=0; i<10; i++){
    v.push_back(i);
  }

  cout << "--------------------" << endl;

  cout << "# vector.size()" << endl;
  cout << "\tVector size:" << v.size() << endl;

  cout << "--------------------" << endl;

  cout << "# v[i]" << endl;
  for(int i=0; i<v.size(); i++){
    cout << v[i] << ' ';
  }
  cout << endl;

  cout << "--------------------" << endl;
  cout << "# iterator" << endl;

  vector<int>::iterator p;
  p = v.begin();

  while(p != v.end()){
    cout << *p << ' ';
    p++;
  }
  cout << endl;

  cout << "****************************************" << endl;
  cout << "# 100でfillされたszie10のvector" << endl;

  vector<int> v2(10, 1);
  p = v2.begin();
  while(p != v2.end()){
    cout << *p << ' ';
    p++;
  }
  cout << endl;

  cout << "--------------------" << endl;

  cout << "# 6番目から3つ90を挿入" << endl;
  v2.insert(v2.begin()+5, 3, 9);

  p = v2.begin();
  while(p != v2.end()){
    cout << *p << ' ';
    p++;
  }
  cout << endl;


  cout << "--------------------" << endl;

  cout << "# 3番目から4つ削除" << endl;
  v2.erase(v2.begin()+2, v2.begin()+2+4);

  p = v2.begin();
  while(p != v2.end()){
    cout << *p << ' ';
    p++;
  }
  cout << endl;

  return 0;
}