我正在实时学习C ++,并且在使用向量时遇到问题,因此我编写了一些使用向量来熟悉它们的程序。
我一直遵循这篇文章中有关打印矢量的size()调用值的建议:
How can I get the size of an std::vector as an int?
我的代码是一个简单的C ++代码:
#include <vector>
int main(int argc, char ** argv) {
/* create an int vector of size 10, initialized to 0 */
std::vector<int> int_list[10];
int int_list_size;
int_list_size = static_cast<int>(int_list.size()); // <-- compilation error here
} // End main()
我在Ubuntu 16.04上出现此错误:
"error: request for member 'size' in 'int_list', which is of non-class type 'std::vector<int> [10]'
由于向量int_list的大小为10,因此size()不应该返回10,然后我可以将其转换为int吗?
答案 0 :(得分:6)
您不是在创建矢量,而是在创建矢量数组:
std::vector<int> int_list[10];
您应该使用:
std::vector<int> int_list(10);
请参阅: