C++ Wiki

2015.02.21

Tags: C++11

初期化リストの使い方

サンプルコード

#include <iostream>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
  for(T v: vec){
    os << v << " ";
  }
  return os;
}

int main()
{
  std::vector<int> v1 = { 1, 2, 3 };
  std::vector<std::vector<char> > v2 = { {'a', 'b'}, {'c', 'd'} };

  std::cout << v1 << std::endl;
  std::cout << v2 << std::endl;

  return 0;
}

実行結果

1 2 3
a b  c d