有没有办法在 C++ 中获取两个 int 数组
int * arr1;
int * arr2;
//pretend that in the lines below, we fill these two arrays with different
//int values
然后将它们组合成一个更大的数组,包含两个数组的值?
原文由 kjh 发布,翻译遵循 CC BY-SA 4.0 许可协议
有没有办法在 C++ 中获取两个 int 数组
int * arr1;
int * arr2;
//pretend that in the lines below, we fill these two arrays with different
//int values
然后将它们组合成一个更大的数组,包含两个数组的值?
原文由 kjh 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是相同的解决方案-
#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;
}
希望能帮助到你。
原文由 user8208104 发布,翻译遵循 CC BY-SA 3.0 许可协议
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
4 回答2.2k 阅读✓ 已解决
3 回答3.4k 阅读
使用
std::copy
在标题中定义的<algorithm>
。 args 是指向输入第一个元素的指针、指向输入最后一个元素的指针和指向输出第一个元素的指针。 ( https://en.cppreference.com/w/cpp/algorithm/copy )只是建议,
vector
作为动态数组而不是指针会做得更好