【问题标题】:how to set or initialize default value for all elements of a table or 2d array or multdimentional array如何为表或二维数组或多维数组的所有元素设置或初始化默认值
【发布时间】:2016-05-04 11:49:27
【问题描述】:

我想为表或二维数组的所有元素设置一个默认的非零值。 array[size]={12} 将第一个元素设置为 12,其他元素连续为 0。但是 fill(array,array+size,12) 仅将所有元素设置为 12。我无法将其应用于2d 数组。有什么方法可以使用 fill() 或任何无需使用 double for 循环直接初始化的方法

#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()
  • 不要使用原始数组。

标签: c++ arrays


【解决方案1】:

对于 C 数组,您可能希望使用 memset。不过,您已将其标记为 C++,所以我觉得有义务给出 C++ 答案:

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

这会创建大小为 10 的 10 个 std::vector&lt;int&gt;s 的 std::vector,每个元素初始化为 45。

有关ideone,请参阅here

【讨论】:

    【解决方案2】:

    与使用一维数组的方式几乎相同:

    #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';
        }
    }
    

    注意:第一个索引不需要值。在这种情况下,编译器会自动计算这个索引。

    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';
        }
    }
    

    【讨论】:

    • 但是想想如果数组大小是 100*100 那么单独设置它们是多么困难
    【解决方案3】:

    一个解决方案(不是很优雅,我知道)可以是

    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;
     }
    

    【讨论】:

    • auto(在那种情况下)是 C++11 的一个特性。你能告诉我们错误吗?添加了一个完整的示例。
    • C:\Users\USER\Desktop\ffdg.cpp\main.cpp|8|warning: 'auto' 在 C++11 中改变了含义;请删除它[-Wc++0x-compat]|
    • 我刚刚过去了您添加的示例。运行,然后发生这个类似的错误
    • 您确定要编译 C++11 吗?根据该错误,您正在编译 C++98。你在mingw中使用gcc?你应该添加-std=c++11
    【解决方案4】:

    这并不比 memset 好多少,但至少你可以为每个 int 指定值,而不是用 std::fill 像这样使用每个字节:

    #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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2011-12-08
      相关资源
      最近更新 更多