#### Archived

This topic is now archived and is closed to further replies.

# A better way??

This topic is 5417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wrote the following function as way to eliminate duplicate points that I stored inside an STL vector. Would anyone please be able to give me some ideas on ways that I could optimize this? Thank you.
template <typename tType>
void fn_eliminateDuplicatePoints(vector<cl_3dPoint<tType> >& vertices);

template <typename tType>
void fn_eliminateDuplicatePoints(vector<cl_3dPoint<tType> >& vertices)
{
int i = 0;
int j = 0;
bool* duplicateFound = NULL;

// GET THE TOTAL NUMBER OF POINTS

int number_of_points = vertices.size();

duplicateFound = new bool[number_of_points];

// INITIALIZE OUR ARRAY OF FLAGS

for( i = 0 ; i < number_of_points ; i = i + 1 )
{
duplicateFound[i] = false;
}
// TAG THE INDEXES OF THE ARRAY WHEREVER DUPLICATE POINTS OCCUR BY SETTING OUR BOOLEAN FLAG

for( i = 0 ; i < number_of_points ; i = i + 1 )
{
for( j = i + 1 ; j <= number_of_points - 1 ; j = j + 1  )
{
if( ( vertices[i] == vertices[j] ) && ( duplicateFound[j] == false  ) )
{
duplicateFound[j] = true;
}
}
}

// GO THRU THE VECTOR CONTAINER ELIMINATING ALL DUPLICATE POINTS

for( i = number_of_points - 1 ; i >= 0 ; i = i - 1 )
{
if( duplicateFound[i] == true )
{
vertices.erase(&vertices[i]);
}
}
delete [] duplicateFound;  // DEALLOCATING OUR DYNAMICALLY ALLOCATED ARRAY

duplicateFound = NULL;
}

[edited by - kelaklub on January 17, 2004 9:42:11 PM] [edited by - kelaklub on January 17, 2004 9:43:52 PM]

##### Share on other sites
Is the order of your points important? If it isn''t, it might just be easier to std::sort() the vector then use std::unique() to get rid of duplicate elements.

##### Share on other sites
Hmmmmm, that sounds like a plan. Thanks.

##### Share on other sites
or use std::set if you want to store a set of unique things

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633353
• Total Posts
3011485
• ### Who's Online (See full list)

There are no registered users currently online

×