Inserting and removing elements from a sorted container

Dirk dirk at email.address
Sun Nov 19 13:41:51 UTC 2017


Hi!

I want to add an uint into a container, but avoid duplicate 
uints, similar to a set<> from C++ STL.

To find out if an uint is already present in the container, it 
would make sense if the container is sorted.

This is some pseudo-D-code that should make clear what i want to 
do:

auto sortedlist = SList!uint(1, 2, 3, 5);
uint newValue = 4;
bool appendValue = true;
foreach( i; sortedlist[] ) {
     if( value == i ) {
         // value already in list
         appendValue = false;
         break;
     }
     if( i > newValue ) {
         // value not there. Insert value in front of i
         sortedlist.insertBefore(i, newValue);
         appendValue = false;
     }
}
if( appendValue )
     sortedlist.append( newValue );

Can someone help me?

Thank you,
Dirk


More information about the Digitalmars-d-learn mailing list