是否可以在C++中交换任意大小的数组
Is it possible to swap arrays of arbitrary size in C++?
我想知道是否可以交换两个不同大小的C++数组的内容(不使用任何预定义的C++函数)?我的代码如下:
#include <iostream>
#include <string>
using namespace std;
void swapNames(char a[], char b[])
{
//can be done with one temp; using two for clarity purposes
char* temp = new char[80];
char* temp2 = new char[80];
int x = 0;
while(*(b+x)!=' ')
{
*(temp+x) = *(b+x);
x=x+1;
}
x=0;
while(*(a+x)!=' ')
{
*(temp2+x) = *(a+x);
x=x+1;
}
x=0;
while(*(temp2+x)!=' ')
{
*(b+x) = *(temp2+x);
x=x+1;
}
x=0;
while(*(temp+x)!=' ')
{
*(a+x) = *(temp+x);
x=x+1;
}
}
int main()
{
char person1[] = "James";
char person2[] = "Sarah";
swapNames(person1, person2);
cout << endl << "Swap names..." << endl;
cout << endl << "Person1 is now called " << person1;
cout << "Person2 is now called " << person2 << endl;;
}
我最初的想法是传入对 person1 和 person2 本身的引用,将数据存储在临时变量中,删除分配给它们的内存,并将它们链接到具有交换数据的新创建的数组。我认为这可以避免预定义的内存限制。但是,似乎不允许将引用(&)传递给数组。
如果 person1 和 person2 的大小相同,则上述工作正常。但是,一旦我们有不同大小的名称,我们就会遇到问题。我认为这是因为我们无法更改最初创建 person1 和 person2 时分配的内存块。
另外,是否可以在不预定义大小的情况下在C++中创建新数组?IE 是一种创建临时变量而不限制其大小的方法。
char person1[] = "James";
只是以下各项的简写:
char person1[6] = "James";
您以后不能在 person1 中存储超过 6 个字符。如果你真正想要的是不同长度的字符串,我建议放弃 C 风格的字符串,转而使用std::string
标准库类型:
#include <string>
#include <algorithm>
std::string person1 = "James";
std::string person2 = "Sarah";
swap(person1, person2);
如果你的书在std::string
之前教C风格的字符串,你应该考虑买一本新书。
允许引用数组,只要数组大小是固定的。
有一个简单的答案,而不是您正在考虑的所有复杂事情。只需使用矢量。
vector<char> a;
vector<char> b;
...
a.swap(b);
还有什么比这更容易的呢?
向量也是您问题的答案,"另外,是否可以在不预定义大小的情况下在C++中创建新数组?您可以创建一个矢量,然后稍后调整其大小(这几乎是一回事)。
您应该始终首选容器类而不是原始数组。但是,如果绝对必须使用数组,则当然可以完成交换,而无需动态分配临时数组。使用模板静态确定传递到交换函数的数组的类型和大小。
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
template<typename T, size_t N>
void swap_arrays( T(&left)[N], T(&right)[N] )
{
T temp[N];
// copy left to temp
std::copy( std::begin(left), std::end(left), std::begin(temp) );
// copy right to left
std::copy( std::begin(right), std::end(right), std::begin(left) );
// copy temp to right
std::copy( std::begin(temp), std::end(temp), std::begin(right) );
}
template<typename T, size_t N>
void swap_and_print( T(&left)[N], T(&right)[N] )
{
std::cout << "nBefore swapping: n";
std::cout << " Left:";
std::for_each( std::begin(left), std::end(left),
[]( T const& t ) { std::cout << " " << t; } );
std::cout << "n Right:";
std::for_each( std::begin(right), std::end(right),
[]( T const& t ) { std::cout << " " << t; } );
swap_arrays( left, right );
std::cout << "nAfter swapping: n";
std::cout << " Left:";
std::for_each( std::begin(left), std::end(left),
[]( T const& t ) { std::cout << " " << t; } );
std::cout << "n Right:";
std::for_each( std::begin(right), std::end(right),
[]( T const& t ) { std::cout << " " << t; } );
}
int main()
{
int foo1[] = {1,2,3,4,5};
int bar1[] = {6,7,8,9,10};
swap_and_print( foo1, bar1 );
char foo2[] = "James";
char bar2[] = "Sarah";
swap_and_print( foo2, bar2 );
}
输出:
Before swapping:
Left: 1 2 3 4 5
Right: 6 7 8 9 10
After swapping:
Left: 6 7 8 9 10
Right: 1 2 3 4 5
Before swapping:
Left: J a m e s �
Right: S a r a h �
After swapping:
Left: S a r a h �
Right: J a m e s �
注意:第二个示例末尾的奇怪字符是因为输入是包含终止 null 字符的数组char
;您看到的是它的视觉表示(因为代码将其打印为字符)。
- 使用指针从C++中的数组中获取最大值
- 数组中最大的非重复元素
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 在c++中获取两个大int,并将它们存储在数组中
- 在二维数组中查找最小值和最大值?
- 遍历并行数组以确定C++中的最大数字
- 如何在数组中交换最小和最大的位置?
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 在二维向量或数组中可以存储的最大元素数是多少?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 如何为我的数组选择更大的数据类型?
- 任意大小的 constexpr 数组是否可以用作 switch 语句中的案例?
- 声明后,gcc 的动态大小数组是否与标准数组有效相同?
- N 维数组的大 O 表示法
- 用于查找数组中最大元素的出现次数的代码,给出分段错误
- 设置指向任意维度数组的指针
- 在打字稿中,如何编写一个函数来处理任意维度数组(例如:<number>Array,Array<Array<number>>,...)?
- 清除任意2D数组