C++ C++;:按ref交换字符

C++ C++;:按ref交换字符,c++,C++,我已经为两个数字实现了swap via函数,效果很好。但是,我现在尝试交换两个字符串,但我收到的名称顺序相同。有谁知道我哪里出了问题,或者我能做些什么让名字改变位置?下面是一个代码示例: #include "stdafx.h" #include <cstring> #include <iostream> #include <string> using namespace std; void swapages (int &age1, int &

我已经为两个数字实现了swap via函数,效果很好。但是,我现在尝试交换两个字符串,但我收到的名称顺序相同。有谁知道我哪里出了问题,或者我能做些什么让名字改变位置?下面是一个代码示例:

#include "stdafx.h"
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

void swapages (int &age1, int &age2);           
void swapname(char *person1, char *person2);

int _tmain(int argc, _TCHAR*argv[])
{
    char person1[] = "Alex";
    char person2[] = "Toby";
    int age1 = 22;
    int age2 = 27;


    cout << endl << "Person1 is called " << person1;
    cout << " and is " << age1 << " years old." << endl;
    cout << "Person2 is called " << person2;
    cout << " and is " << age2 << " years old." << endl;

    swapname(person1,person2);
    swapages(age1,age2);
    cout << endl << "Swap names..." << endl;
    cout << endl << "Person1 is now called " << person1;
    cout << " and is " << age1 << " years old." << endl;
    cout << "Person2 is now called " << person2;
    cout << " and is " << age2 << " years old." << endl;

    system("pause");
    return 0;
}

void swapages(int &age1, int &age2)
{
    int tmp = age2;
    age2 = age1;
    age1 = tmp;
}

void swapname(char *person1, char *person2)
{
    char* temp = person2;
    person2 = person1;
    person1 = temp;
}
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
无效交换页(国际和第1页、国际和第2页);
void swapname(char*person1,char*person2);
int _tmain(int argc,_TCHAR*argv[]
{
charperson1[]=“Alex”;
charperson2[]=“托比”;
国际年龄1=22;
国际年龄2=27;

cout问题在于,当您需要交换字符串时,您试图交换作为函数局部变量的指针。因此,您需要将一个字符串复制到另一个字符串中。此外,字符串可以具有不同的长度,因此不可能进行精确交换。如果函数的参数为相同大小的数组(对数组的引用)

void swap_name( char ( &lhs )[5], char ( &rhs )[5] )
{
    char tmp[5];

    std::strcpy( tmp, lhs );
    std::strcpy( lhs, rhs );
    std::strcpy( rhs, tmp );
}

问题是,当您需要交换字符串时,您试图交换作为函数局部变量的指针。因此,您需要将一个字符串复制到另一个字符串中。此外,字符串可以具有不同的长度,因此不可能进行精确交换。如果函数的参数为数组,则可以顺利完成交换(对数组的引用)具有相同的大小。例如

void swap_name( char ( &lhs )[5], char ( &rhs )[5] )
{
    char tmp[5];

    std::strcpy( tmp, lhs );
    std::strcpy( lhs, rhs );
    std::strcpy( rhs, tmp );
}

你需要做一些小的改变,让它按照你想要的方式工作

void swapname(char **person1, char **person2);
.
.
char *person1 = "Alex";
char *person2 = "Toby";
.
.
swapname(&person1, &person2);
.
.


你需要做一些小的改变,让它按照你想要的方式工作

void swapname(char **person1, char **person2);
.
.
char *person1 = "Alex";
char *person2 = "Toby";
.
.
swapname(&person1, &person2);
.
.


你已经把它标记为C++,你已经包括了<代码> <代码>头,为什么不使用<代码> STD:String < /C>而不是所有的指针和数组?< /P>
void swapname(string &person1, string &person2)
{
    string temp(person2);
    person2 = person1;
    person1 = temp;
}

int _tmain(int argc, _TCHAR*argv[])
{
    string person1 = "Alex";
    string person2 = "Toby";

    swapname(person1, person2);
}

你已经把它标记为C++,你已经包括了<代码> <代码>头,为什么不使用<代码> STD:String < /C>而不是所有的指针和数组?< /P>
void swapname(string &person1, string &person2)
{
    string temp(person2);
    person2 = person1;
    person1 = temp;
}

int _tmain(int argc, _TCHAR*argv[])
{
    string person1 = "Alex";
    string person2 = "Toby";

    swapname(person1, person2);
}

在您的代码中,person1和person2被定义为2个字符数组而不是字符指针变量,您不能交换它们,如果您将这2个数组传递到以2个指针为参数的swapname函数中,它甚至不应该编译。

在您的代码中,person1和person2被定义为2个字符数组而不是字符指针变量,您不能交换如果您将这两个数组传递到swapname函数中,该函数使用两个指针作为参数,它甚至不应该编译。

好吧,正如您所说的“按引用”。但是我没有看到任何引用。为了能够修改,您必须逐指针(
char**
,指针到指针)或按引用传递
char*
指针(
char*&
,正如您在
swapages()
中使用
int
s所做的那样)。顺便说一句,请忘记它并使用它。如果您使用标准库,您的代码会更简单(并且实际工作)。正如您所说的,“按引用”。但我没有看到任何引用。要进行修改,您必须逐个指针传递
char*
char**
,指针指向指针)或通过引用(
char*&
,就像您在
swapages()
中使用
int
s一样)。顺便说一句,所以请忘记它并使用它。您的代码会更简单(并且实际工作)如果你使用了标准库,这是C代码,不是C++。你至少可以使用<代码> char *和<代码>参数。这是C代码,而不是C++。你至少可以使用<代码> char *和<代码>参数。