2016-05-04 23 views
6

我想爲表或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

+1

您可以優先使用[memset()](http://stackoverflow.com/questions/13327155/memset-definition-and-use)。 –

+0

請勿使用原始數組。 – erip

回答

2

幾乎相同的方式,而不直接初始化任何方式,你會用1二維陣列:

#include <iostream> 
#include <iomanip> 

int main() { 
    int arr[10][10] = { 
     { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 
     { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }, 
     { 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 }, 
     { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 }, 
     { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 }, 
     { 6, 12, 18, 24, 30, 36, 42, 48, 54, 60 }, 
     { 7, 14, 21, 28, 35, 42, 49, 56, 63, 70 }, 
     { 8, 16, 24, 32, 40, 48, 56, 64, 72, 80 }, 
     { 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 }, 
     { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 } 
    }; 

    for(int i=0; i<10; ++i) { 
     for(int j=0; j<10; ++j) { 
      std::cout << std::setw(4) << arr[i][j]; 
     } 
     std::cout << '\n'; 
    } 
} 

注意:第一個索引不是r等於一個價值。在這種情況下,編譯器會自動計算這個索引。

int arr[][10] = { ... }; 

編輯

的替代方案,將避免雙重循環是:

## Heading ###include <iostream> 
#include <iomanip> 


int main() { 
    int arr[10][10] = {}; // Initializes all values to 0 
    for(int i=0; i<10; ++i) arr[i][0] = 12; 

    for(int i=0; i<10; ++i) { 
     for(int j=0; j<10; ++j) { 
      std::cout << std::setw(4) << arr[i][j]; 
     } 
     std::cout << '\n'; 
    } 
} 
+1

但是,想想看,如果數組大小是100 * 100那麼它是多麼困難的設置它們個人 –

1

對於C數組,你可能會想使用memset。您已將此作爲C++,雖然,所以我覺得有必要給一個C++回答:

std::vector<std::vector<int>> v(10, std::vector<int>(10, 45)); 

這與初始化爲45

見每個元素創建 A S尺寸10用於ideone的here

1

溶液(不是真正優雅的,我知道),可以

for (auto & row : arra) 
    for (auto & elem : row) 
     elem = 45; 

,或者使用std::fill()

for (auto & row : arra) 
    std::fill(std::begin(row), std::end(row), 45); 

---- 編輯 ----

全部示例

#include <iostream> 

int main() 
{ 
    int a[10][10]; 

    // mode A 
    for (auto & row : a) 
     for (auto & elem : row) 
     elem = 45; 

    // mode B 
    for (auto & row : a) 
     std::fill(std::begin(row), std::end(row), 47); 

    for (int i = 0 ; i < 10 ; ++i) 
    { 
     for (int j = 0 ; j < 10 ; ++j) 
     std::cout << '[' << a[i][j] << ']'; 

     std::cout << '\n'; 
    } 

    return 0; 
} 
+0

我使用C++ 11。它只是告訴我錯誤的汽車 –

+0

'汽車'(在該上下文中)是一個C + + 11特徵。你能告訴我們錯誤嗎?增加了一個完整的例子 – max66

+0

C:\ Users \ USER \ Desktop \ ffdg.cpp \ main。cpp | 8 |警告:'auto'在C++ 11中改變了意義;請刪除它[-WC++ 0x-compat] | –

1

這比memset的也好不了多少,但至少對於每個詮釋,而不是與STD每個字節可以指定值::使用這樣的填充物:

#include <algorithm> 
#include <stdio.h> 

int main() { 
    int arra[10][10]; 
    std::fill((int*)arra,(int*)arra+sizeof(arra)/sizeof(int),45); 

    for (auto& row : arra) { 
    for (auto& x : row) 
     printf("%d ", x); 
    puts(""); 
    } 

    return 0; 
} 

這依賴於所述陣列元件是在連續記憶。

45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
45 45 45 45 45 45 45 45 45 45 
相關問題