C++基础--字符串倒序输出

本文介绍了C++中实现字符串倒序输出的四种方法,包括基本数组、使用vector、迭代器以及双向链表list,并提供了相关的头文件引用。通过这些方法,可以有效地遍历并输出字符串的反向顺序。

(一)用基本的数组实现

#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[10] = "abcde", ch2[10] = {0}; int n=0, i=0, j=0; n = strlen(ch1); vector <char> cVec(ch1, ch1+n); for(i = cVec.size()-1; i>=0; i--) { ch2[j] = ch1[i]; j++; } printf("%s\n%s\n", ch1, ch2); return 0; }

(三)加入迭代器(iterator), iterator是一中检查容器内元素并遍历元素的数据类型,每个容器都可以定义自己的迭代器。

使用迭代器,需include <iterator>

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <vector>
#include <iterator>

int main()
{
    char ch1[10] = "abcde", ch2[10] = {0};
    int n=0, i=0, j=0;
    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 0; }

(四)使用双向链表list,list可以被视为一个双向链表,每个元素都具有前后元素的链接

1. (同上为反向迭代器)

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator>

int main()
{
    char ch1[10] = "abcde", ch2[10] = {0};
    int n=0, i=0, j=0;
    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 0;
}

(四)使用双向链表list,正向迭代器

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator>

int main()
{
    char ch1[10] = "abcde", ch2[10] = {0};
    int n=0, i=0, j=0;
    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 0;
}

以上所有输出结果为:

(五)使用双向链表list,iterator正向迭代器复制的例子;

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
using namespace std;
#include <list>
#include <iterator>

int main()
{
    char ch1[10] = "abcde", ch2[10] = {0};
    int n=0, i=0, j=0;
    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 0;
}

输出结果为:

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值