(一)用基本的数组实现

#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++基础--字符串倒序输出的更多相关文章

  1. Java基础知识强化08:将字符串倒序输出(包括空格)的几种方法

    1.最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了: package himi.hebao05; public class TestDemo02 { pub ...

  2. Java实现字符串倒序输出的几种方法

    1. 最容易想到的估计就是利用String类的toCharArray(),再倒序输出数组的方法了. import javax.swing.JOptionPane; public class Rever ...

  3. Java基础 -- 字符串(格式化输出、正则表达式)

    一 字符串 1.不可变String String对象是不可变的,查看JDK文档你就会发现,String类中每一个看起来会修改String值的方法,实际上都是创建一个全新的String对象,以包含修改后 ...

  4. C语言-实现字符串倒序输出

    方法1: Action(){//倒序输出 char *src="abcdefgh123"; char *desc; desc=(char *)malloc(100*sizeof(c ...

  5. Java基础学习【字符串倒序输出+排序】

    字符串逆序输出 import java.util.*; public class Main{ public static void main(String [] args) { //字符串逆序输出 S ...

  6. C++如何输入单行和多行带空格的字符串并将字符串倒序输出

    首先,我们知道在C++中,空格或者回车被认为是字符串的分割标志,使用cin输入string类的字符串时遇到会自动停止接收输入 例如,当如下程序时: #include <bits/stdc++.h ...

  7. Python基础 | 字符串格式化输出及print()函数介绍

    在写代码时,我们会经常与字符串打交道,Python中控制字符串格式通常有三种形式,分别是使用str%,str.format(),f-str,用法都差不多,但又有一些细微之差. 一起来看看吧~~~ 一. ...

  8. Java开发中经典的小实例-(字符串倒序输出)

    public class Test12 {    public static void main(String[] args) {        // TODO Auto-generated meth ...

  9. C#字符串的倒序输出

    介绍 在本文中,我将演示如何将字符串的单词倒序输出.在这里我不是要将“John” 这样的字符串倒序为成“nhoJ”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...

随机推荐

  1. Hadoop Hive概念学习系列之什么是Hive?

    参考  <Hadoop大数据分析与挖掘实战>的在线电子书阅读                   http://yuedu.baidu.com/ebook/d128cf8e33687e21 ...

  2. mac 配置charles

    从官网下载链接http://www.charlesproxy.com/download 附上注册码: Registered Name: https://zhile.io License Key: 48 ...

  3. UVALive - 4682

    /*H E A D*/ struct Trie{ int son[maxn<<2][2]; int b[67],tot; void init(){ // memset(son,0,size ...

  4. 关闭PhpStorm拼写错误

  5. Oracle 设定字符集

    在Redhat上安装Oracle 10g没有设定字符集,采用的是操作系统默认字符集:WE8ISO8859P1,将字符集修改为:ZHS16GBK.由于过程不可逆,首先需要备份数据库. 1.数据库全备 2 ...

  6. hdu-1022-栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. 认识CSS中css背景样式设置

    前端之HTML,CSS(五) CSS CSS背景 CSS可以添加背景颜色和背景图片,也可以对图片进行设置.设置的样式有: background-color 背景颜色 background-image ...

  8. python 爬虫系列09-异步斗图来一波

    斗图斗图,妈妈再也不怕我都不赢了 import requests from lxml import etree from urllib import request import os import ...

  9. 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)

    前言 上两章节,介绍了下关于注册中心-Eureka的使用及高可用的配置示例,本章节开始,来介绍下服务和服务之间如何进行服务调用的,同时会讲解下几种不同方式的服务调用. 一点知识 何为负载均衡 实现的方 ...

  10. 深入理解JavaScript系列(20):《你真懂JavaScript吗?》答案详解

    介绍 昨天发的<大叔手记(19):你真懂JavaScript吗?>里面的5个题目,有很多回答,发现强人还是很多的,很多人都全部答对了. 今天我们来对这5个题目详细分析一下,希望对大家有所帮 ...