Fork me on GitHub

算法之美--3.1.1 字符串

       明天回家了,也许是懒吧,这本书就开始学习了一段时间,后面就没有动过,本来博客专栏需要不断更新的,结果几个月没有动静了,认真想想,这本书上的东西真需要好好静下心来看才有收获,打算回家看看;晚上回去总结一下。

/*!
 * \file 算法之美--字符串.cpp
 * \date 2017/01/19 20:48
 *
 * \author ranjiewen
 * Contact: user@company.com
 *
 * \brief 
 *
 * TODO: long description
 *
 * \note
*/

#include <iostream>
#include <iomanip>
#include <string>
 
using namespace std;
int main(int argc, char** argv)
{
    string str = "高德纳 美国 计算机科学家 计算机程序设计艺术";
    string str_temp = "";
    str_temp.assign(str);

    string result[4] = {"","","",""};
    int position = 0;
    for (int i = 0; i < 3; i++)
    {
        position = str_temp.find(" ");
        result[i] = str_temp.substr(0, position);
        str_temp = str_temp.substr(position + 1, str_temp.length() - position);

    }
    result[3] = str_temp;
    cout << "姓名:" << setw(8) << result[0] << endl;
    cout << "国籍:" << setw(6) << result[1] << endl;
    cout << "职业:" << setw(14) << result[2] << endl;
    cout << "代表作:" << setw(18) << result[3] << endl;

    str_temp.swap(result[0]);   //将str_temp与reuslt[0]交换
    for (int j = 1; j < 4;j++)
    {
        str_temp += " ";
        str_temp.append(result[j]);
    }
    int equal = str.compare(str_temp);
    if (equal==0)
    {
        cout << "successful matching!" << endl;
    }
    else
    {
        cout << "Unsuccessful matching" << endl;
    }

    return 0;
}

 

posted @ 2017-01-19 21:15  ranjiewen  阅读(200)  评论(0编辑  收藏  举报