Jump to content
  • Advertisement
Sign in to follow this  

Container type for this problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a mesh file which im extracting data from that contains a list of vertices to be drawn, but this list contains at least 30% duplicates, as the triangles/faces are sharing vertices, so im wondering how i can go through the list, and copy all non duplicates to a second container, such that all the vertices in the second container are different. Now for this i could use an std::set, but the other part of the problem is that i dont want the second list to have any sort of automatic sorting, becuase i'll be taken the position of any any added vertex. Here is the rough code
vector <vertex> data;
vector <vertex> OptimizedData
readFile(data);//fill data from file

//remove duplicates
for (all data)
   index = AddToVectorIfNotDuplicate(data[0], &OptimizedData);

So it goes through the data vector from start to end, and then adds to optimzeddata, only if that value is not already in optimzeddata, either way, it returns the position of the new/existing value. ie //data 1,2,3 3,4,5 6,6,6 6,6,6 1,2,3 //optimized data 1,2,3 3,4,5 6,6,6 //index 0 1 2 2 0

Share this post

Link to post
Share on other sites
Here's some code that does what you want using a map. It would be better to use a hash_map, but hash_map is not standard [yet], so I would stick to map if you find it fast enough.
#include <iostream>
#include <map>
#include <vector>

struct Point {
float x,y,z;
Point(float x, float y, float z) : x(x),y(y),z(z) {

bool operator<(Point const &p) const {
return x<p.x;
return y<p.y;
return z<p.z;

// new_vector and indices are supposed to be empty when this function is called
void optimize_data(std::vector<Point> const &old_vector, std::vector<Point> &new_vector,
std::vector<size_t> &indices) {
std::map<Point,size_t> new_index;

for(std::vector<Point>::const_iterator it=old_vector.begin(), end=old_vector.end(); it!=end; ++it) {
std::map<Point,size_t>::const_iterator found=new_index.find(*it);
if(found==new_index.end()) {

int main() {
std::vector<Point> data;

std::vector<Point> optimized_data;
std::vector<size_t> index;
optimize_data(data, optimized_data, index);

for(std::vector<Point>::const_iterator it=optimized_data.begin(), end=optimized_data.end();
it!=end; ++it)
std::cout << '(' << it->x << ',' << it->y << ',' << it->z << ")\n";
std::cout << '\n';

for(std::vector<size_t>::const_iterator it=index.begin(), end=index.end(); it!=end; ++it)
std::cout << *it << '\n';
std::cout << '\n';

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!