Jack's Blog

流淌的心,怎能阻拦,吹来的风,又怎能阻挡。

swap函数的一些扩展(数据结构)

Jacob posted @ Oct 23, 2017 07:35:49 PM in 未分类 with tags C语言 , 1116 阅读
#include <iostream>

using namespace std;
struct A{
    int a;
    int b;
}A1={0};
struct A my_swap(int a1,int b1)
{
    A1.a=b1;
    A1.b=a1;
    return A1;
}

int main()
{
    int a=1,b=2;
    A A2= my_swap(a,b);
    my_swap(a,b);
    printf("%d %d",a,b);
    return 0;
}

用结构体实现了swap

其实最简单的还是用指针来写swap

my_swap(int &a,int &b)
my_swap(int *a,int *b)
/×
两种写法等价

然而直接用

my_swap(int a,int b);
是不能交换函数值的

因为虽然在这个函数虽然在这个函数内部互换了函数值,但是这仅仅局限与函数内部,对于整个main函数的调用并没有使用返回值。

我在想如果用内联函数进行展开是不是可以交换呢。读者们可以亲自试试。
inline int swap(int a,int b);

 

Avatar_small
BSNL Tune 说:
2022年8月09日 19:26

BSNL Personalized Ring Back Tone services, a mobile subscriber greet their callers with their favorite song as best choice, and this new My BSNL Tunes App service is an innovative service help the subscribers to express themselves, access information, entertainment and connect the other by greeting. BSNL Tune Most popular albums are vying for honor, but to find the best with codes or by names it is difficult, but now with this new launch of My BSNL Tunes App, each mobile user now have a chance to listen and activate their favorite songs of any language to set as BSNL caller tune on their mobiles.

Avatar_small
Emma 说:
2023年1月23日 20:12

This swap function is quite an impressive extension! It provides an effective and efficient way to rearrange data within various data structures. It is sure to be a great real estate services Smithfield asset for any programmer, making the process of organizing data a much simpler task. Thanks to this expansion, it will be much easier to modify and store data!


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter