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. Distinctive Image Features from Scale-Invariant Keypoints(SIFT) 基于尺度不变关键点的特征描述子——2004年

    Abstract摘要本文提出了一种从图像中提取特征不变性的方法,该方法可用于在对象或场景的不同视图之间进行可靠的匹配(适用场景和任务).这些特征对图像的尺度和旋转不变性,并且在很大范围的仿射失真.3d ...

  2. 解决Acunetix 12中文汉化的方法

    最近下载一款测试软件acunetix,苦于满屏英文的苦恼,看不懂,于是乎就问度娘,结果度娘就是给中文破解包: 我是12版的,网上提供的都是11版的,没法用.怎么办呢?还好我是做测试的,平时做兼容性测试 ...

  3. [CF1054C]Candies Distribution

    题目:Candies Distribution 传送门:http://codeforces.com/problemset/problem/1054/C 分析: 方法一: 1)类似拓扑排序的做法. 2) ...

  4. 基于vue模块化开发后台系统——构建项目

    文章目录如下:项目效果预览地址项目开源代码基于vue模块化开发后台系统--准备工作基于vue模块化开发后台系统--构建项目基于vue模块化开发后台系统--权限控制 前言 在熟悉上一篇说到准备工具之后, ...

  5. db2查看当前用户模式及当前用户的权限

    1.连接数据库:db2 connect to appdb 2.查询当前用户模式:select current schema from sysibm.sysdummy1 或 select current ...

  6. 认识Dow(下)

    节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...

  7. VMware 接入 Openstack — 使用 Openstack 创建 vCenter 虚拟机

    目录 目录 软件环境 前言 Openstack 接口驱动 使用 KVM 在 Compute Node 上创建虚拟机的流程 使用 VCDirver 在 vCenter 上创建虚拟机的流程 配置 vCen ...

  8. Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray

    714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...

  9. String 和 new String()的区别

    String 和 new String()的区别 For Example String str1 = "ABC" String str2 = new String("AB ...

  10. Java相关面试题总结+答案(九)

    [MySQL] 164. 数据库的三范式是什么? 第一范式:强调的是列的原子性,即数据库表的每一列都是不可分割的原子数据项. 第二范式:属性完全依赖于主键(满足第一范式的前提下),即任意一个字段只依赖 ...