C++ 在C+中实现查找和替换过程+;

C++ 在C+中实现查找和替换过程+;,c++,algorithm,C++,Algorithm,只是为了好玩,我正试图编写一个查找和替换过程,就像文字处理器一样。我想知道是否有人能帮我找出我做错了什么(我得到了一个超时错误),并能帮我写一个更优雅的过程 #include <iostream> #include <string> void find_and_replace(std::string& text, const std::string& fword, const std::string& rword) { for (std

只是为了好玩,我正试图编写一个查找和替换过程,就像文字处理器一样。我想知道是否有人能帮我找出我做错了什么(我得到了一个
超时
错误),并能帮我写一个更优雅的过程

#include <iostream>
#include <string>

void find_and_replace(std::string& text, const std::string& fword, const std::string& rword)
{
     for (std::string::iterator it(text.begin()), offend(text.end()); it != offend;)
     {
        if (*it != ' ')
        {
           std::string::iterator wordstart(it);
           std::string thisword;
           while (*(it+1) != ' ' && (it+1) != offend)
                thisword.push_back(*++it);   
           if (thisword == fword)
               text.replace(wordstart, it, rword);
        }
        else {
            ++it;
        }

     }   
}

int main()
{
    std::string S("Yo, dawg, I heard you like ...");
    std::string f("dawg");
    std::string w("dog");
    // Replace every instance of the word "dawg" with "dog":
    find_and_replace(S, f, w);
    std::cout << S;

    return 0;
}
#包括
#包括
void find_和_replace(std::string和text,const std::string和fword,const std::string和rword)
{
for(std::string::iterator it(text.begin()),off(text.end());it!=off;)
{
如果(*它!='')
{
std::string::迭代器wordstart(it);
std::字符串thisword;
而(*(it+1)!=''&(it+1)!=冒犯)
这个词。推回(*++);
if(thisword==fword)
替换(wordstart、it、rword);
}
否则{
++它;
}
}   
}
int main()
{
字符串S(“哟,老兄,我听说你喜欢……”);
std::字符串f(“dawg”);
std::字符串w(“狗”);
//将单词“dawg”的每个实例替换为“dog”:
查找和替换(S、f、w);
std::cout1)您不会将找到的单词的第一个符号推入“thisword”变量

2) 您只使用空格符号“”作为分隔符,逗号“,”如何。您的程序将找到单词“dawg”,而不是“dawd”

下面的代码可以工作,但您应该考虑其他分词符。您真的需要只替换整个单词,还是只替换符号序列

#include <iostream>
#include <string>

void find_and_replace(std::string& text, const std::string& fword, const std::string& rword)
{
     for (std::string::iterator it(text.begin()), offend(text.end()); it != offend;)
     {
        if (*it != ' ' && *it != ',')
        {
           std::string::iterator wordstart(it);
           std::string thisword;
           while ((it) != offend && *(it) != ' ' && *(it) != ',')
                thisword.push_back(*it++);   
           if (thisword == fword)
               text.replace(wordstart, it, rword);
        }
        else {
            ++it;
        }

     }   
}

int main()
{
    std::string S("Yo, dawg, I heard you like ...");
    std::string f("dawg");
    std::string w("dog");
    // Replace every instance of the word "dawg" with "dog":
    find_and_replace(S, f, w);
    std::cout << S;

    return 0;
}
#包括
#包括
void find_和_replace(std::string和text,const std::string和fword,const std::string和rword)
{
for(std::string::iterator it(text.begin()),off(text.end());it!=off;)
{
如果(*it!='&&*it!=','))
{
std::string::迭代器wordstart(it);
std::字符串thisword;
而((它)!=冒犯&&*(它)!='&&*(它)!=','))
这个词。推回(*it++);
if(thisword==fword)
替换(wordstart、it、rword);
}
否则{
++它;
}
}   
}
int main()
{
字符串S(“哟,老兄,我听说你喜欢……”);
std::字符串f(“dawg”);
std::字符串w(“狗”);
//将单词“dawg”的每个实例替换为“dog”:
查找和替换(S、f、w);

std::cout像大多数编辑器一样的查找和替换将涉及常规的 表达式。如果您要查找的只是文字 替换,您需要的功能是
std::search
,以查找 要替换的文本,以及执行以下操作的
std::string::replace
实际更换。您将面临的唯一真正问题是:
std::string::replace
会使迭代器无效。您可以 始终在字符串的开头开始搜索,但是 如果替换文本包含 搜索字符串(例如类似于
s/vector/std::vector/
)。 您应该转换从
std::search
在执行替换(
offset)之前将偏移量添加到字符串中
=iter-str.begin()
),然后将其转换回迭代器 (
iter=str.begin()+offset+replacement.size()
添加
replacement.size()
是为了避免重新扫描文本 您刚刚插入,这可能会导致一个无限循环 原因同上。)

  • 使用
    text.replace
    可能会使任何文本迭代器无效(即
    it
    off
    ):这是不安全的
  • 将每个字符复制到一个临时字符串(每次开始一个新词时都会创建和销毁该字符串)是一种浪费
最简单的可行方法是:

  • 使用
    find
    查找第一个匹配的子字符串:它返回一个
    位置
    ,在替换子字符串时该位置不会失效
  • 检查是否:
  • 子字符串要么位于文本开头,要么前面有单词分隔符
  • 子字符串要么在文本末尾,要么后面有一个单词分隔符
  • 如果2.1和2.2为真,则替换子字符串
  • 如果替换了它,则将替换字符串的长度增加
    位置
    (从1开始)
  • 否则,将
    位置
    增加搜索字符串的长度
  • 从1开始重复,这次从
    位置开始查找(从4/5开始)

  • 当步骤1返回position
    std::string::npos

    时结束,为什么不使用
    std::find_if
    ?然后使用返回的迭代器进行替换?有几个问题,但是…你从哪里得到了
    超时
    错误?单词分隔符与查找和替换有什么关系?他用se查错了树我不知道为什么需要单词分隔符,但是这个程序取代了用空格分隔的单词,而不仅仅是符号序列。我只是修复了当前实现中的错误。