By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

Set of vectors and...

P: n/a
Hello.

I am having some problem organizing a set of vectors. The vectors itself,
could contain a pointer( say integer pointer) or could contain another
object MyClass.

1>So, first of all, is there anyway where I can accomodate both the vector
types into a single set. Something like a set<vector<void*>, my_compare func
.

Right now, I am having them as two different set dayatypes.

2>Coming to the compare_func and other things, lets assume this following
set type
set<vector<int*>, compare_func > for simplicity. Right now what I have for
the compare_func, what I have is something that isnt correct( I know that).

template <typename Cont>
struct less_vector
{
bool operator()( const Cont& c1, const Cont& c2)
{
return (c1 < c2);
}
};

and therefore I use set<vector<int*>, less_vector<vector<int*> >.

The problem as evident here is the comparison function compares the two
vector types which would do a lexicographical_compare on the pointers.
But what I really want is another comparison( and a set of comparison
functions that I could overload), which would

1>Do lexicographical compare based on the actual elements of the array that
int* in the vector points to.
2>Sort the vectors based on the addition of the elements in the array that
int* point to
3>blah blah blah...

Is there anyway of doing this ? I know it would mean sending a predicate, to
the less_vector Function Object, but I am not able to get the syntax
correct. The part
where I am confused is how do I use the iterator and the iterator_traits to
do that.

3>Last but not the least, How do I iterate over the set and through the
elements pointed to by the int* of the vector within the set.

Right now I use the for loop,

for(s1::iterator pos = s.begin(); pos != s.end(); pos++)
{
cout << "New Vector" << "\n" ;
transform((*pos).begin(), (*pos).end(), std::ostream_iterator<int>(cout,
"\n"),
Dereference());
}

The Dererence just does a derefernce of the int*. For sake of simplicity, I
have assumed that the the int* in the vector points only to one integer, so
you can do *iter in the Dereference and return the value.

What I am looking for is something like a single shot transform function
iterating over the entire thing
transform(s.begin(), s.end(), std::ostream_iterator<int>(cout, "\n"),
DereferenceandDisplaythevectorelemnts());

Thanks for reading and Thanks in advance for any solutions.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Amit" <am***********@intel.com> wrote in
news:d0**********@news01.intel.com:
Hello.

I am having some problem organizing a set of vectors. The vectors
itself, could contain a pointer( say integer pointer) or could contain
another object MyClass.


A set of vectors, huh? The first question I'd have is whether it makes any
sense at all to have an ordering on the vectors... ie: trying to make an
ordering between

{ 1, 2 }, { 2, 1 }, {}, { 1 }

(which, should end up as {}, { 1 }, { 1, 2 }, { 2, 1 } )

Now even assuming that ordering the vectors makes sense, you're not allowed
to modify the vectors inside the set. You'd have to erase the existing
vector and insert a new one (vector) in order to add an item to it.

Third, how do you locate the vector that you wish to insert a new item
into? (Assuming that the set of vectors is doing what you expect it to)

Jul 23 '05 #2

P: n/a

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 135...
"Amit" <am***********@intel.com> wrote in
news:d0**********@news01.intel.com:
Hello.

I am having some problem organizing a set of vectors. The vectors
itself, could contain a pointer( say integer pointer) or could contain
another object MyClass.
A set of vectors, huh? The first question I'd have is whether it makes

any sense at all to have an ordering on the vectors... ie: trying to make an
ordering between If you get a bunch of instructions which could be opcodes(dwords for
instance) and
they come down as an array, that I feed into a vector.
These vectors could have 5 instructons or 10 instructions or 100
instructions.
Further more, Based on the availability of the h/w, I can break the 100
instructions into instruction sets of 10.
The question of modifying the vector once inserted into the set doesnt even
come into the picture if you have read my problem.
Thanks for reading.

{ 1, 2 }, { 2, 1 }, {}, { 1 }

(which, should end up as {}, { 1 }, { 1, 2 }, { 2, 1 } )

Now even assuming that ordering the vectors makes sense, you're not allowed to modify the vectors inside the set. You'd have to erase the existing
vector and insert a new one (vector) in order to add an item to it.

Third, how do you locate the vector that you wish to insert a new item
into? (Assuming that the set of vectors is doing what you expect it to)

Jul 23 '05 #3

P: n/a
"Amit" <am***********@intel.com> wrote in
news:d0**********@news01.intel.com:

"Andre Kostur" <nn******@kostur.net> wrote in message
news:Xn*******************************@207.35.177. 135...
"Amit" <am***********@intel.com> wrote in
news:d0**********@news01.intel.com:
> Hello.
>
> I am having some problem organizing a set of vectors. The vectors
> itself, could contain a pointer( say integer pointer) or could
> contain another object MyClass.


A set of vectors, huh? The first question I'd have is whether it
makes

any
sense at all to have an ordering on the vectors... ie: trying to make
an ordering between

If you get a bunch of instructions which could be opcodes(dwords for
instance) and
they come down as an array, that I feed into a vector.
These vectors could have 5 instructons or 10 instructions or 100
instructions.
Further more, Based on the availability of the h/w, I can break the
100 instructions into instruction sets of 10.
The question of modifying the vector once inserted into the set doesnt
even come into the picture if you have read my problem.


Still not making any sense to me.

Let's assume that you get 3 of these arrays (call them array A, B, and
C), which get turned into vectors vA, vB, and vC. Also let's assume that
they have 5 elements each. How would you determine what the correct
ordering of vA, vB, vC is?
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.