user4541806
user4541806

Reputation:

C++ List Insert

I'm trying to insert objects into a list in an ordered manner, based on the objects data member family_id

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.

Running into some trouble with the List insert function... How can you insert before or behind the iterator (itr) using this function? I'm not sure whether to use some push_front / push_back methods, but the if/else statement can get quite long this way...

Would appreciate ideas on how to create this in a simple way.

Family Class

class Family
{
private:
   int family_id;

public:
   int get_family_id()
   {
      return family_id;
   }
};

Main

list <Family> families;

void insertFamily(int input_id)
{
   list<Family>::iterator itr;
   for(itr = families.begin(); itr != families.end(); itr++)
   {
      if (input_id < itr->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++
      }
};

Upvotes: 0

Views: 12420

Answers (1)

sabreitweiser
sabreitweiser

Reputation: 643

If you're asking what I think you're asking, the insert function will put the value into the position given, and push everything else back. For example:

#include <list>
#include <iostream>
int main(){
    std::list<int> my_list;
    for(int i = 0; i < 5; i++)
        my_list.push_back(i);
    std::list<int>::iterator itr = my_list.begin();
    ++itr;
    my_list.insert(itr, 5);
    for(itr = my_list.begin(); itr != my_list.end(); ++itr)
        std::cout << *itr << " ";
}

Prints 0 5 1 2 3 4

So if you want it to insert BEFORE whatever you stop on, just call the insert as given; if you want to insert AFTER whatever you stop on, call ++itr and then do the insert.


Edit: If I'm reading your code right, you're also missing a break statement

Family *fam = new Family(input_id);
families.insert(itr, *fam);
break;
^^^^

This breaks the for loop when you're done; without it, you'll keep adding new Families with the same id for every family with a lower index. Also, you don't need the itr++ in the else statement, since it's already being called in your for loop (unless you want to increase it twice when it's not matched?). Then your code might look like:

Main

list <Family> families;

void insertFamily(int input_id)
{
list<Family>::iterator itr;
for(itr = families.begin(); itr != families.end(); itr++)
{
   if (input_id < itr->get_familyid())
   {
         Family *fam = new Family(input_id);
         families.insert(itr, *fam);
         break; //Added break line
   } //Removed else
};

If you want faster insertion, you might look into binary searching, but this won't do much unless you have a very large list. Other than that, your code looks good!

Upvotes: 4

Related Questions