Hi,

I have a 2 dimensional vector containing entries such as :

10X
01X
001

I merge two rows at a time and get a new row of elements. In other words, when i combine "10X" and "01X" i might get "001" but since "001" is already there i do not want to add it to the 2D vector.

I think "Set" will allow me to store unique elements. But i do not know how to define a Set for this.

Any help is appreciated.

Thanks

Hi,

I tried writing the code using <set> for my problem. Please let me know if my code is right or not. It is running but still i wish to make sure.

#include <set>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main()
{
	typedef std::vector<int> v1;
	set<v1> cube;
	
	v1 v2;
	v2.push_back(0);
	v2.push_back(0);
	v2.push_back(2);
	cube.insert(v2);
	v2.clear();
	
	v2.push_back(0);
	v2.push_back(1);
	cube.insert(v2);
	v2.clear();
	
	v2.push_back(0);
	v2.push_back(1);
	cube.insert(v2);
	v2.clear();
	
	v2.push_back(1);
	v2.push_back(1);
	v2.push_back(0);
	cube.insert(v2);
	v2.clear();

	v2.push_back(0);
	v2.push_back(0);
	v2.push_back(2);
	cube.insert(v2);
	v2.clear();

	v2.push_back(0);
	v2.push_back(2);
	v2.push_back(0);
	cube.insert(v2);
	v2.clear();

	cout << "Size of set = " << cube.size() << endl;
	
	// Display the cube.
	set<v1>::iterator it;
	for( it = cube.begin(); it != cube.end(); it++ ) 
	{
		for( vector<int>::const_iterator it1 = it->begin(); it1!= it->end(); ++it1 )
		{
			cout << *it1 << " ";
		}
		cout << endl;
	}
	
	return 0;
}

Hi,

I think that the set automatically sorts the elements. Is there a way to not do so?

Any help is appreciated.

That is right, set is has unique elements and is sorted.

Yes is possible, you have to write a own remove function.

like:
- remove only if is already in list/vector
- while not unique

This article has been dead for over six months. Start a new discussion instead.