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”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...
随机推荐
- [Beta阶段]第一次Scrum Meeting
Scrum Meeting博客目录 [Beta阶段]第一次Scrum Meeting 基本信息 名称 时间 地点 时长 第一次Scrum Meeting 19/04/29 大运村寝室6楼 70min ...
- R语言学习笔记(四)
6. 数据转换 本章主要讲述apply系列函数:apply.lapply.sapply.tapply.mapply,以及姊妹函数by.split.适用于批量处理数据,而不许循环. 6.1 向量分组 用 ...
- abp 后台项目在IIS 中运行
安装 Current .NET Core Hosting Bundle installer (direct download)
- 如何写javascript代码隐藏和显示这个div
如何写javascript代码隐藏和显示这个div 浏览次数:82次悬赏分:10 | 解决时间:2011-4-21 14:41 | 提问者:hade_girl <div id="div ...
- WPF 一 概述
创建一个项目吧 菜单>文件>新建>项目 看一看目录结构 WPF应用程序”会在“引用”里面自动添加下图中所示的 PresentationCore.PresentationFramewo ...
- js去掉浏览器右键点击默认事件(+vue项目开启右键行为)
js去掉浏览器右键点击默认事件 1.阻止整个页面所有的右击事件 document.oncontextmenu = function(){ return false;} 2.特定的区域/元素 docum ...
- 《STL详解》解题报告
看完发现文档缺页...... 3.5 菲波那契数 vector<int> v; v.push_back(); v.push_back(); ;i < ;++i) v.push_ba ...
- cas aqs lock之间的关系
CAS 对应cpu的硬件指令, 是最原始的原子操作 cas主要是在AtomicInteger AtomicXXX类中使用, 用于实现线程安全的自增操作 ++. 对应一个unsafe对象, 根据os平台 ...
- java中的线程(3):线程池类 ThreadPoolExecutor「线程池的类型、参数、扩展等」
官方文档: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html 1.简介 pu ...
- oracle 备份恢复篇(六)---基于12c的pdb备份与恢复
一,备份前提描述 SQL> show con_name CON_NAME ------------------------------ CDB$ROOT SQL> archive log ...