微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

声明二维向量的大小时出错

如何解决声明二维向量的大小时出错

我想先声明我的二维向量,然后给它一个大小。

但是为什么我会出错?

谁能给我解释一下?

int main() {
    vector<vector<int>> a;
    a = vector<int>(16,vector<int>(15));
    cout << a.size() << a[0].size();
}

这样做的原因是我之前不知道大小,但是在从用户那里得到输入之后,我想给它大小。

错误

Char 9: error: no matching constructor for initialization of 'vector<int>'
    a = vector<int>(16,vector<int>(15));
        ^           ~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:507:7: note: candidate constructor not viable: no kNown conversion from 'vector<int>' to 'const std::vector<int,std::allocator<int>>::allocator_type' (aka 'const std::allocator<int>') for 2nd argument
      vector(size_type __n,const allocator_type& __a = allocator_type())
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:519:7: note: candidate constructor not viable: no kNown conversion from 'vector<int>' to 'const std::vector<int,std::allocator<int>>::value_type' (aka 'const int') for 2nd argument
      vector(size_type __n,const value_type& __value,^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:572:7: note: candidate constructor not viable: no kNown conversion from 'int' to 'const std::vector<int,std::allocator<int>>' for 1st argument
      vector(const vector& __x,const allocator_type& __a)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:604:7: note: candidate constructor not viable: no kNown conversion from 'int' to 'std::vector<int,std::allocator<int>>' for 1st argument
      vector(vector&& __rv,const allocator_type& __m)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:622:7: note: candidate constructor not viable: no kNown conversion from 'int' to 'initializer_list<std::vector<int,std::allocator<int>>::value_type>' (aka 'initializer_list<int>') for 1st argument
      vector(initializer_list<value_type> __l,^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:650:2: note: candidate template ignored: deduced conflicting types for parameter '_InputIterator' ('int' vs. 'std::vector<int,std::allocator<int>>')
        vector(_InputIterator __first,_InputIterator __last,^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:494:7: note: candidate constructor not viable: requires single argument '__a',but 2 arguments were provided
      vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:550:7: note: candidate constructor not viable: requires single argument '__x',but 2 arguments were provided
      vector(const vector& __x)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:569:7: note: candidate constructor not viable: requires 1 argument,but 2 were provided
      vector(vector&&) noexcept = default;
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:582:7: note: candidate constructor not viable: requires 3 arguments,but 2 were provided
      vector(vector&& __rv,const allocator_type& __m,true_type) noexcept
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:586:7: note: candidate constructor not viable: requires 3 arguments,false_type)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:484:7: note: candidate constructor not viable: requires 0 arguments,but 2 were provided
      vector() = default;
      ^
1 error generated.

解决方法

vector<int> 不是二维向量。

取而代之的是:

a = vector<int>(16,vector<int>(15));

你应该使用这个:

a = vector<vector<int>>(16,vector<int>(15));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?