c++c++11stdforward-list

C++ standard library forward_list issue


I need some help solving the exercise 9.28 from the C++ Primer 5th edition. Here is the task:

Write a function that takes a forward_list and two additional string arguments. The function should find the first string and insert the second immediately following the first. If the first string is not found, then insert the second string at the end of the list.

I can't get the idea written in the book as to how forward_list actually works and why we indeed need it, so I'm asking for help. Below is the code I wrote which is incomplete and I need someone help me finish it:

#include <iostream>
#include <forward_list>

using namespace std;

void find_and_insert(forward_list<string>& flstr, const string& needle, const string& rplc) {

    auto curr = flstr.begin();
    auto last = flstr.end();
    bool found = false;

    while (curr != last) {

        if (*curr == needle) {

            found = true;
            // what to put here?!
            break;
        }

        // what to put here?!... 
        ++curr;
    }

    for (const auto& elem : flstr) {

        cout << elem << endl;
    }

    return;
}

int main(int argc, char *argv[]) {

    cout << endl;

    forward_list<string> flstring = {"Foo", "Bar", "Baz", "Tomcat"};

    find_and_insert(flstring, "Bar", "Insertion");

    cout << endl;

    return EXIT_SUCCESS;
}

Any suggestion to refactor the code is welcome!


Solution

  • You can write the function the following way using member function before_begin

    #include <iostream>
    #include <forward_list>
    #include <string>
    
    void find_and_insert( std::forward_list<std::string> &lst, 
                          const std::string &src, 
                          const std::string &dest ) 
    {
        auto before = lst.before_begin();
        auto first  = lst.begin();
    
        while ( first != lst.end() && *first != src ) ++before, ++first;
    
        lst.insert_after( first == lst.end() ? before : first, dest );
    }
    
    int main()
    {
        std::forward_list<std::string> lst;
    
        find_and_insert( lst, "one", "zero" );
    
        for ( const std::string &s : lst ) std::cout << s << ' ';
        std::cout << std::endl;
    
        find_and_insert( lst, "zero", "two");
    
        for ( const std::string &s : lst ) std::cout << s << ' ';
        std::cout << std::endl;
    
        find_and_insert( lst, "zero", "one");
    
        for ( const std::string &s : lst ) std::cout << s << ' ';
        std::cout << std::endl;
    
        find_and_insert( lst, "two", "three");
    
        for ( const std::string &s : lst ) std::cout << s << ' ';
        std::cout << std::endl;
    
        find_and_insert( lst, "four", "four");
    
        for ( const std::string &s : lst ) std::cout << s << ' ';
        std::cout << std::endl;
    }    
    

    The program output is

    zero 
    zero two 
    zero one two 
    zero one two three 
    zero one two three four