Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 9.28(The loop may not stop) #217

Open
Yuanzidan081 opened this issue Oct 12, 2023 · 0 comments
Open

Exercise 9.28(The loop may not stop) #217

Yuanzidan081 opened this issue Oct 12, 2023 · 0 comments

Comments

@Yuanzidan081
Copy link

The loop can't stop if there is a string in the list (type: forward_list) that can match to_find (type: string)

void find_and_insert(forward_list<string> &list, const string& to_find, const string& to_add)
{
    auto prev = list.before_begin();
    auto size = std::distance(list.begin(), list.end());
    for (auto curr = list.begin(); curr != list.end(); prev = curr++)
        if (*curr == to_find) list.insert_after(curr, to_add);
    if (size == std::distance(list.begin(), list.end())) list.insert_after(prev, to_add);
}

The code should be modified in this line:

if (*curr == to_find) curr = list.insert_after(curr, to_add);

best regards!

@Yuanzidan081 Yuanzidan081 changed the title Excise 9.28(The loop may not stop) Exercise 9.28(The loop may not stop) Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant