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”,.这是不一样的,因为它完全倒序了整个字符串.而以下代码将教你如何将“你 好 我是 缇 ...
随机推荐
- 在StoryBoard对UICollectionViewCell 进行Autolayout是遇到的Xcode6.01的BUG
使用Sb对UICollectionViewCell 的内容进行Autolayout约束时候,发现了一个Xcode6.01的BUG,就是你对UICollectionCell约束完了之后,在模拟器上现实的 ...
- Cheerleaders UVA - 11806 计数问题
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...
- Java 实现手机发送短信验证码
Java 实现手机发送短信验证码 采用引入第三方工具的方式,网上查了半天,发现简单的实现方式便是注册一个中国网建的账号,新建账号的时候会附带赠几条免费短信,彩信 ,之后想要在使用就得花钱了.简单的操作 ...
- python之freshman00
编译型vs解释型 编译型优点:编译器一般会有预编译的过程对代码进行优化.因为编译只做一次,运行时不需要编译,所以编译型语言的程序执行效率高.可以脱离语言环境独立运行.缺点:编译之后如果需要修改就需要整 ...
- 静态区间第K小(整体二分、主席树)
题目链接 题解 主席树入门题 但是这里给出整体二分解法 整体二分顾名思义是把所有操作放在一起二分 想想,如果求\([1-n]\)的第\(k\)小怎么二分求得? 我们可以二分答案\(k\), \(O(n ...
- Linq 与 Lambda 简单使用
//Lambda表达式详解 //int //List<int> numbers = new List<int> {1,2,3,4,5,6,7,8,9 }; //var n = ...
- nginx-图片资源代理
location ~ .*\.(gif|jpg|jpeg|png|bmp|mp3)$ { root E:/xx/Uploads/; } 记得放在根目录下面
- oracle 备份恢复篇(一)---rman
一,rman介绍 RMAN(Recovery Manager)是随Oracle服务器软件一同安装的工具软件,它可以用来备份和恢复数据库文件.归档日志和控制文件,用来执行完全或不完全的数据库恢复.与传统 ...
- CountDownLatch 多线程,等待所有线程结束
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); 构造 ...
- elasticsearch 2.4.0执行update的时候发现的一个问题
请关注inline参数的变化 正确: POST /test/type1/1/_update{ "script" : { "inline": "ctx. ...