title: C++ List Insert
body: <p>I'm trying to insert objects into a list in an ordered manner, based on the objects data member <code>family_id</code></p>
  
  <p>The first object created skips this function, so there will be at least one object in the list named 'families' before this function is executed.</p>
  
  <p>Running into some trouble with the List insert function... How can you insert before or behind the iterator (<code>itr</code>) using this function? I'm not sure whether to use some <code>push_front</code> / <code>push_back</code> methods, but the if/else statement can get quite long this way...</p>
  
  <p>Would appreciate ideas on how to create this in a simple way.</p>
  
  <h3>Family Class</h3>
  
  <pre><code>class Family
  {
  private:
     int family_id;
  
  public:
     int get_family_id()
     {
        return family_id;
     }
  };
  </code></pre>
  
  <h3>Main</h3>
  
  <pre><code>list &lt;Family&gt; families;
  
  void insertFamily(int input_id)
  {
     list&lt;Family&gt;::iterator itr;
     for(itr = families.begin(); itr != families.end(); itr++)
     {
        if (input_id &lt; itr-&gt;get_familyid())
        {
           Family *fam = new Family(input_id);
           families.insert(itr, *fam);
           // Does this insert the object into the first position of the list,
           // bumping the original to the second position?
        } else
           itr++
        }
  };
  </code></pre>