gpt4 book ai didi

C++ 简单字典 : list of sorted words

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:18:21 24 4
gpt4 key购买 nike

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。任何帮助将不胜感激。提前致谢。

最佳答案

#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;
}

关于C++ 简单字典 : list of sorted words,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30170854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com