[Error] request for member ‘push_back‘ in ‘a‘, which is of non-class type ‘std::vector<int> [500]‘

C++ vector.push_back错误:请求成员“push_back”...,该成员属于非类类型“std::vector<int>[500]”

[Error] request for member 'push_back' in 'a', which is of non-class type 'std::vector<int> [500]'

#include <bits/stdc++.h>
using namespace std;

int main()
{
	vector<int> a[500];
	for(int i=0;i<500;i++)
	{
		for(int j=0;j<5;j++)
		a.push_back(j);
		
	}
		cout<<a[1].size()<<endl;
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		cout<<a[i][j]<<" ";
		cout<<endl;
		
	}

return 0;
}

 报错原因:

 a.push_back(j) 单维数组插入,而vector<int>a[500] 定义的是一维长度500,二维动态的二维数组

故将  a.push_back(j) 改为          a[i].push_back(j);

猜你喜欢

转载自blog.csdn.net/m0_59081230/article/details/128069954