是否可以在C++中交换任意大小的数组

Is it possible to swap arrays of arbitrary size in C++?

本文关键字:任意大 数组 交换 C++ 是否      更新时间:2023-10-16

我想知道是否可以交换两个不同大小的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;您看到的是它的视觉表示(因为代码将其打印为字符)。