0

Here is my code using STL library, where I try inserting a node at the end, in the middle and in front. For inserting in the middle, I want to provide insertion after a specific node, and not by incrementing the iterator by 2, as I might not know what to increment it by if it is a long list,

Kindly help why is find function not working:

#include <iostream>
#include <list>
#include <string>

using namespace std;

void printlist(list<int> l)
{
    list<int>::iterator it = l.begin();
    for (it; it != l.end(); ++it)
    {
        cout << "printlist function call list items:  " << *it << endl;
    }
}

int main()
{
    list<int> l;
    l.push_back(1);
    l.push_back(2);
    l.push_back(3);
    list<int>::iterator it = l.begin();
    cout << 1 << endl;
    printlist(l);
    l.push_front(0);
    cout << 2 << endl;
    printlist(l);
    it = l.find(l.begin(), l.end(), 2);
    l.insert(it, 25);
    cout << 3 << endl;
    printlist(l);
    return 0;
}

Thanks...

David G
  • 94,763
  • 41
  • 167
  • 253
Pavan Kate
  • 84
  • 1
  • 8
  • find is not a member function, it's a template defined in `` ... use `it= find(l.begin(),l.end(),2);` – quantdev Nov 23 '14 at 03:15
  • 2
    Why do you copy the whole list just to print its elements? – Neil Kirk Nov 23 '14 at 03:17
  • _"Kindly help why is find function not working:"_ Welcome to the c++ shark tank. It's all beyond any _kindness_ here. Improve your question 1st.__ – πάντα ῥεῖ Nov 23 '14 at 03:17
  • thanks, i was not getting as to why it isnt working.... – Pavan Kate Nov 23 '14 at 03:18
  • 2
    Spending some time reviewing all [**std::list<>**](http://en.cppreference.com/w/cpp/container/list) has to offer as well as the samples therein would probably help immensely. The canned [**algorithms**](http://en.cppreference.com/w/cpp/algorithm), notable [**`std::find`**](http://en.cppreference.com/w/cpp/algorithm/find), would be equally helpful. – WhozCraig Nov 23 '14 at 03:19
  • 1
    A frequent nitpick in these parts: "STL" is not the C++ standard library of collection classes that ship with your compiler. STL is the historical source of the collection designs that ultimately was modified and amended to become the C++ standard library of collection classes...but they are two different things. While some continue to use the terms interchangeably, it's really not ideal to do so. – HostileFork says dont trust SE Nov 23 '14 at 03:21

2 Answers2

4

std::list<> doesn't have a find() method. You can use the standard algorithm std::find() declared in <algorithm>:

it = std::find(l.begin(), l.end(), 2);
David G
  • 94,763
  • 41
  • 167
  • 253
1

See the answer by @0x499602D2.

But to elaborate on an important point raised in a comment by @NeilKirk, you wrote:

void printlist(list<int> l)
{
    list<int>::iterator it = l.begin();
    for (it; it != l.end(); ++it)
    {
       cout << "printlist function call list items:  " << *it << endl;
    }
}

Note that you are passing the list l by value, not by reference. Passing a class by value (that has not been designed to use implicit sharing) will make a copy. Thus, l will be a copy of the parameter passed. If your list contained a million elements, then passing it by value will make a million-element-copy. You can fix that with:

void printlist(list<int> & l) { ... }

Or if you don't plan on making any changes, it's always nice to announce that with:

void printlist(list<int> const & l) { ... }

Also, C++11 has a range-based for which does the iterator begin/end stuff under the hood for you, and automatic variable typing:

void printlist(list<int> const & l)
{
    for (auto i : l)
    {
       cout << "printlist function call list items:  " << i << endl;
    }
}

Lots of ways to get fancy in that spirit. But the more critical thing is not go making copies of your data structures, passing them by value when you don't need to!

Community
  • 1
  • 1