如何使用引用运算符'&'连接2个数组? [英] How can I concatenate 2 arrays by using reference operator '&'?

查看:92
本文介绍了如何使用引用运算符'&'连接2个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我想串联2个数组,如下所示:

Hi!
I want to concatenate 2 array as follows:

double little_arrayA[] = {1, 2, 3, 4, 5};
double little_arrayB[] = {6, 7, 8, 9, 10};

double *BIG_array = new double [2*5];

//START "BLOCK 1" (concatenate by using reference operator '&')
&BIG_array[0] = little_arrayA;
&BIG_array[5] = little_arrayB;
//END "BLOCK 1"



显然,我无法编译此代码,但我想了解如何以最有效的方式在块1"中实现该功能.为了提高效率,似乎不适合使用循环,但是我想使用不起作用的引用运算符&".
你有什么主意吗?

谢谢!

塞尔吉奥

编辑:
感谢您收到的所有信息!希望做些澄清,下面提出一些需要考虑的约束:
1)我必须将2个double数组连接到一个double数组中.
2)效率"一词的意思是在尽可能短的时间内执行该功能.
3)每个数组都很大/非常大.
4)我在编译时不知道数组大小.



Obviously I can not compile this code but I would like to understand how to implement the function in "BLOCK 1" in the most efficient way. For efficiency, using cycle seems unsuitable, but rather I wanted to use not working reference operator ''&''.
Do you have any idea?

Thank you!

Sergio

EDIT:
I thank you for all the information received! Hoping to do a bit of clarity, I present below some constraints to be considered:
1) I have to concatenate 2 array of double in a single array of double.
2) By the word "efficiency" I mean perform the function in the shortest possible time.
3) Each array is large/very large.
4) I don''t know array size in compile time.

推荐答案

//EDIT
这个怎么样 ? :):
// EDIT
How about it ? :) :
template <typename T>
class Concatenation {
  T* m_pDataA;
  int m_iCountA;
  
  T* m_pDataB;
  int m_iCountB;

public:
  Concatenation(T* pDataA, int iCountA, T* pDataB, int iCountB)
  : m_pDataA(pDataA), m_iCountA(iCountA)
  , m_pDataB(pDataB), m_iCountB(iCountB)
  {
    // some assertions... :)
  }

  T* GetAt(int iIdx)
  {
    // some assertions... :)

    T* pResult(NULL);

    if (0 <= iIdx && iIdx < (m_iCountA + m_iCountB)) {
      if (iIdx < m_iCountA) {
        pResult = &m_pDataA[iIdx];
      } else {
        pResult = &m_pDataB[iIdx - m_iCountA];
      }
    }

    return pResult;
  }
};

void Test(int iCountA, int iCountB)
{
  // some assertions... :)

  int* pIntA(new int[iCountA]);
  int* pIntB(new int[iCountB]);

  Concatenation<int> cCat(pIntA, iCountA, pIntB, iCountB);
  *cCat.GetAt((iCountA + iCountB) /2) = 123;
  ASSERT(123 == *cCat.GetAt((iCountA + iCountB) /2));

  delete[] pIntA;
  delete[] pIntB; 
}


如果您对C ++解决方案感兴趣,请访问^ ].它将使您摆脱所有循环的苦难.

如果要使用C版本,请查看pwassers解决方案.
If you''re interested in a C++ solution, have a look at std::copy()[^]. It will get you away from all the looping misery.

If you want a C version, take a look at pwassers solution.


所有这些数组都位于内存中不同且不连续的位置.要有效地将所有内容集中在一处,这是一种选择.

All these arrays are in different and not contiguous locations in memory. To get the contents all in one place efficiently this is one option.

double little_arrayA[] = {1, 2, 3, 4, 5};
double little_arrayB[] = {6, 7, 8, 9, 10};


double *BIG_array = (double *) new char [sizeof(little_arrayA) + sizeof(little_arrayB)];


memcpy(BIG_array, little_arrayA, sizeof(little_arrayA));
memcpy(BIG_array + sizeof(little_arrayA), little_arrayB, sizeof(little_arrayB));


这篇关于如何使用引用运算符'&amp;'连接2个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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