在这篇文章中解决Chris的回答How to print out the contents of a vector?我试图重载ostream <<
来打印矢量向量。这是我从文件中读取的内容,其中每一行都成为一个向量:
1, 2, 3
4, 4, 4, 4, 4
7, 7
41, 52, 632
所以结构看起来像
v[0] = [1, 2, 3]
等等。 我认为克里斯的解决方案应该完全适用于这种情况,它肯定适用于1d矢量,但我试图理解为什么它不适用于2d矢量。代码如下:
template <typename T>
std::ostream& operator << (std::ostream& out, const std::vector<T>& v){
if(! v.empty()){
std::cout<< '[';
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout<< ']' << std::endl;
}
else{
std::cout << "[ ]" << std::endl;
}
}
我认为我需要将std::ostream_iterator
中的函数替换为<<
,因为这是我首先要覆盖的内容,但我已尝试<<
和{{ 1}}和ostream::<<
这些都会产生编译时错误。这样做的正确方法是什么?
如果我尝试在int的向量向量上运行上面的代码,我有以下编译时错误:
std:: <<
这是完整档案:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h: In instantiation of ‘std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = std::vector<int>; _CharT = char; _Traits = std::char_traits<char>]’:
/usr/include/c++/4.9/bits/stl_algobase.h:336:18: required from ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const std::vector<int>*; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:396:70: required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const std::vector<int>*; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:434:38: required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:466:17: required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
ctest.cpp:15:75: required from ‘std::ostream& operator<<(std::ostream&, const std::vector<T>&) [with T = std::vector<int>; std::ostream = std::basic_ostream<char>]’
ctest.cpp:70:18: required from here
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: error: no match for ‘operator<<’ (operand types are ‘std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >::ostream_type {aka std::basic_ostream<char>}’ and ‘const std::vector<int>’)
*_M_stream << __value;
^
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: candidates are:
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:108:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/include/c++/4.9/ostream:108:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.9/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/include/c++/4.9/ostream:117:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.9/ostream:127:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/include/c++/4.9/ostream:127:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.9/ostream:166:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^
/usr/include/c++/4.9/ostream:166:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long int’
/usr/include/c++/4.9/ostream:170:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^
/usr/include/c++/4.9/ostream:170:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long unsigned int’
/usr/include/c++/4.9/ostream:174:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^
/usr/include/c++/4.9/ostream:174:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘bool’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘short int’
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:181:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^
/usr/include/c++/4.9/ostream:181:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘short unsigned int’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:105:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘int’
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:192:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^
/usr/include/c++/4.9/ostream:192:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘unsigned int’
/usr/include/c++/4.9/ostream:201:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^
/usr/include/c++/4.9/ostream:201:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long long int’
/usr/include/c++/4.9/ostream:205:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^
/usr/include/c++/4.9/ostream:205:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long long unsigned int’
/usr/include/c++/4.9/ostream:220:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^
/usr/include/c++/4.9/ostream:220:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘double’
/usr/include/c++/4.9/ostream:224:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^
/usr/include/c++/4.9/ostream:224:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘float’
/usr/include/c++/4.9/ostream:232:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^
/usr/include/c++/4.9/ostream:232:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long double’
/usr/include/c++/4.9/ostream:245:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^
/usr/include/c++/4.9/ostream:245:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘const void*’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:119:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
In file included from /usr/include/c++/4.9/string:52:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/basic_string.h:2772:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/include/c++/4.9/bits/basic_string.h:2772:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: ‘const std::vector<int>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:471:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
/usr/include/c++/4.9/ostream:471:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::vector<int>’)
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:476:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
/usr/include/c++/4.9/ostream:476:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:482:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
/usr/include/c++/4.9/ostream:482:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:488:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
/usr/include/c++/4.9/ostream:488:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘signed char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:493:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
/usr/include/c++/4.9/ostream:493:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘unsigned char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:513:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
/usr/include/c++/4.9/ostream:513:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: mismatched types ‘const _CharT*’ and ‘std::vector<int>’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:321:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.9/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:530:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.9/ostream:530:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:543:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
/usr/include/c++/4.9/ostream:543:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const signed char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:548:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
/usr/include/c++/4.9/ostream:548:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const unsigned char*’
*_M_stream << __value;
^
答案 0 :(得分:2)
这在很大程度上取决于您希望输出格式化的方式。您可以按原样使用运算符,它可以工作(尽管您可能喜欢也可能不喜欢格式化):
#include <vector>
using std::vector;
#include <iostream>
using std::ostream;
template<typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
out << "[";
size_t last = v.size() - 1;
for (size_t i = 0; i < v.size(); ++i) {
out << v[i];
if (i != last)
out << ", ";
}
out << "]";
return out;
}
int main() {
std::vector<std::vector<int> > x { { 1, 2, 3 }, { 4, 5, 6 } };
std::cout << x;
}
产地:
[[1, 2, 3], [4, 5, 6]]
如果您需要/想要更改一个实例与另一个实例之间的格式,例如在数字之间使用逗号,以及在行之间添加新行,则事情变得更加困难。解决这个问题有点不重要,但我现在就把它留在这里,因为它根本不清楚你真的想要那个。
答案 1 :(得分:1)
这个想法使用std::enable_if
来迭代我们选择定义为is_container
的成员。我写的代码更多是出于好奇而不是出于实用性。
#include <iostream>
#include <type_traits>
#include <vector>
// by default you declare that no type is a container
template<typename T>
struct is_container {
static constexpr bool value = false;
};
// then you specify which types are actually containers.. I'm just declaring
// std::vector<T, A>, but you could make a similar declaration for list, array,
// and so on.
template<typename T, typename Allocator>
struct is_container<std::vector<T, Allocator> > {
static constexpr bool value = true;
};
// then you enable a print function to print all elements in a container (you
// can modify the actual formatting to your liking)
template<typename T>
typename std::enable_if<
is_container<T>::value,
std::ostream& >::type
inline operator<<(std::ostream& os, const T& container) {
const auto N = container.size();
std::size_t current = 0;
os << "[";
for(const auto & item : container) {
const char* separator = current++ == N - 1 ? "" : ", ";
os << item << separator;
}
os << "]";
return os;
}
// then the code will work for any level of nesting
int main() {
int scalar = 1;
std::cout << "Scalar: " << scalar << std::endl;
std::vector<int> vector {1, 2, 3, 4};
std::cout << "Vector: " << vector << std::endl;
std::vector< std::vector<int> > vvector {
{1, 2, 3, 4},
{5, 6, 7},
{8, 9, 10, 11, 12}
};
std::cout << "Vector<Vector>: " << vvector << std::endl;
std::vector< std::vector< std::vector<int> > > vvvector {
{
{1, 2, 3, 4},
{5, 6, 7},
{8, 9, 10, 11, 12}
},
{
{-1, -2, -3, -4},
{-5, -6, -7},
{-8, -9, -10}
}
};
std::cout << "Vector<Vector<Vector>>: " << vvvector << std::endl;
// you get the idea...
return 0;
}
示例运行:
$ g++ example.cpp -std=c++14 -Wall -Wextra -O3
$ ./a.out
Scalar: 1
Vector: [1, 2, 3, 4]
Vector<Vector>: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12]]
Vector<Vector<Vector>>: [[[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12]], [[-1, -2, -3, -4], [-5, -6, -7], [-8, -9, -10]]]