c++将两个int数组连接到一个更大的数组中

时间:2021-11-30 12:21:15

Is there a way to take two int arrays in C++

是否有办法在c++中使用两个int数组?

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

and then combine them into one larger array that contains both arrays' values?

然后把它们合并成一个包含两个数组值的更大的数组?

6 个解决方案

#1


27  

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

Just suggestion, vector will do better as a dynamic array rather than pointer

只是建议,向量作为一个动态数组而不是指针会做得更好。

#2


8  

If you're using arrays, you need to allocate a new array large enough to store all of the values, then copy the values into the arrays. This would require knowing the array sizes, etc.

如果使用数组,则需要分配一个足够大的新数组来存储所有的值,然后将这些值复制到数组中。这需要知道数组的大小等等。

If you use std::vector instead of arrays (which has other benefits), this becomes simpler:

如果您使用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());

#3


4  

Another alternative is to use expression templates and pretend the two are concatenated (lazy evaluation). Some links (you can do additional googling):

另一种替代方法是使用表达式模板,并假装两者是串联的(延迟评估)。一些链接(你可以做额外的谷歌搜索):

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

http://www10.informatik.uni-erlangen.de/ ~弗洛姆/弗洛姆/研讨会/ http://www.altdevblogaday.com/2012/01/23/abusing-c-with-expression-templates/ http://aszt.inf.elte.hu/德牧/ halado_cpp / ch06s06.html

If you are looking for ease of use, try:

如果你正在寻找易用性,试试:

#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;
}

#4


1  

Here is the solution for the same-

这是相同的解

#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;
}

Hope it helps.

希望它可以帮助。

#5


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));
    }

}

This might help you along, it doesn't require vectors. I had a similar problem in my programming class. I hope this helps, it was required that I used pointer arithmetic. This assumes that array1 and array2 are initialized dynamically to the size "aSize"

这可能对你有帮助,它不需要向量。我在编程课上遇到过类似的问题。我希望这能有所帮助,我需要使用指针算法。这假定array1和array2被动态初始化为“aSize”大小

#6


0  

Given int * arr1 and int * arr2, this program Concatenates in int * arr3 the elements of the both arrays. Unfortunately, in C++ you need to know the sizes of each arrays you want to copy. But this is no impediment to choose how many elements you want to copy from arr1 and how many from arr2.

给定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]<<", ";
}

}

#1


27  

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

Just suggestion, vector will do better as a dynamic array rather than pointer

只是建议,向量作为一个动态数组而不是指针会做得更好。

#2


8  

If you're using arrays, you need to allocate a new array large enough to store all of the values, then copy the values into the arrays. This would require knowing the array sizes, etc.

如果使用数组,则需要分配一个足够大的新数组来存储所有的值,然后将这些值复制到数组中。这需要知道数组的大小等等。

If you use std::vector instead of arrays (which has other benefits), this becomes simpler:

如果您使用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());

#3


4  

Another alternative is to use expression templates and pretend the two are concatenated (lazy evaluation). Some links (you can do additional googling):

另一种替代方法是使用表达式模板,并假装两者是串联的(延迟评估)。一些链接(你可以做额外的谷歌搜索):

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

http://www10.informatik.uni-erlangen.de/ ~弗洛姆/弗洛姆/研讨会/ http://www.altdevblogaday.com/2012/01/23/abusing-c-with-expression-templates/ http://aszt.inf.elte.hu/德牧/ halado_cpp / ch06s06.html

If you are looking for ease of use, try:

如果你正在寻找易用性,试试:

#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;
}

#4


1  

Here is the solution for the same-

这是相同的解

#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;
}

Hope it helps.

希望它可以帮助。

#5


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));
    }

}

This might help you along, it doesn't require vectors. I had a similar problem in my programming class. I hope this helps, it was required that I used pointer arithmetic. This assumes that array1 and array2 are initialized dynamically to the size "aSize"

这可能对你有帮助,它不需要向量。我在编程课上遇到过类似的问题。我希望这能有所帮助,我需要使用指针算法。这假定array1和array2被动态初始化为“aSize”大小

#6


0  

Given int * arr1 and int * arr2, this program Concatenates in int * arr3 the elements of the both arrays. Unfortunately, in C++ you need to know the sizes of each arrays you want to copy. But this is no impediment to choose how many elements you want to copy from arr1 and how many from arr2.

给定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]<<", ";
}

}