C/C++ 二维数组
使用C语言用到了二维数组
#include <iostream>
#include <stdlib.h>
using namespace std; void print_arr_fun1(int arr[][], int row){
for (int i = ; i < row; ++i){
for (int j = ; j < ; ++j)
cout << arr[i][j] << " ";
cout << endl;
}
} void print_arr_fun2(int *arr, int row, int col){
for (int i = ; i < row; ++i){
for (int j = ; j < col; ++j)
cout << *(arr + i * row + j) << " ";
cout << endl;
}
} void print_arr_fun3(int **arr, int row, int col){
for (int i = ; i < row; ++i){
for (int j = ; j < col; ++j)
cout << arr[i][j] << " ";
cout << endl;
}
} int main(){
const int row = ; //这里是const
const int col = ;
int arr1[row][col];
for (int i = ; i < row; ++i)
for (int j = ; j < col; ++j)
arr1[i][j] = i * col + j; cout << "print_arr_fun1---------------------------" << endl;
print_arr_fun1(arr1, row);
cout << "print_arr_fun2---------------------------" << endl;
print_arr_fun2((int*)arr1, row, col); cout << "print_arr_fun3---------------------------" << endl;
int **arr2 = (int**)malloc(sizeof(int*) * row);
//malloc
for (int i = ; i < row; ++i)
arr2[i] = (int*)malloc(sizeof(int) * col);
for (int i = ; i < row; ++i)
for (int j = ; j < col; ++j)
arr2[i][j] = i * col + j;
print_arr_fun3(arr2, row, col); //free
for (int i = ; i < row; ++i)
free(arr2[i]);
free(arr2); return ;
}
输出:
print_arr_fun1---------------------------
0 1 2
3 4 5
print_arr_fun2---------------------------
0 1 2
2 3 4
print_arr_fun3---------------------------
0 1 2
3 4 5
C/C++ 二维数组的更多相关文章
- PHP 二维数组根据某个字段排序
二维数组根据某个字段排序有两种办法,一种是通过sort自己写代码,一种是直接用array_multisort排序函数 一. 手写arraysort PHP的一维数组排序函数: sort 对数组的值按 ...
- 剑指Offer-【面试题03:二维数组中的查找】
package com.cxz.question3; /* * 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. * 请完成一个函数,输入这样的一个二维数组和 ...
- PHP开发笔记:二维数组根据某一项来进行排序
比如说我们现在有一个二维数组: $arr = array( ‘d' => array(‘id' => 5, ‘name' => 1, ‘age' => 7), ‘b' => ...
- 剑指Offer面试题:2.二维数组中的查找
一.题目:二维数组中的查找 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...
- PHP 根据key 给二维数组分组
我们经常拿到一个二维数组出来,会发现结果和自己想要的有些偏差,可能需要根据二维数组里的某个字段对数组分组.先来看以下数组, Array ( [0] => Array ( [id] => 1 ...
- Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式
递归.二维数组顺时针旋转90°.正则表达式 1. 递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...
- 个人学习记录1:二维数组保存到cookie后再读取
二维数组保存到cookie后再读取 var heartsArray = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0],[0,0, ...
- PHP如何判断一个数组是一维数组或者是二维数组?用什么函数?
如题:如何判断一个数组是一维数组或者是二维数组?用什么函数? 判断数量即可 <?php if (count($array) == count($array, 1)) { echo '是一维数组' ...
- jquery解析php通过ajax传过来的json二维数组对象
ajax获得php传过来的json二维数组对象,jquery解析 php代码: <?php $news = array( '武汉'=>array(1,2,3), '广州'=>arra ...
- char 型二维数组
char FutureFunc[][16] = {"XMA","ZIG","PEAK","PEAKBARS"," ...
随机推荐
- git入门教程,主要命令详解。
准备工作 git clone url / ssh ----------------------------------------------------------------------从git ...
- 初探Runloop(一)
iOS 的最大特点就是运行时. 保证运行时的就是RunLoop 1.什么是RunLoop呢? 从字面理解就是:运行循环 引用下官方文档的介绍: A run loop is an event proce ...
- 解决apache上访问 cgi脚本时总是在网页中显示出脚本的源代码而不是执行结果的问题
apache是支持cgi脚本的,但是需要保证四个条件: 1.放置cgi脚本的文件夹本身需要对apache服务器这个用户(一般默认用户名是www,linux下的用户机制请自行百度)开放x(即可执行)权限 ...
- Hadoop系列之(三):使用Cloudera部署,管理Hadoop集群
1. Cloudera介绍 Hadoop是一个开源项目,Cloudera对Hadoop进行了商业化,简化了安装过程,并对hadoop做了一些封装. 根据使用的需要,Hadoop集群要安装很多的组件,一 ...
- AutoMapper实际项目运用
AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前 ...
- HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)
Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- 依赖注入(DI)和控制反转(IOC)的理解,写的太好了。
学习过spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- MySQL主从复制几个重要的启动选项
MySQL主从复制几个重要的启动选项 (1) log-slave-updates log-slave-updates这个参数用来配置从服务器的更新是否写入二进制日志,这个选项默认是不打开的,但是,如 ...
- 交叉编译strace
下载地址:https://sourceforge.net/projects/strace/ 我下的版本是4.18, 也可以到这里下载. 下面是交叉编译用的脚本: #!/bin/bash ../conf ...
- Subversion detected an unsupported working copy version
关于这个错误:Subversion detected an unsupported working copy version while checking the status of 'XXXX'. ...