作者peterwu4 (notd)
看板C_and_CPP
标题Re: [问题] 找出句子中最长的单字(C++)
时间Tue Dec 12 17:47:43 2017
※ 引述《MrPanda (不人气揪团师)》之铭言:
: 标题: [问题] 找出句子中最常的单字(C++)
: 时间: Sun Dec 10 13:03:18 2017
:
~
: 喂入的资料(Input):
:
: 条件
: 1. 遇到符号'.'为结数字元
: 2. 以空白键当作做为区隔单字识别字元
: 3. 长度一样则输出第一个
: 输入测试字串
: I am a normal ptt user like everybody.
: Hello world.
:
: 推 loveflames: 建议你把cppreference的list initialization看个一轮 12/12 15:00
: → loveflames: 问题通通迎刃而解 12/12 15:00
: → galic: 楼上示范一下如何在读完你说的参考资料以後 写出"迎刃而解" 12/12 17:34
: → galic: 的程式码 12/12 17:34
路过献丑一下XD
=====================
#include <iostream>
#include <string>
#include <list>
int main()
{
std::list<std::string> l;
std::string str;
do {
std::cin >> str;
if (str[str.length()-1] == '.') {
std::string str2 = str.substr(0, str.length()-1);
l.push_back(str2);
} else {
l.push_back(str);
}
} while (str[str.length()-1] != '.');
l.sort([](std::string &lhs, std::string &rhs)
{
return (lhs.length() > rhs.length()) ? true : false;
});
// 输出最长单字(在最前面)
std::cout << std::endl << l.front() << std::endl;
return 0;
}
===========================
初来乍到的,规矩多有不熟悉^^"
https://ideone.com/eKv1BS
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.224.124.20
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1513072065.A.548.html
1F:→ peterwu4: 好像不是在讲list,不过用container蛮好做的 12/12 18:02
※ 编辑: peterwu4 (36.224.124.20), 12/13/2017 07:41:15
2F:推 s25g5d4: 从头扫一次就能抓到最长的单字,何必用 sort? 12/13 20:22
3F:推 s25g5d4: 我认真看了一下,推文说的是 list initialization, 你回 12/13 20:31
4F:→ s25g5d4: 这个完全是不一样的东西啊,这是 STL container 耶 12/13 20:31
5F:→ peterwu4: 是啊,搞错了 12/14 00:51
6F:推 TitanEric: list的sort超花时间的 不过这里也不用sort就对了 12/14 12:25
7F:推 dustlike: 这个记忆体搬移量看得我全身都不舒服惹 12/15 15:21
8F:→ peterwu4: 可以去cplusplus或是cppreference看一下list这个contai- 12/19 11:30
9F:→ peterwu4: ner的特性,它和vector实现的方式是不一样的~ 12/19 11:30
10F:→ loveflames: vector可以直接当阵列用 12/20 09:47