1.俩整数,不使用中间变量交换其值:

int& intswap(int& a, int& b)
{
b ^= a;
a ^= b;
b ^= a;
return b;
}

2.C++中俩string交换字符串

string & strswap(string & a, string & b)
{
a=a.append(b);
b= a.substr(,a.length()-b.length());
a=a.substr(b.length(),a.length());
return b;
}

3.char*字符串交换值//不使用动态内存,执行1000w次耗时2s,使用动态内存耗时3s。

//不使用动态内存:
char* cswap(char* a, char* b)
{
int i = ;
int alen = strlen(a),blen= strlen(b);
strcat(a, b);
for (;i < alen;i++)
{
b[i] = a[i];
}
b[i] = '\0';
for (i = ;i < blen;i++)
{
a[i] = a[alen + i];
}
a[i] = '\0';
return a;
}
// 使用动态内存
int charswap(char *a, char *b)
{
char* temp=NULL;
int n = strlen(a) > strlen(b) ? (strlen(a)+1) : (strlen(b)+1);
temp = (char*)malloc(n * sizeof(char));
strcpy(temp, a);
strcpy(a, b);
strcpy(b, temp);
free(temp);
return ;
}

函数调用:

 #include<iostream>
#include<string.h>
using namespace std;
int main(void)
{
clock_t start, finish;
char a[] ="hellohellohellohellohellohellohellohellohellohello";
char b[] = "hihihihihihihihihihihi";
int alen = strlen(a);
int blen = strlen(b);
start = clock();
for (int i = ;i < ;++i)
{
cswap(a, b);
//charswap(a, b);
}
finish = clock();
double t = (finish - start)/CLOCKS_PER_SEC ;
cout << "costs: " << t << "s" << endl;
cout << "a= " << a << endl;
cout << "b= " << b << endl;
return ;
}

执行结果:

int型、char*、string、的swap算法的更多相关文章

  1. C字符串和C++中string的区别 &&&&C++中int型与string型互相转换

    在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别:   C字符串 string对象(C++) 所需的头文件名称 ...

  2. c++编写递归函数char *itostr (int n,char *string),该函数将整数n转换为十进制表示的字符串。

    #include<iostream> #include<stdio.h> using namespace std; ; char *itostr (int n,char *St ...

  3. C字符串和C++中string的区别 &amp;&amp;&amp;&amp;C++中int型与string型互相转换

    在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别:   C字符串 string对象(C++) 所需的头文件名称 ...

  4. C++中int型与string型互相转换(转)

    http://greatverve.cnblogs.com/archive/2012/10/24/cpp-int-string.html 本以为这么多年C#经验,学个C++没多难,现在发现错了.C++ ...

  5. c语言,string库函数itoa实现:将int转换为char*

    原型:char  *itoa(int   value,char   *string)        功能:将整数value转换成字符串存入string,默认为十进制;      说明:返回指向转换后的 ...

  6. Arduino中数据类型转换 int转换为char 亲测好使,itoa()函数

    由于博主最近在做一个项目,需要采集不同传感器的数据,包括float型的HCHO,以及int型的PM2.5数据.但是最终向服务器上传的数据都得转换为char型才能发送,这是借鉴了一个github上面的实 ...

  7. C++ int与char[]的相互转换

    C++ int与char[]的相互转换 一.itoa函数与atio函数①把int类型数字转成char类型,可以使用itoa函数. itoa函数原型: char*itoa(int value,char* ...

  8. Java 中的 int 型转为 long 型

    先将 int 型转为 String 型,然后再将 String 转为 long 型,如下图: public class TestIntToLong { public static void main( ...

  9. 所学新知——int、char型转string 类型等

    1. 利用stringstream类 定义头文件#include<sstream> 通过 int a; char b; sstream ss,ss1; ss<<a; ss1&l ...

随机推荐

  1. opencv入门教程三

    本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/20537737 作者:毛星云(浅墨)  ...

  2. vue双向绑定原理(简单实现原理附demo)

    先上效果图 简单的实现数据的双向绑定首先来了解一个东西:Object.defineProperty() https://developer.mozilla.org/zh-CN/docs/Web/Jav ...

  3. jetty 启动时出现的问题

    启动时出现: 1.  NoClassDefFoundError: javax/xml/registry/infomodel/User 是需要导入jaxr-api包, <dependency> ...

  4. jmeter的日常特殊参数化

    1.map转译符号:   如果///Mobile///:///18888888888///   需要再参数化请这样做,////Mobile////://///${Mobile}/////   2.in ...

  5. 006-Spring Boot自动配置-Condition、Conditional、Spring提供的Conditional自动配置

    一.接口Condition.Conditional(原理) 主要提供一下方法 boolean matches(ConditionContext context, AnnotatedTypeMetada ...

  6. VMware 虚拟化编程(4) — VDDK 安装

    目录 目录 前文列表 VDDK 安装 VDDK 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化编程(2) - 虚拟磁盘 ...

  7. [转载]Parsing X.509 Certificates with OpenSSL and C

    Parsing X.509 Certificates with OpenSSL and C Zakir Durumeric | October 13, 2013 While OpenSSL has b ...

  8. Mac018--VisualBox & ubuntu 安装

    一.安装虚拟机VMware 参考博客:https://blog.csdn.net/u013142781/article/details/50529030 Step1:下载ubuntu镜像 注:选择Ub ...

  9. 关于Vue的理解以及与React框架的对比

    1.Vue的理解 概念: Vue是一套用于构建用户界面的渐进式框架: Vue的核心库只关注视图层: 是一个数据驱动的MVVM框架: 特性: 确实轻量:体积比较小: 数据绑定简单.方便: 有一些简单的内 ...

  10. Web前端开发 --》 如何实现页面同时在移动端和pc端的兼容问题

    很简单,只需要在html文件中对你引入的css进行一个类似于媒体查询的操作 <!DOCTYPE html> <html lang="en"> <hea ...