java vector reverse,倒置函数reverse的用法

倒置字符串函数reverse:用于倒置字符串s中的各个字符的位置,如原来字符串中如果初始值为123456,则通过reverse函数可将其倒置为654321,程序如下:

#include

#include

void reverse(char s[])

{ int c,j,i;

for(i=0,j=strlen(s)-1;i

{

c=s[i];

s[i]=s[j];

s[j]=c;

}

}

main()//主函数,用于测试reverse函数的功能

{ char s[]="123456";

reverse(s);//调用倒置函数

printf("倒置后的字符串为:%s/n",s);//输出倒置后的字符串

}

运行结果如下;654321

功能颠倒字符串中字符次序。  语法Reverse (

string )

参数string:要颠倒字符次序的字符串返回值String。函数执行成功时返回颠倒字符次序后的字符串,如果发生错误,那么返回空字符串("")。用法Reverse()函数将一个字符串中最后一个字符放置到另一个字符串的第一个字符位置、倒数第二个字符放置在另一个字符串的第二个字符位置,以此类推。

EXAMPLE:

// reverse algorithm example

#include

#include

#include

using namespace std;

int main () {

vector myvector;

vector::iterator it;

// set some values:

for (int i=1; i<10; ++i) myvector.push_back(i);

// 1 2 3 4 5 6 7 8 9

reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2

1

// print out content:

cout << "myvector

contains:";

for (it=myvector.begin(); it!=myvector.end(); ++it)

cout << " "

<< *it;

cout << endl;

return 0;

}

Output:

myvector contains: 9 8 7 6 5 4 3 2 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值