C++基础--字符串倒序输出
(一)用基本的数组实现
#include "stdafx.h"
#include <stdio.h>
#include <string.h> int main()
{
char ch1[10] = "abcde", ch2[10] = {0};
int n=0, i=0, j=0;
n = strlen(ch1);
for(i = n-1; i>=0; i--)
{
ch2[j] = ch1[i];
j++;
}
printf("%s\n%s\n", ch1, ch2);
return 0;
}
(二)加入向量vector, vector是具有方向的矢量容器,使用时,需include <vector>
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <vector>
int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
vector <char> cVec(ch1, ch1+n);
for(i = cVec.size()-; i>=; i--)
{
ch2[j] = ch1[i];
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}
(三)加入迭代器(iterator), iterator是一中检查容器内元素并遍历元素的数据类型,每个容器都可以定义自己的迭代器。
使用迭代器,需include <iterator>
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <vector>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
vector <char> cVec(ch1, ch1+n);
vector <char>::reverse_iterator cRIter;
for(cRIter=cVec.rbegin(); cRIter!=cVec.rend(); cRIter++)
{
ch2[j] = *cRIter;//同时也可更改*cRIter为cRIter[0];
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}
(四)使用双向链表list,list可以被视为一个双向链表,每个元素都具有前后元素的链接
1. (同上为反向迭代器)
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
list<char> cList(ch1, ch1+n);
list<char>::reverse_iterator cRIter; for(cRIter=cList.rbegin(); cRIter!=cList.rend(); cRIter++)
{
ch2[j] = *cRIter;
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}
(四)使用双向链表list,正向迭代器
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
list<char> cList(ch1, ch1+n);
list<char>::iterator cIter = cList.end();
cIter--;
for(cIter; cIter!=cList.begin();cIter--)
{
ch2[j] = *cIter;
j++;
}
if(cIter==cList.begin())
{
ch2[j] = *cIter;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}
以上所有输出结果为:
(五)使用双向链表list,iterator正向迭代器复制的例子;
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator> int main()
{
char ch1[] = "abcde", ch2[] = {};
int n=, i=, j=;
n = strlen(ch1);
list<char> cList(ch1, ch1+n);
list<char>::iterator cIter; for(cIter=cList.begin(); cIter!=cList.end(); cIter++)
{
ch2[j] = *cIter;
j++;
}
printf("%s\n%s\n", ch1, ch2);
return ;
}
输出结果为:
C++基础--字符串倒序输出的更多相关文章
- Java基础知识强化08:将字符串倒序输出(包括空格)的几种方法
1.最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了: package himi.hebao05; public class TestDemo02 { pub ...
- Java实现字符串倒序输出的几种方法
1. 最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了. import javax.swing.JOptionPane; public class Rever ...
- Java基础 -- 字符串(格式化输出、正则表达式)
一 字符串 1.不可变String String对象是不可变的,查看JDK文档你就会发现,String类中每一个看起来会修改String值的方法,实际上都是创建一个全新的String对象,以包含修改后 ...
- C语言-实现字符串倒序输出
方法1: Action(){//倒序输出 char *src="abcdefgh123"; char *desc; desc=(char *)malloc(100*sizeof(c ...
- Java基础学习【字符串倒序输出+排序】
字符串逆序输出 import java.util.*; public class Main{ public static void main(String [] args) { //字符串逆序输出 S ...
- C++如何输入单行和多行带空格的字符串并将字符串倒序输出
首先,我们知道在C++中,空格或者回车被认为是字符串的分割标志,使用cin输入string类的字符串时遇到会自动停止接收输入 例如,当如下程序时: #include <bits/stdc++.h ...
- Python基础 | 字符串格式化输出及print()函数介绍
在写代码时,我们会经常与字符串打交道,Python中控制字符串格式通常有三种形式,分别是使用str%,str.format(),f-str,用法都差不多,但又有一些细微之差. 一起来看看吧~~~ 一. ...
- Java开发中经典的小实例-(字符串倒序输出)
public class Test12 { public static void main(String[] args) { // TODO Auto-generated meth ...
- C#字符串的倒序输出
介绍 在本文中,我将演示如何将字符串的单词倒序输出.在这里我不是要将“John” 这样的字符串倒序为成“nhoJ”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...
随机推荐
- jmeter 正则表达式的关联
在工作中,用JM录制了登录---退出的脚本,但是多次请求后发现,总是返回的录制脚本的时候使用的账号的数据. 经过研究发现,login后,响应里的每次返回的token值是变化的,顺着往下看,下一个请求中 ...
- JSP使用Struts2标签库报错
JSP中使用<%@ taglib prefix="s" uri="/struts-tags" %>报错:Cannot find the tag li ...
- java 中的Debug eclipse 开发工具使用
Eclipse的debug模式:代码调试 * Eclipse或MyEclipse就是java的开发工具 * Eclipse开源的.免费的Java开发工具 * MyEclipse基于Eclipse开发出 ...
- 关于strcmp函数的用法
strcmp函数是在string.h库下的han函数, 具体用法如下: strcmp函数是用来比较2个字符串的函数,如srcmp(字如果符串1,字符串2),从第一个字符开始比较,如果到最后两个字符串完 ...
- 【Python】struts2-045批量检测
0x00 环境 存在struts2-045漏洞的war包 apache-tomcat 0x01 脚本 #coding:utf-8 import re import urllib import ...
- gitlab 日常运维命令
2018-10-24 gitlab查看版本 cat /opt/gitlab/embedded/service/gitlab-rails/VERSION 查看gitlab服务状态命令 gitlab-ct ...
- PIE SDK地图书签
地图书签,可以理解为暂时记录当前地图的范围和放大级别,在后续的操作中如果想回到地图之前的状态,就可以点击保存的书签就可以回到此状态,如图所示: 地图刚加载的时候是一幅世界地图 我们将地图的中心拖到南美 ...
- 深入应用C++11:代码优化与工程级应用》勘误表
https://www.cnblogs.com/qicosmos/p/4562174.html
- 一步步带你做vue后台管理框架
1.登录 (1). 一步步带你做vue后台管理框架(三)——登录功能 2.权限控制 (1) 基于Vue2.0实现后台系统权限控制 (2) 手摸手,带你用vue撸后台 系列二(登录权限篇)
- linux 运维基础之 禁止 ping
ping命令不要小瞧呀,小伙子!!! 听过死亡之ping不? 语法 ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面>][-l ...