中心反转中文句子

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

vector<wstring> GetStr (const wstring &s)
{
    locale china ("chs");

    wistringstream line (s);
    wstring word;
    vector<wstring> words;
    while (line >> word) {
        words.push_back (word);
    }
    return move (words);
}

vector<wstring> TurnStrVec (const vector<wstring> &vec)
{
    auto vec_mid_it = 
        vec.cbegin() + (vec.size () / 2);

    vector<wstring> tmp_vec (vec_mid_it
                            , vec.cend());
    tmp_vec.insert (tmp_vec.cend()
                    , vec.cbegin ()
                    , vec_mid_it);
    return move (tmp_vec);
}

wstring VecToStr (const vector<wstring> &vec)
{
    wstring tmp_s;
    for (const auto &elem : vec) {
        tmp_s.append (elem);
        //tmp_s.append (L" ");
    }
    return move (tmp_s);
}

void TurnStr (wstring &s)
{
    s = VecToStr (TurnStrVec(GetStr(s)));
}


int main ()
{
    ios::sync_with_stdio(flase);
    locale china ("chs");
    wcout.imbue (china);
    wstring s (L"你 好 啊, 朋 友!");
    TurnStr (s);
    wcout << s << endl;
    return 0;
}

 

posted @ 2015-01-30 14:31  wu_overflow  阅读(198)  评论(0编辑  收藏  举报