C++ 需要在C+中删除和插入的帮助吗+;
所以基本上我想创建一个格式函数,它接受一个字符串,并用用户想要替换的内容替换该字符串中的单词。起初,我对不可延迟的迭代器有一些问题,直到我意识到当更改字符串的大小时,任何迭代器都可能无效。现在它不再抛出异常了,因为输出与输入相同。有什么建议吗C++ 需要在C+中删除和插入的帮助吗+;,c++,insert,erase,C++,Insert,Erase,所以基本上我想创建一个格式函数,它接受一个字符串,并用用户想要替换的内容替换该字符串中的单词。起初,我对不可延迟的迭代器有一些问题,直到我意识到当更改字符串的大小时,任何迭代器都可能无效。现在它不再抛出异常了,因为输出与输入相同。有什么建议吗 string& formatFn(string& s, string& oldWord, string& newWord) { string word = ""; for (auto iter1 = s.be
string& formatFn(string& s, string& oldWord, string& newWord)
{
string word = "";
for (auto iter1 = s.begin(); iter1 != s.end(); ++iter1)
{
string tmpWord = "";
if (!isblank(*iter1)) // Testing for whitespace
{
tmpWord += *iter1;
if (tmpWord == oldWord)
{
string::iterator beg = iter1 - word.size();
string::iterator end = iter1;
auto sIter = s.erase(beg, end); // Get the position returned by erase
auto i = sIter - s.begin(); // Get an index
s = s.insert(s[i], newWord);
}
}
if (isblank(*iter1))
{
tmpWord.clear();
}
}
return s;
}
我不确定word实际上做了什么。您正在尝试删除旧单词,对吗?那么应该是:
string::iterator beg = iter1 - oldWord.size();
编辑:这是代码的改进版本:
string formatFn(const string& s, const string& oldWord, const string& newWord) {
string result = ""; // holds the string we want to return
string word = ""; // while iterating over 's', holds the current word
for (auto iter1 = s.begin(); iter1 != s.end(); ++iter1) {
if (!isblank(*iter1))
word += *iter1;
else { // if it is a whitespace, it must be the end of some word
// if 'word' is not same as 'oldword', just append it
// otherwise append 'newWord' instead
if (word == oldWord)
result += newWord;
else
result += word;
result += *iter1;
word.clear(); // reset 'word' to hold the next word in s
}
}
// the end of the string might not end with a whitespace, so the last word
// might be skipped if you don't make this test
if (word == oldWord)
result += newWord;
else
result += word;
return result;
}
如果已经使用字符串,为什么不使用所有方法
for (auto it = text.find(o_text); it != string::npos; it = text.find(o_text)){
text.replace(it, o_text.size(), n_text);
}
你把事情复杂化了:
std::string replace_all(std::string s, const std::string& sOld, const std::string& sNew)
{
std::size_t p = s.find(sOld);
while (p != std::string::npos)
{
s.replace(p, sOld.length(), sNew);
p = s.find(sOld, p + sNew.length());
}
return s;
}
如果您希望仅替换整个单词(您当前的尝试不会这样做):
#包括
#包括
布尔测试(常数标准::字符串和s,常数标准::字符串和已售出,标准::大小\u t位置)
{
return(pos==0 | |!::isalpha(s[pos-1])&&(!::isalpha(s[pos+sall.length())| | pos+sall.length()>=s.length());
}
std::size\u t find\u word(const std::string&s,const std::string&sed,std::size\u t pos)
{
pos=s.find(已售出,pos);
而(pos!=std::string::npos&&(!test(s,sell,pos)&&pos std::cout为什么不使用string::find
和string::replace
?我认为OP的要求是替换单词,而不是任何子字符串。这就是为什么他拥有所有的空格处理。为什么不接受对字符串的引用并对其进行变异?然后可以返回替换的单词数,例如std::size\u t replace\all(std::string&s,const std::string&old,const std::string&new);
@rwols:您可以这样做,但这会改变原始值。这种方法会获取临时值并返回它(相当于通过const引用传递并制作副本),保留原稿。如果修改原稿是可以接受的,那么返回void
并通过引用传递也会起作用。关键在于算法。@john:如果你只在出售时传递单词,那么所有被替换的都是匹配的单词(不必担心空格)。此外,如果他想在sell
参数中传入多个单词,并用sNew
中的内容替换它们,那么将sell
的参数类型更改为const std::vector&
修改算法以循环所有sell
以进行替换是一件相当简单的事情。似乎OP想要替换单词,find将查找任何子字符串,而不仅仅是整个单词。
std::string replace_all(std::string s, const std::string& sOld, const std::string& sNew)
{
std::size_t p = s.find(sOld);
while (p != std::string::npos)
{
s.replace(p, sOld.length(), sNew);
p = s.find(sOld, p + sNew.length());
}
return s;
}
#include <iostream>
#include <string>
bool test(const std::string& s, const std::string& sOld, std::size_t pos)
{
return (pos == 0 || !::isalpha(s[pos - 1])) && (!::isalpha(s[pos + sOld.length()]) || pos + sOld.length() >= s.length());
}
std::size_t find_word(const std::string& s, const std::string& sOld, std::size_t pos)
{
pos = s.find(sOld, pos);
while (pos != std::string::npos && (!test(s, sOld, pos) && pos < s.length()))
{
pos++;
pos = s.find(sOld, pos);
}
return pos;
}
std::string replace_all(std::string s, const std::string& sOld, const std::string& sNew)
{
std::size_t p = find_word(s, sOld, 0);
while (p != std::string::npos && p < s.length())
{
s.replace(p, sOld.length(), sNew);
p = find_word(s, sOld, p + sNew.length());
}
return s;
}
int main()
{
std::string sOrig = "eat Heat eat beat sweat cheat eat";
std::string sOld = "eat";
std::string sNew = "ate";
std::string sResult = replace_all(sOrig, sOld, sNew);
std::cout << "Result: " << sResult << std::endl;
// Output: "ate Heat ate beat sweat cheat ate"
return 0;
}