Practice5_1_string_Traversing

字符串中字符的遍历(Traversing)方式。

// Practice5_string.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    /* 字符的遍历*/
    char* cArray = "2017/3/2 22:40";
    string str(cArray);
    //数组方式
    for(unsigned int i = 0; i < str.size(); i++)
    {
        cout << str[i] << " ";
    }
    cout << endl;

    //正向迭代器方式
    string::iterator it = str.begin();
    for(; it != str.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //反向迭代器方式
    string::reverse_iterator itReverse = str.rbegin();
    for(; itReverse != str.rend(); itReverse++)
    {
        cout << *itReverse << " ";
    }
    cout << endl;

    return 0;
}

 

posted on 2017-03-02 22:46  cleverlzc  阅读(91)  评论(0编辑  收藏  举报