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

问题描述

您好,我尝试使用的std ::复制()函数来复制一个二维数组。我在想,如果有可能做到这一点,像这样!我不断收到一个段错误,但数组被正确地复制。我试过减了一些,并加入了一些到结束的情况下的复制功能,但没有成功。

  const int的列= 3;
    const int的列= 3;
    INT敏[行] [列] = {{1,2,3},{4,5,6},{7,8,9}};
    INT favint [行] [列];
    性病::复制(敏,敏+行*列,favint);

很明显,敏行+ *列是不正确的,事实证明,这个值对应于整个行,使得敏行+ *列= 1意味着它将复制整个第一行。如果敏行+ *列= 2会复制前两行等有人能解释一下作业本给我吗?


解决方案

 的std ::复制(敏,敏+行*列,favint);

应该是:

 的std ::复制(安培;敏[0] [0],&安培;敏[0] [0] +行*列,和放大器; favint [0] [0] );

原型的std ::复制

 模板<类InputIt,类OutputIt>
OutputIt副本(InputIt第一,InputIt最后OutputIt d_first);

请注意,指针数组元素可以作为包装器的迭代器。

Hello I'm trying to use the std::copy() function to copy a two dimensional array. I was wondering if it's possible to do it like so! I keep getting a "Segmentation Fault" but the array is copied correctly. I've tried subtracting a few and adding a few to the end case for the copy function, but with no success.

    const int rows = 3;
    const int columns = 3;
    int myint[rows][columns]={{1,2,3},{4,5,6},{7,8,9}};
    int favint[rows][columns];
    std::copy(myint, myint+rows*columns,favint);

It's obvious that "myint+rows*columns" is incorrect, and it turns out that this value corresponds to entire rows such that "myint+rows*columns=1" means it will copy the entire first row. if "myint+rows*columns=2" it copies the first two rows etc. Can someone explain the operation of this for me?

解决方案
std::copy(myint, myint+rows*columns,favint);

should be:

std::copy(&myint[0][0], &myint[0][0]+rows*columns,&favint[0][0]);

prototype of std::copy:

template< class InputIt, class OutputIt >
OutputIt copy( InputIt first, InputIt last, OutputIt d_first );

Notice that pointer to array element could be wrapper as an iterator.

这篇关于性病::复制二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:19