紧凑、可读、高效的C++算法,可就地反转字符串的单词
Compact, readable, efficient C++ algorithm to reverse the words of a string IN-PLACE
所以我试图为提示提出一个很好的C++解决方案
"字符串中的反转单词(单词由一个或多个单词分隔 空格)。现在就地执行。到目前为止,最受欢迎的字符串问题!
而我现在拥有的是一个怪物:
void reverse_words ( std::string & S )
{
/*
Programming interview question: "Reverse words in a string (words are separated by one or more spaces). Now do it in-place. By far the most popular string question!"
http://maxnoy.com/interviews.html
*/
if (S.empty()) return;
std::string::iterator ita = S.begin() , itb = S.end() - 1;
while (ita != itb)
{
if (*ita != ' ')
{
std::string sa; // string to hold the current leftmost sequence of non-whitespace characters
std::string::iterator tempa = ita; // iterator to the beginning of sa within S
while (ita != ' ' && ita != itb) sa.push_back(*ita++); // fill sa
while (*itb == ' ' && itb != ita) --itb; // move itb back to the first non-whitespace character preceding it
std::string sb; // string to hold the current rightmost sequence of non-whitespace characters
std::string::iterator tempb = itb; // iterator to the end of sb within S
while (*itb != ' ' && itb != ita) sb.push_back(*itb--); // fill sb
S.replace(tempa, ita-tempa, sb); // replace the current leftmost string with the current rightmost one
S.replace(tempb, itb-tempb, sa); // and vice-versa
}
else
{
++ita;
}
}
}
我想我的想法是正确的(找到第一个字符串,将其与最后一个字符串交换,找到第一个字符串之后的下一个字符串,将其与最后一个字符串之前的字符串交换,等等),但我需要一些更好的工具来实现这一目标。如何利用标准库来解决这个问题?
如果您使用以下方法,这非常简单:
- 反转整个字符串。
- 分别颠倒每个单词。
这个算法已经是 O(n),你只需运行一次整个字符串,然后再次运行以找到每个单词的反向。
现在,为了使它更有效率,看看反转序列的算法,它从前面和后面读取一个,并将每个存储在另一个的位置。现在,每当你存储一个空格时,你只是终止了一个单词,所以现在就反转这个词。效率更高的原因是包含该字的缓存在 CPU 上仍然很热。这里的困难是在极端情况下做到这一点。您必须考虑中间单词,多个连续空格和字符串两端的空格。
我想就是这样。简单、高效、清晰。请注意,std::basic_string::find...
在pos >= str.size()
时返回npos
。
void ReverseWords(std::string& msg) {
std::string::size_type pos_a = 0, pos_b = -1;
while ((pos_a = msg.find_first_not_of(' ', pos_b + 1)) != msg.npos) {
pos_b = std::min(msg.find(' ', pos_a + 1), msg.size());
std::reverse(msg.begin() + pos_a, msg.begin() + pos_b);
}
std::reverse(msg.begin(), msg.end());
}
相关文章:
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- C++-字符串是否包含一个带有简单循环的单词
- 在C++中查找(奇怪的)字符串中的单词
- 当字符串是某个单词时给出输出?
- 替换字符串中的单词,但忽略引号中的单词
- 替换字符串位置 X 中的单词C++
- 视觉C++使用 map 来比较字符串中的每个单词
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 输出返回编号。等于输入字符串的单词的字符串数
- 在字符串的每个单词的末尾插入字符串
- 分析字符串中的双精度和单词
- 如何使用运算符>>在自定义字符串中输入多个单词?
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- 字符串单词的唯一哈希值
- 使用std :: string ::比较(C )的字符串/单词操纵
- 如何比较字符串单词和聊天猜测
- 用C++计算文本文件中的相同字符串/单词