这是我的2D整数向量。
vector<vector<int>> nodes (r*c, vector<int> (5));
使用for循环我正在尝试在此向量中的push_back值。 r和c将整数传递给此函数。
for(i = 0; i < r*c; i++)
{
nodes[i].push_back({i/c, i%c, -1, -1, 0});
}
答案 0 :(得分:0)
使用方法push_back
代替nodes[i].insert( nodes[I].end(), {i/c, i%c, -1, -1, 0});
vector<vector<int>> nodes (r*c );
但在此之前你应该声明像
这样的矢量assign
否则每个子向量的前5个元素将包含零。
您也可以使用方法nodes[i].assign( {i/c, i%c, -1, -1, 0});
core.umd.js:3004 EXCEPTION: Uncaught (in promise): Error: Error in `enter code here`http://localhost:3000/app/dashboard.component.js class DashboardComponent_Host - inline template:0:0 caused by: $ is not defined
ReferenceError: $ is not defined
at DashboardComponent.ngOnInit (http://localhost:3000/app/dashboard.component.js:22:9)
at Wrapper_DashboardComponent.detectChangesInInputProps (/AppModule/DashboardComponent/wrapper.ngfactory.js:18:53)
at _View_DashboardComponent_Host0.detectChangesInternal (/AppModule/DashboardComponent/host.ngfactory.js:30:32)
at _View_DashboardComponent_Host0.AppView.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:9305:18)
at _View_DashboardComponent_Host0.DebugAppView.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:9410:48)
at ViewRef_.detectChanges (http://localhost:3000/node_modules/@angular/core/bundles/core.umd.js:7398:24)
at RouterOutlet.activate (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:3458:46)
at ActivateRoutes.placeComponentIntoOutlet (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:2955:20)
at ActivateRoutes.activateRoutes (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:2933:26)
at eval (http://localhost:3000/node_modules/@angular/router/bundles/router.umd.js:2902:23)
答案 1 :(得分:0)
nodes[i]
是整数的向量。您正尝试将向量附加到整数向量。
要么:
nodes.push_back({i/c, i%c, -1, -1, 0});
或
nodes[i] = {i/c, i%c, -1, -1, 0};
第二种解决方案是最好的,因为你已经为你的矢量赋予了适当的尺寸。无需添加r*c
个更多元素......
创建为空,并填充push_back
:
std::vector<std::vector<int>> nodes;
for(i = 0; i < r*c; i++)
{
nodes.push_back({i/c, i%c, -1, -1, 0});
}
或使用适当的尺寸创建并指定项目:
std::vector<std::vector<int>> nodes (r*c, std::vector<int> (5));
for(i = 0; i < r*c; i++)
{
nodes[i] = {i/c, i%c, -1, -1, 0};
}