简体   繁体   English

尝试 output 向量时出现第 30 行的编译器错误

[英]Compiler error at line 30 when trying to output vector

I'm just starting to take C++ courses online and right now, I'm doing vectors.我刚开始在线学习 C++ 课程,现在我正在做矢量。 Can someone please tell me what I did wrong on line 30 and 31?有人可以告诉我我在第 30 行和第 31 行做错了什么吗? I can't seem to output what is in vector_2d at index 0 and 1. Thanks!我似乎无法 output 索引 0 和 1 处的 vector_2d 中的内容。谢谢!

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

int main()
{
    vector <int> vector1 {};
    vector <int> vector2 {};

    vector1.push_back(10);
    vector1.push_back(20);

    cout << vector1.at(0) << endl;
    cout << vector1.at(1) << endl;
    cout << vector1.size() << endl;



    vector2.push_back(100);
    vector2.push_back(200);

    cout << vector2.at(0) << endl;
    cout << vector2.at(1) << endl;

    vector <vector <int>> vector_2d {};

    vector_2d.push_back(vector1);
    vector_2d.push_back(vector2);

    cout << vector_2d.at(0) << endl;
    cout << vector_2d.at(1) << endl;
    
    return 0;
}

Update, I've copied and pasted below most of the complete error output, I'm limited to 30,000 characters.更新,我已经复制并粘贴了大部分完整的错误 output,我限制为 30,000 个字符。 :

C:\Users\FT86TT\AppData\Local\JetBrains\CLion2021.2\cygwin_cmake\bin\cmake.exe --build "/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/cmake-build-debug" --target untitled14 -- -j 6
Scanning dependencies of target untitled14
[ 50%] Building CXX object CMakeFiles/untitled14.dir/main.cpp.o
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp: In function 'int main()':
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:10: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'})
   32 |     cout << vector_2d.at(0) << endl;
      |     ~~~~ ^~ ~~~~~~~~~~~~~~~
      |     |                   |
      |     |                   __gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type {aka std::vector<int>}
      |     std::ostream {aka std::basic_ostream<char>}
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:108:7: note: candidate: '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>]'
  108 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:108:36: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka '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>&)'}
  108 |       operator<<(__ostream_type& (*__pf)(__ostream_type&))
      |                  ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:117:7: note: candidate: '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>]'
  117 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:117:32: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka '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>&)'}
  117 |       operator<<(__ios_type& (*__pf)(__ios_type&))
      |                  ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:127:7: note: candidate: '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>]'
  127 |       operator<<(ios_base& (*__pf) (ios_base&))
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:127:30: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'std::ios_base& (*)(std::ios_base&)'
  127 |       operator<<(ios_base& (*__pf) (ios_base&))
      |                  ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:166:7: note: candidate: '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>]'
  166 |       operator<<(long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:166:23: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long int'
  166 |       operator<<(long __n)
      |                  ~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:170:7: note: candidate: '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>]'
  170 |       operator<<(unsigned long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:170:32: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long unsigned int'
  170 |       operator<<(unsigned long __n)
      |                  ~~~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:174:7: note: candidate: '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>]'
  174 |       operator<<(bool __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:174:23: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'bool'
  174 |       operator<<(bool __n)
      |                  ~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:826,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ostream.tcc:91:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]'
   91 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ostream.tcc:92:22: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'short int'
   92 |     operator<<(short __n)
      |                ~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:181:7: note: candidate: '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>]'
  181 |       operator<<(unsigned short __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:181:33: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'short unsigned int'
  181 |       operator<<(unsigned short __n)
      |                  ~~~~~~~~~~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:826,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ostream.tcc:105:5: note: candidate: 'std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]'
  105 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ostream.tcc:106:20: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'int'
  106 |     operator<<(int __n)
      |                ~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:192:7: note: candidate: '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>]'
  192 |       operator<<(unsigned int __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:192:31: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'unsigned int'
  192 |       operator<<(unsigned int __n)
      |                  ~~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:201:7: note: candidate: '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>]'
  201 |       operator<<(long long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:201:28: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long long int'
  201 |       operator<<(long long __n)
      |                  ~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:205:7: note: candidate: '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>]'
  205 |       operator<<(unsigned long long __n)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:205:37: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long long unsigned int'
  205 |       operator<<(unsigned long long __n)
      |                  ~~~~~~~~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:220:7: note: candidate: '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>]'
  220 |       operator<<(double __f)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:220:25: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'double'
  220 |       operator<<(double __f)
      |                  ~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:224:7: note: candidate: '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>]'
  224 |       operator<<(float __f)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:224:24: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'float'
  224 |       operator<<(float __f)
      |                  ~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:232:7: note: candidate: '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>]'
  232 |       operator<<(long double __f)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:232:30: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'long double'
  232 |       operator<<(long double __f)
      |                  ~~~~~~~~~~~~^~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:245:7: note: candidate: '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>]'
  245 |       operator<<(const void* __p)
      |       ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:245:30: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'const void*'
  245 |       operator<<(const void* __p)
      |                  ~~~~~~~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:826,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ostream.tcc:119:5: note: candidate: '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>]'
  119 |     basic_ostream<_CharT, _Traits>::
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ostream.tcc:120:34: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} to 'std::basic_ostream<char>::__streambuf_type*' {aka 'std::basic_streambuf<char>*'}
  120 |     operator<<(__streambuf_type* __sbin)
      |                ~~~~~~~~~~~~~~~~~~^~~~~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/string:55,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/locale_classes.h:40,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ios_base.h:41,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/basic_string.h:6530:5: note: candidate: '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>&)'
 6530 |     operator<<(basic_ostream<_CharT, _Traits>& __os,
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/basic_string.h:6530:5: note:   template argument deduction/substitution failed:
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:27: note:   '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'} is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
   32 |     cout << vector_2d.at(0) << endl;
      |                           ^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/bits/ios_base.h:46,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ios:42,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:38,
                 from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/system_error:263:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)'
  263 |     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/system_error:263:5: note:   template argument deduction/substitution failed:
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:25: note:   cannot convert 'vector_2d.std::vector<std::vector<int> >::at(0)' (type '__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >, std::vector<int> >::value_type' {aka 'std::vector<int>'}) to type 'const std::error_code&'
   32 |     cout << vector_2d.at(0) << endl;
      |             ~~~~~~~~~~~~^~~
In file included from /usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/iostream:39,
                 from /cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:506:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)'
  506 |     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
      |     ^~~~~~~~
/usr/lib/gcc/x86_64-pc-cygwin/11/include/c++/ostream:506:5: note:   template argument deduction/substitution failed:
/cygdrive/c/Users/FT86TT/Desktop/COP 3363/untitled14/main.cpp:32:27: note:   deduced conflicting types for parameter '_CharT' ('char' and 'std::vector<int>')
   32 |     cout << vector_2d.at(0) << endl;
      |                           ^

vector_2d is of type std::vector<std::vector<int>> , so vector_2d.at(0) and vector_2d.at(1) is of type std::vector . vector_2d属于std::vector<std::vector<int>>类型,因此vector_2d.at(0)vector_2d.at(1)属于std::vector类型。 Since std::cout don't know how to print an object of type std::vector<int> , it gives an error.由于std::cout不知道如何打印std::vector<int>类型的 object ,因此会出错。

If you want to print the whole vector, do:如果要打印整个向量,请执行以下操作:

for(const auto &i : vector_2d.at(0))
{
    std::cout << i << ' ';
}
std::cout << '\n'
for(const auto &i : vector_2d.at(1))
{
    std::cout << i << ' ';
}

The elements of the vector vector_2d are in turn objects of the type std::vector.向量vector_2d 的元素又是std::vector 类型的对象。

vector <vector <int>> vector_2d {};

There is no standard overloaded operator << for vectors.向量没有标准的重载运算符 <<。 So the compiler issues the error message for these statements所以编译器会为这些语句发出错误消息

cout << vector_2d.at(0) << endl;
cout << vector_2d.at(1) << endl;

You could use the range based for loop to output elements of the vectors as for example例如,您可以将基于范围的 for 循环用于向量的 output 元素

for ( const auto &item : vector_2d.at(0) )
{
    std::cout << item << ' ';
}
std::cout << '\n';

for ( const auto &item : vector_2d.at(1) )
{
    std::cout << item << ' ';
}
std::cout << '\n';

Otherwise you need to overload the operator << for the class std::vector your self as for example否则,您需要为 class std::vector 重载运算符 << 例如

$include <iostream>
#include <vector>

std::ostream &operator <<( std::ostream &os, const std::vector<int> &v )
{
    for (const auto &item : v)
    {
        os << item << ' ';
    }

    return os;
}

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    std::cout << v << '\n';
}

And call the operator like并像这样调用操作员

std::cout << vector_2d.at(0) << '\n';

Pay attention to that this initialization of the vectors注意向量的初始化

vector <int> vector1 {};
vector <int> vector2 {};

is redundant and has no effect.是多余的,没有效果。 You could just write你可以写

vector <int> vector1;
vector <int> vector2;

In the both cases empty vectors are defined.在这两种情况下,都定义了空向量。

I changed:我变了:

cout << vector_2d.at(0) << endl;
cout << vector_2d.at(1) << endl;

to:至:

cout << vector_2d.at(0).at(0) << " " << vector_2d.at(0).at(1) << endl;
cout << vector_2d.at(1).at(0) << " " << vector_2d.at(1).at(1) << endl;

Thanks all!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM