二维数组值C ++ [英] 2D array values C++

查看:141
本文介绍了二维数组值C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要声明一个二维数组并赋值给它,不运行一个for循环。

我想我可以用下面的想法

  int数组[5] = {1,2,3,4,5};

工作正常初始化二维数组为好。但很显然,我的编译器不喜欢这样。

  / *
 1 8 12 20 25
 5 9 13 24 26
* /#包括LT&;&iostream.h时GT;诠释的main()
{
    INT ARR [2] [5] = {0}; //这实际上一切都初始化为0。
    改编[1] [] = {1,8,12,20,25}; // 11号线
    ARR [2] [] = {5,9,13,24,26};
    返回0;
}


  

记者:\\ CPP \\网> bcc32.exe Grid.cpp


  
  

用Borland C ++ 5.5.1为Win32版权所有(C)1993年,2000 Borland公司


  
  

Grid.cpp:


  
  

错误E2188 Grid.cpp 11:main函数前pression语法()


  
  

错误E2188 Grid.cpp 12:main函数前pression语法()


  
  

警告W8004 Grid.cpp 14:'ARR'被分配了从未在函数中使用的值
  离子的main()


  
  

* 2的错误在编译*


请帮忙,什么是初始化与我的设定值的二维数组的正确途径。


解决方案

这样的:

  INT的main()
{
    INT ARR [2] [5] =
    {
        {} 1,8,12,20,25,
        {} 5,9,13,24,26
    };
}

这应该由你的C ++教材涵盖:?哪一个是您使用

总之,更好,可以考虑使用的std ::矢量或一些现成的矩阵类如从升压。

I wanted to declare a 2D array and assign values to it, without running a for loop.

I thought I could used the following idea

int array[5] = {1,2,3,4,5};

Which works fine to initialize the 2D array as well. But apparently my compiler doesn't like this.

/*
 1   8  12  20  25
 5   9  13  24  26
*/

#include <iostream.h>

int main()
{
    int arr[2][5] = {0};   // This actually initializes everything to 0.
    arr [1] [] = {1,8,12,20,25}; // Line 11
    arr [2] [] = {5,9,13,24,26};
    return 0;
}

J:\CPP\Grid>bcc32.exe Grid.cpp

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland

Grid.cpp:

Error E2188 Grid.cpp 11: Expression syntax in function main()

Error E2188 Grid.cpp 12: Expression syntax in function main()

Warning W8004 Grid.cpp 14: 'arr' is assigned a value that is never used in funct ion main()

* 2 errors in Compile *

Please help as to what is the right way to initialize the 2d array with my set of values.

解决方案

Like this:

int main()
{
    int arr[2][5] =
    {
        {1,8,12,20,25},
        {5,9,13,24,26}
    };
}

This should be covered by your C++ textbook: which one are you using?

Anyway, better, consider using std::vector or some ready-made matrix class e.g. from Boost.

这篇关于二维数组值C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆