Для предисловия, я довольно недавно начал программирование самостоятельно, и, таким образом, я почти как новичок, как они получают. Если вы заметили какие-либо плохие общие правила кодирования в моем коде (в том числе не относящийся к моему вопросу), пожалуйста, дайте мне знать, так как я, честно говоря,, вероятно, не знаю ничего лучше.Почему эта строка работает дважды и с ненормальным поведением?
Onto вопроса:
Я пытаюсь сделать небольшой фрагмент кода, чтобы заменить вхождения искомой строки с новой строкой. Я знаю, что существуют функции, которые значительно улучшат процесс (т. Е. Находят, заменяют и т. Д.), Для целей упражнения я избегаю этих и ограничиваю себя использованием итераторов, вставкой и стиранием. Я также знаю, что есть много других мест, где код может быть улучшен, и я бы тоже хотел получить информацию об этих частях, но я в первую очередь обеспокоен тем, что я считаю аномальным поведением. То, что я прямо сейчас:
#include <iostream>
#include <string>
bool scan(const std::string, const std::string::iterator);
//replaces occurrences of oldVal in s with newVal
std::string& replace (std::string &s, const std::string &oldVal, const std::string &newVal)
{
std::string::iterator iter = s.begin();
while (iter != s.end()) { //process s
if (scan(oldVal, iter)) { //Checks if characters match
iter = s.erase(iter, iter + oldVal.size()); //deletes occurrence of oldVal
iter = s.insert(iter, newVal.begin(), newVal.end()); //inserts newVal in oldVal's place
iter = iter + newVal.size(); //continues processing after inserted newVal
}
else
++iter; //Move to next character in s to process
}
return s;
}
//This function returns 1 if the strings match and 0 if they do not
bool scan(const std::string target, std::string::iterator iter)
{
for (auto beg = target.begin(); beg != target.end(); ++beg) {
std::cout << "Line 27 " << *iter << " " << *beg << std::endl; //MAIN CONCERN! This line is run twice.
//(It was solely added for debugging. Verifies values being compared)
if (*iter != *beg) {
std::cout << "Condition met; "
<< *iter << " != " << *beg << std::endl; //added for debugging. Double verifies values post-comparison
return 0;
}
++iter;
++beg;
}
return 1;
}
int main()
{
std::string mainStr, oldStr, newStr;
std::getline(std::cin, mainStr); //Overall it'd be better for s to be a list of strings, but my concern is with line 27
std::cin.clear();
std::cin >> oldStr >> newStr;
std::cout << "Output: " << replace(mainStr, oldStr, newStr) << std::endl; //Prints post-replacement string
}
То, что кажется, происходит то, что строка 27 (станд :: соиЬ < < «Линия 27» ...) выполняется в два раза первый раз сканирования называется. Например, данный вход:
tho
tho though
Я получаю выход (// являются комментариями, я добавляю в наружной пробеге, то есть только для этого поста)
Line 27 t t //This part is run twice and does weird things
Line 27 h o //If it was just incrementing it should read "Line 27 h h"
Condition met; h != o //This shouldn't happen; The condition should have tested t != t
Line 27 h t //It's seems fine from this point onwards
Condition failed; h != t
Line 27 o t
Condition failed; o != t
Output: tho
Что может быть причиной этого?
Спасибо!
Я не знаю, почему он проголосовали за закрытие. Похоже на тему. Я не пробовал запускать его, но похоже, что @tea попытался его отладить. –