我想爲表或2d數組的所有元素設置默認的非零值。 array [size] = {12}設置第一個元素只有12個,其他的全部爲0。但是fill(array,array + size,12)將所有元素設置爲12行。我不能將它應用於2D array.Is有沒有辦法做到這一點使用填()或使用雙for循環如何設置或初始化表的所有元素的默認值或2d數組或多維數組
#include <iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
using namespace std;
int main()
{
int arra[10][10];//declare 2d array
for(int k=0;k<10;k++)//takes k's value 10 for 10 rows
fill(arra,arra+10,45);//select a row and set all columns to 45 didn't work
}
數組初始化 http://www.fredosaurus.com/notes-cpp/arrayptr/array-initialization.html
您可以優先使用[memset()](http://stackoverflow.com/questions/13327155/memset-definition-and-use)。 –
請勿使用原始數組。 – erip