LoginSignup
0
0

More than 5 years have passed since last update.

C++でコンテナの中身を標準出力にprintする

Posted at

例えばPythonであれば str.join()を使えばリストなどの中身をprintできる。

$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = ['a', 'b', 'c', 'd']
>>> print(','.join(a))
a,b,c,d

C++であれば std::copystd::ostream_iteratorを使えばコンテナの中身をprintできる。

#include <iostream>
#include <iterator>
#include <vector>

int main() {
  std::vector<std::string> v = {"a", "b", "c", "d"};
  std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, ","));
  std::cout << std::endl;
}
$ g++ -std=c++11 print_container.cpp && ./a.out
a,b,c,d,
0
0
2

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