C ++简单字典:排序单词列表

时间:2015-05-11 14:49:16

标签: c++

int main()
{
    vector<string> words;
    cout << "Please enter some words\n";
    for (string temp; cin >> temp; words.push_back(temp), sort(words.begin(), words.end()))
        for (int i = 0; i < words.size(); ++i){
            if (i == 0 || words[i - 1] != words[i])
                cout << words[i] << "\n";
    }
    return 0;
}

代码必须能够按首字母列出排序的单词,例如我输入:

a man a plan a canal panama; 

它会写:

    a
    canal
    man
    panama
    plan

任何人都可以给我建议是我的错误循环。我使用VS 2013.任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <set>

using namespace std;

int main()
{

    multiset<string> words;

    for(string temp; cin >> temp; words.insert(temp))
    {
    }

    cout << endl;
    copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));

    return 0;
}