I have a list from the stl called Clist. It has alot of items in it and one is a double called fVal. I want to insert new nodes in the list in ascending value according to the fVal. I have tried the following code and it works fine for up 8 nodes but after 9 nodes it breaks down and just inserts new nodes at the end of the list. Any comments would be very much appreciated.

int Ccounter = 0;
				itC = Clist.begin();
				while( Ccounter <= Clist.size())
					Ctest = *itC; 
					Ctest2 = *itC;
					if((fx1 >= Ctest.fVal) && (fx1 <= Ctest2.fVal) )
						Clist.insert(itC, X1);
						Ccounter = Ccounter + Clist.size();
					if(Ccounter == 0 && fx1 <= Ctest.fVal)
						Ccounter = Ccounter+Clist.size()+1;
					if(itC == Clist.end())
						Ccounter = Ccounter+Clist.size();
6 Years
Discussion Span
Last Post by Lerner

use std::find_if to locate the right position to insert the new item.
For example:

#include <iostream>
#include <functional>
#include <algorithm>
#include <list>
#include <iterator>

struct Ctest
    Ctest( double fv ) : fVal(fv) /* .... */ { /* ... */ }

    // ...

    [b]bool operator > ( const Ctest& that ) const
    { return this->fVal > that.fVal ; }[/b]

    operator double () const { return fVal ; }

    // ...

       double fVal ;

       // ...

int main()
  using namespace std ;

  list<Ctest> Clist ;

  double fval ;
  while( cout << "? " &&  cin >> fval )
      [b]Clist.insert( find_if( Clist.begin(), Clist.end(),
                             bind2nd( greater<Ctest>(), fval ) ),
                    fval ) ;[/b]

      copy( Clist.begin(), Clist.end(),
                  ostream_iterator<double>( cout, "->" ) ) ;

      cout << "null\n" ;

Edited by vijayan121: n/a


vijayan121 demonstrates full use of STL. Here's a version that's not quite so sophisticated and may be a little easier for a beginner to understand.

Key concepts:
1) The insert() method of lists inserts to the left of (that is, before) the iterator. This allows it to work for a list of size one and, given that end() exists (by requirements of the standard), allows for insertion at the end of the list as well, without need for separate special cases to cover insertion at the beginning or end of the list.

2) You don't need the variable counter at all, the number of nodes in the list are maintained, by requirement of the standard, by the implementation of the STL list class. That value is obtainable by calling the size() method.

if list size is zero (ie, if the list is empty)
 push current value on list
 //start at the begining
 iterator starts at begin()

 //loop to find first value in list that is larger than value to insert
 while iterator not equal to end()
    if value in iterator is less than the value to insert
       advance iterator by one

//insert value 
  Clist.insert(iterator, val)

Edited by Lerner: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.