huangmingchuan / Cpp_Primer_Answers

《C++ Primer》第五版中文版习题答案

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

练习9.49有错误

cegd748 opened this issue · comments

这是我的,bug少点。
#include
#include
std::string find_no_ascender_and_descender_and_maxlength_word(const std::string&);
int main()
{
std::cout << find_no_ascender_and_descender_and_maxlength_word("i am not evil, i am a ace, aaaaaa.") << std::endl;
return 0;
}

std::string find_no_ascender_and_descender_and_maxlength_word(const std::string& s)
{
std::string find, max_find;
std::string::size_type pos = 0, pos_end, size = 0, max_size = 0;
std::string alpha_need{ "acemnorsuvwxz" };
std::string alpha_not_need{ "bdfghijklpqty" };

while ((pos = s.find_first_of(alpha_need, pos)) != s.npos)
{
	if ((pos == 0 || s[pos - 1] == ' ')
		&& ((pos_end = s.find_first_not_of(alpha_need, pos + 1)) != s.find_first_of(alpha_not_need, pos + 1) || pos_end == s.npos)
		&& (size = (find = s.substr(pos, pos_end - pos)).size()) > max_size)
	{
		max_find = find;
		max_size = size;
		pos += size;
	}
	else
	{
		++pos;
	}
}

return max_find;

}