C++将两个整数数组连接成一个更大的数组

25

有没有一种方法可以在 C++ 中接收两个 int 数组?

int * arr1;
int * arr2;
//pretend that in the lines below, we fill these two arrays with different
//int values

如何将两个数组的值合并成一个更大的数组?


这些不是数组 - oromoiluig
这些不是数组 - undefined
6个回答

36

使用定义在头文件 <algorithm> 中的 std::copy。参数是指向输入第一个元素的指针、指向输入最后一个元素的下一个指针和指向输出第一个元素的指针。 (https://en.cppreference.com/w/cpp/algorithm/copy)

int * result = new int[size1 + size2];
std::copy(arr1, arr1 + size1, result);
std::copy(arr2, arr2 + size2, result + size1);

仅仅是一个建议,vector作为动态数组比指针更好用。


resultstd::vector 类型吗? - LAL
7
可能解释一下这个copy函数是什么? - juanchopanza
我同意@juanchopanza的观点,但如果texasbruce没有反应,我会说这是std::copy - gsamaras
3
由于最佳答案未描述 std::copy 的作用,这里提供一份文档链接以供参考:https://zh.cppreference.com/w/cpp/algorithm/copy。 - Furkan Toprak

16
如果你使用数组,你需要分配一个足够大的新数组以存储所有值,然后将值复制到数组中。这需要知道数组大小等信息。
如果你使用std::vector而不是数组(它还有其他好处),则这变得更简单了:
std::vector<int> results;
results.reserve(arr1.size() + arr2.size());
results.insert(results.end(), arr1.begin(), arr1.end());
results.insert(results.end(), arr2.begin(), arr2.end());

在你编写的第一个results.insert语句中,为什么要将插入位置设置为results.end()?也许我没有理解这个函数的工作方式。 - kjh
1
@kjh 在STL中,results.end()初始时是指向容器的起始位置,但是当你进行追加操作时,它会将值推送到向量的“末尾”。 - Reed Copsey

4

另一种选择是使用表达式模板,并假装两个表达式被连接起来(惰性求值)。以下是一些链接(您可以进行额外的搜索):

http://www10.informatik.uni-erlangen.de/~pflaum/pflaum/ProSeminar/ http://www.altdevblogaday.com/2012/01/23/abusing-c-with-expression-templates/ http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s06.html

如果您正在寻找易用性,请尝试:
#include <iostream>
#include <string>

int main()
{
  int arr1[] = {1, 2, 3};
  int arr2[] = {3, 4, 6};

  std::basic_string<int> s1(arr1, 3);
  std::basic_string<int> s2(arr2, 3);

  std::basic_string<int> concat(s1 + s2);

  for (std::basic_string<int>::const_iterator i(concat.begin());
    i != concat.end();
    ++i)
  {
    std::cout << *i << " ";
  }

  std::cout << std::endl;

  return 0;
}

2
这里是相同问题的解决方案 -
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void Concatenate(char *s1,char *s2)
{
    char s[200];
    int len1=strlen(s1);
    int len2=strlen(s2);
    int j;
    ///Define k to store the values on Kth address Kstart from 0 to len1+len2;
    int k=0;

        for(j=0;j<len1;j++)
        {
            s[k]=s1[j];
            k++;

        }
        for(j=0;j<len2;j++)
        {
            s[k]=s2[j];
            k++;
        }
        ///To place a null at the last of the concatenated string to prevent Printing Garbage value;
        s[k]='\n';

    cout<<s;
}

int main()
{
    char s1[100];
    char s2[100];
    cin.getline(s1,100);
    cin.getline(s2,100);
    Concatenate(s1,s2);

    return 0;
}

希望这能有所帮助。

0
for (int i = 0; i< arraySize * 2; i++)
{
    if (i < aSize)
    {
        *(array3 + i) = *(array1 + i);
    }
    else if (i >= arraySize)
    {
        *(array3 + i) = *(array2 + (i - arraySize));
    }

}

这可能会对你有所帮助,它不需要向量。我在编程课上遇到了类似的问题。希望这可以帮到你,我必须使用指针算术来解决这个问题。这假设array1和array2动态初始化为大小"aSize"。


0
给定 int * arr1 和 int * arr2,此程序将两个数组的元素连接到 int * arr3 中。不幸的是,在 C++ 中,您需要知道要复制的每个数组的大小。但这并不妨碍您选择从 arr1 复制多少个元素和从 arr2 复制多少个元素。
#include <iostream>
using namespace std;

int main(){
int temp[] = {1,2,3,4};
int temp2[] = {33,55,22};
int * arr1, * arr2, *arr3;
int size1(4), size2(3); //size1 and size2 is how many elements you 
//want to copy from the first and second array. In our case all.
//arr1 = new int[size1]; // optional
//arr2 = new int[size2];

arr1=temp;
arr2=temp2;

arr3 = new int; 
//or if you know the size: arr3 = new int[size1+size2];

for(int i=0; i<size1+size2; i++){
    if (i<size1)
        arr3[i]=arr1[i];
    else
        arr3[i] = arr2[i-size1];
}
cout<<endl;
for (int i=0; i<size1+size2; i++) {
    cout<<arr3[i]<<", ";
}

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接