演示使用string对象(续)

/*演示将美国格式转换为国际格式的例子*/
#include<iostream>
#include<string>

using namespace std;
void main()
{
    cout << "Enter the date in American format"
        << "(eg.may 5, 2016)"<<endl;
    string Date;
    getline(cin, Date, '\n');
    /*注意‘\n’若输入空格,则Date参数将只能获得月,此参数的功能是告诉计算机,
    我输入的流,直到遇到换行符,则换行符前全部给对象Date*/
    int i = Date.find(" ");
    string Month = Date.substr(0, i); //取得月
    int k = Date.find(",");
    string Day = Date.substr(i + 1, k - i - 1);//取日
    string Year = Date.substr(k + 2, 4);//取年
    cout << "The original date is : " << Date << endl;
    cout << "The converted date is :" << Day + " " + Month + " " + Year << endl;
}

运行结果如图所示

posted @ 2016-03-20 21:53  知_行  阅读(231)  评论(0编辑  收藏  举报