C中二维数组的赋值问题

晓枫gis 2013-07-12 09:04:14



// 两个二维数组a、b,分别是10*10和4*4,并对两个数组进行了赋值
int **a,**b;
a=new int*[10];b=new int*[4];
for(int i=0;i<10;i++)a[i]=new int[10];
for(int i=0;i<4;i++)b[i]=new int[4];

for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
a[i][j]=i+j;

for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
b[i][j]=i+j;

// 现在想把4*4数组b的值来更新10*10数组a中的值,例如:数组a的原始值为:
// 0 1 2 3 4 5 6 7 8 9
// 1 2 3 4 5 6 7 8 9 10
// 2 3 4 5 6 7 8 9 10 11
// 3 4 5 6 7 8 9 10 11 12
// . . . . . . . . . . .
// 9 10 11 12 13 14 15 16 17 18


//希望结果成为:
// 0 1 2 0 1 2 3 7 8 9
// 1 2 3 1 2 3 4 8 9 10
// 2 3 4 2 3 4 5 9 10 11
// 3 4 5 3 4 5 6 11 12 13
// . . . . . . . . . . .
// 9 10 11 12 13 14 15 16 17 18



除了使用逐个赋值外,还有没有快捷的方法,比如通过使用指针来修改地址从而批量的修改数组a,想了挺久的一直没想出来,希望大家帮帮我!
...全文
330 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
晓枫gis 2013-11-15
  • 打赏
  • 举报
回复
抱歉,才看到,可能是我没说清楚,用内存复制的办法可以做到!将小数组打包,再赋给大数组指定的位置
nextseconds 2013-07-18
  • 打赏
  • 举报
回复
这个由于,你每个值都不一样,没法批量设置的啊,若想快速,建议仿照4#的方法
一根烂笔头 2013-07-18
  • 打赏
  • 举报
回复
怎么啦?嫌跑的慢那? OpenMP搞起!!! GPU家伙先进的话(如果有的话) OpenCL走起!!! 对于批量处理,估计是没有,毕竟你的值都不一样! 每个变量相当于一个人,人人都在自己的屋子里!而且只能敲门挨个通知,不能使用大喇叭!即使值都一样,你也要逐个访问逐个通知!
AnYidan 2013-07-12
  • 打赏
  • 举报
回复
什么使“快捷的方法” 1。 运算速度不够快?--google 数组初始化 2。 循环赋值麻烦?--如果是没有规律的数值,你必须一个一个写
www_adintr_com 2013-07-12
  • 打赏
  • 举报
回复
引用 4 楼 zhcosin 的回复:
boost::assign 示例代码:

#include <boost/assign/std/vector.hpp> // for 'operator+=()' 
using namespace std;
using namespace boost::assign; // bring 'operator+=()' into scope

{
    vector<int> values;  
    values += 1,2,3,4,5,6,7,8,9; // insert values at the end of the container
}
这是新的 C++ 标准支持的特性吗? 按照以前的语法, "1,2,3,4,5,6,7,8,9" 整个是一个逗号表达式, 返回的结果是最后一个值 9
zhcosin 2013-07-12
  • 打赏
  • 举报
回复
boost::assign 示例代码:

#include <boost/assign/std/vector.hpp> // for 'operator+=()' 
using namespace std;
using namespace boost::assign; // bring 'operator+=()' into scope

{
    vector<int> values;  
    values += 1,2,3,4,5,6,7,8,9; // insert values at the end of the container
}
zhcosin 2013-07-12
  • 打赏
  • 举报
回复
使用标准容器 vector,然后使用 boost::assign 来赋值,简单快捷。
Fi3_h 2013-07-12
  • 打赏
  • 举报
回复
静等好的idea……
www_adintr_com 2013-07-12
  • 打赏
  • 举报
回复
每一个的值都不一样, 怎么批量...

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧