简体   繁体   English

如何使用C ++引用交换两个C样式字符串?

[英]How to swap two C style strings using C++ references?

Below code gives me a compilation error, but I don't understand what I am doing wrong. 下面的代码给了我一个编译错误,但是我不明白我在做什么错。 Sorry about asking such a silly question. 很抱歉问这样一个愚蠢的问题。

$ cat swapcstrings.cc
#include <iostream>

void swap(char*& c, char*& d) {
    char* temp = c;
    c = d;
    d = temp;
}

int main() {
    char c[] = "abcdef";
    char d[] = "ghijkl";
    std::cout << "[" << c << "," << d << "]\n";
    swap(c, d);
    std::cout << "[" << c << "," << d << "]\n";
}
$ g++ swapcstrings.cc
swapcstrings.cc: In function ‘int main()’:
swapcstrings.cc:13: error: invalid initialization of non-const reference of type ‘char*&’ from a temporary of type ‘char*’
swapcstrings.cc:3: error: in passing argument 1 of ‘void swap(char*&, char*&)’
$

Arrays cannot be modified, and they merely decay to temporary pointers, they are not really pointers and cannot be swapped. 数组不能被修改,它们只能衰减为临时指针,它们并不是真正的指针,也不能被交换。 The address of an array cannot be changed, and the compiler errors when you try to bind the temporary pointer you got from the array to a non- const reference, which is against the rules of the language. 数组的地址无法更改,并且当您尝试将从数组中获得的临时指针绑定到非const引用时,编译器会出错,这违反了语言规则。

Declare the arrays, then swap two pointers to them. 声明数组,然后交换两个指向它们的指针

char a[] = "abcdef";
char b[] = "defghi";

char* aptr = a, *bptr = b;

std::cout << "[" << aptr << "," << bptr << "]\n";
swap(aptr, bptr);
std::cout << "[" << aptr << "," << bptr << "]\n";

Or if you can change the prototype of the function, use const char* in the first place: 或者,如果您可以更改函数的原型,请首先使用const char*

void swap(const char*& c, const char*& d) {
    const char* temp = c;
    c = d;
    d = temp;
}

const char* c = "abcdef", // These must be const char* because the arrays are
          * d = "ghijkl"; // const char[N]

std::cout << "[" << c << "," << d << "]\n";
swap(c, d);
std::cout << "[" << c << "," << d << "]\n";

c and d are arrays. cd是数组。 They will get autoconverted to pointers where pointers are required. 它们将自动转换为需要指针的指针。 This is the “temporary” your compiler output talks about. 这是编译器输出所谈论的“临时”。 Think about it like this: 像这样思考:

char c[] = "abcdef", d[] = "ghijkl";
char *cp = (char*)c, *dp = (char*)d;
swap(cp, dp);

The above would compile, but only swap cp and dp , not the original c and d . 上面的代码可以编译,但只能交换cpdp ,而不交换原始的cd As the above code gives names to these pointers, you can now have references to them as well. 由于上面的代码为这些指针指定了名称,因此您现在也可以对其进行引用。 But in your original code, the temporaries had no names, and any modification of them would be an indication of a likely error. 但是在您的原始代码中,临时成员没有名称,对它们的任何修改都可能表示错误。 So the compiler won't let you do that, and will complain instead. 因此,编译器不会让您这样做,而是会抱怨。

If you want to exchange C-style strings in their arrays, you'll have to do so one character at a time: 如果要交换其数组中的C样式字符串,则必须一次只交换一个字符:

template<size_t n> void swap(char (&a)[n], char (&b)[n]) {
  for (size_t i = 0; i != n; ++i)
    std::swap(a[i], b[i]);
}

This template ensures that both arguments are arrays of the same length. 此模板确保两个参数都是相同长度的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM