473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

STL container for sorted items

Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly

----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?

Mar 14 '07 #1
18 2916
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly

----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?
I think the formattin above is screwed up.I'm pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------

Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)


Mar 14 '07 #2
On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly
----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?

I think the formattin above is screwed up.I'm pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------

-------------
Obj1
------------
Obj2
------------

Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)
I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.

Regards

Mar 14 '07 #3
On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:
On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly
----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?
I think the formattin above is screwed up.I'm pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)

I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.

Regards
Assuming the class is called A and it contains id as int and the
pointers to your objects;
Hence:
typedef set <A, less<A>A_set;
..
..
..
For your A class definition:
bool A::operator < (const A & a) const {return this->id < a.id;}
now A_set s;
s takes simply an object of A and it is ensured that all the object of
A inserted into the set are sorted in ascending order based on the id
inside each object of A.

Hope it helps

Mar 14 '07 #4
On Mar 14, 9:40 pm, "coosa" <coos...@gmail.comwrote:
On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:


On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly
----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?
I think the formattin above is screwed up.I'm pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)
I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.
Regards

Assuming the class is called A and it contains id as int and the
pointers to your objects;
Hence:
typedef set <A, less<A>A_set;
.
.
.
For your A class definition:
bool A::operator < (const A & a) const {return this->id < a.id;}
now A_set s;
s takes simply an object of A and it is ensured that all the object of
A inserted into the set are sorted in ascending order based on the id
inside each object of A.

Hope it helps- Hide quoted text -

- Show quoted text -
How would a set help in returning an object compared to a map?
Since the items given to me are already sorted, and i would have to
retrieve an object based on its identity, wouldnt map be more useful
as in the set i would have to decode the object first?

Mar 15 '07 #5
On Mar 15, 1:17 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 9:40 pm, "coosa" <coos...@gmail.comwrote:
On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:
On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly
----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?
I think the formattin above is screwed up.I'm pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)
I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.
Regards
Assuming the class is called A and it contains id as int and the
pointers to your objects;
Hence:
typedef set <A, less<A>A_set;
.
.
.
For your A class definition:
bool A::operator < (const A & a) const {return this->id < a.id;}
now A_set s;
s takes simply an object of A and it is ensured that all the object of
A inserted into the set are sorted in ascending order based on the id
inside each object of A.
Hope it helps- Hide quoted text -
- Show quoted text -

How would a set help in returning an object compared to a map?
Since the items given to me are already sorted, and i would have to
retrieve an object based on its identity, wouldnt map be more useful
as in the set i would have to decode the object first?
Yes, map would be a better choice assuming you don't have to add any
more to your collection;
In simple words, if your application requires later that you add more
into your collection which needs to be always sorted, then I suggest
using a set; however, if you no longer need to add and the current
data you have is already sorted before inserting into your new
collection, then a map is a good choice for fast lookup, even though
it doesn't play role in maps whether the keys are sorted or not (some
one corrects me if I'm wrong).

Mar 16 '07 #6
On Mar 16, 9:06 am, "coosa" <coos...@gmail.comwrote:
On Mar 15, 1:17 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 9:40 pm, "coosa" <coos...@gmail.comwrote:
On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:
On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly
----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?
I think the formattin above is screwed up.I'm pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)
I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.
Regards
Assuming the class is called A and it contains id as int and the
pointers to your objects;
Hence:
typedef set <A, less<A>A_set;
.
.
.
For your A class definition:
bool A::operator < (const A & a) const {return this->id < a.id;}
now A_set s;
s takes simply an object of A and it is ensured that all the object of
A inserted into the set are sorted in ascending order based on the id
inside each object of A.
Hope it helps- Hide quoted text -
- Show quoted text -
How would a set help in returning an object compared to a map?
Since the items given to me are already sorted, and i would have to
retrieve an object based on its identity, wouldnt map be more useful
as in the set i would have to decode the object first?

Yes, map would be a better choice assuming you don't have to add any
more to your collection;
In simple words, if your application requires later that you add more
into your collection which needs to be always sorted, then I suggest
using a set; however, if you no longer need to add and the current
data you have is already sorted before inserting into your new
collection, then a map is a good choice for fast lookup, even though
it doesn't play role in maps whether the keys are sorted or not (some
one corrects me if I'm wrong).
But I just recalled ...
Why not using hash_map?
I guess it's the fastest way to retrieve elements by a certain key
Check http://www.sgi.com/tech/stl/hash_map.html or by MSDN
http://msdn2.microsoft.com/en-us/lib...6z(VS.80).aspx for more
reference.

Mar 16 '07 #7
On Mar 16, 6:21 am, "coosa" <coos...@gmail.comwrote:
On Mar 16, 9:06 am, "coosa" <coos...@gmail.comwrote:


On Mar 15, 1:17 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 9:40 pm, "coosa" <coos...@gmail.comwrote:
On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:
On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
Problem
Reocrds are already stored with a unique identity in a binary
format.The structure is roughly
----------------------
Record Count
--------------------
Record1 -------------------------------------- String Identifier
------------------
--------------------
Obj
pointer
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
I propose to have a map of <strings , Obj Pointer>. The map would be
initialised with the file item initially and then routines would be
provided to search an item based on the identifier and update an item
based on the identifier.
Also if dealing with a file stored in memory , is it advisable to use
a STL in the first place instead of say directly using address
manipulations to get to the items since the identifiers are already
sorted?
I think the formattin above is screwed up.I'm pasting the format once
again
----------------------
Record Count
--------------------
Record1
Record 2
------------------
Record n
--------------------
-------------
Obj1
------------
Obj2
------------
Each Record is made up of
Identity (unique)
Obj Pointer (pointer to objects obj1,ibj2)
I believe a Set is more appropriate;
The sorting itself should be made in such that the < operator should
be overloaded.
For instance, since sorted elements is important to you, you could
probably create a class which contains both the identity and object
content; in that class you should overload the less operator compared
to the identity.
Regards
Assuming the class is called A and it contains id as int and the
pointers to your objects;
Hence:
typedef set <A, less<A>A_set;
.
.
.
For your A class definition:
bool A::operator < (const A & a) const {return this->id < a.id;}
now A_set s;
s takes simply an object of A and it is ensured that all the object of
A inserted into the set are sorted in ascending order based on the id
inside each object of A.
Hope it helps- Hide quoted text -
- Show quoted text -
How would a set help in returning an object compared to a map?
Since the items given to me are already sorted, and i would have to
retrieve an object based on its identity, wouldnt map be more useful
as in the set i would have to decode the object first?
Yes, map would be a better choice assuming you don't have to add any
more to your collection;
In simple words, if your application requires later that you add more
into your collection which needs to be always sorted, then I suggest
using a set; however, if you no longer need to add and the current
data you have is already sorted before inserting into your new
collection, then a map is a good choice for fast lookup, even though
it doesn't play role in maps whether the keys are sorted or not (some
one corrects me if I'm wrong).

But I just recalled ...
Why not using hash_map?
I guess it's the fastest way to retrieve elements by a certain key
Checkhttp://www.sgi.com/tech/stl/hash_map.htmlor by MSDNhttp://msdn2.microsoft.com/en-us/library/6x7w9f6z(VS.80).aspxfor more
reference.- Hide quoted text -

- Show quoted text -
The more i think of it i feel we should use a vector with a pair
instead of any associative container. The reasons as explained in item
23 in Effective Stl by Scott meyers says that associative containers
would lead to page faults and more memory due to the fact that they
use 3 pointers internally and they are not stored in a contiguos
location in memory.
So when the decision is
Insert elements
Sort Elements -- skip this in my case
Look up elements
the best decision is to use a pair in a vector to achieve the same
thing as a map<string,structurewould do.
Do let me know if i'm wrong in my explanations

Mar 16 '07 #8
On Fri, 16 Mar 2007 04:59:02 -0700, Hunk wrote:
On Mar 16, 6:21 am, "coosa" <coos...@gmail.comwrote:
>On Mar 16, 9:06 am, "coosa" <coos...@gmail.comwrote:


On Mar 15, 1:17 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 9:40 pm, "coosa" <coos...@gmail.comwrote:
On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:
On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions
[snip]
Yes, map would be a better choice assuming you don't have to add any
more to your collection;
In simple words, if your application requires later that you add more
into your collection which needs to be always sorted, then I suggest
using a set; however, if you no longer need to add and the current
data you have is already sorted before inserting into your new
collection, then a map is a good choice for fast lookup, even though
it doesn't play role in maps whether the keys are sorted or not (some
one corrects me if I'm wrong).

But I just recalled ...
Why not using hash_map?
I guess it's the fastest way to retrieve elements by a certain key
Check http://www.sgi.com/tech/stl/hash_map.htmlor by
MSDNhttp://msdn2.microsoft.com/en-us/library/6x7w9f6z(VS.80).aspxfor
more reference.

The more i think of it i feel we should use a vector with a pair instead
of any associative container. The reasons as explained in item 23 in
Effective Stl by Scott meyers says that associative containers would
lead to page faults and more memory due to the fact that they use 3
pointers internally and they are not stored in a contiguos location in
memory.
So when the decision is
Insert elements
Sort Elements -- skip this in my case Look up elements
the best decision is to use a pair in a vector to achieve the same thing
as a map<string,structurewould do. Do let me know if i'm wrong in my
explanations
My inclination would be to use the data structure that most closely
reflects the problem you are addressing; in your case this sounds like a
map. You won't know until you try it whether efficiency is an issue. If it
is, you could try other ideas, such as a vector of pairs.

--
Lionel B
Mar 16 '07 #9
On Mar 16, 8:20 pm, Lionel B <m...@privacy.netwrote:
On Fri, 16 Mar 2007 04:59:02 -0700, Hunk wrote:
On Mar 16, 6:21 am, "coosa" <coos...@gmail.comwrote:
On Mar 16, 9:06 am, "coosa" <coos...@gmail.comwrote:
On Mar 15, 1:17 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 9:40 pm, "coosa" <coos...@gmail.comwrote:
On Mar 15, 12:30 am, "coosa" <coos...@gmail.comwrote:
On Mar 14, 7:23 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 14, 4:16 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
Would like some advice on the fillowing
I have a sorted list of items on which i require to search and
retrieve the said item and also modify an item based on its identity.
I think an Map stl container would be most suited. Am i correct in my
assumptions

[snip]
Yes, map would be a better choice assuming you don't have to add any
more to your collection;
In simple words, if your application requires later that you add more
into your collection which needs to be always sorted, then I suggest
using a set; however, if you no longer need to add and the current
data you have is already sorted before inserting into your new
collection, then a map is a good choice for fast lookup, even though
it doesn't play role in maps whether the keys are sorted or not (some
one corrects me if I'm wrong).
But I just recalled ...
Why not using hash_map?
I guess it's the fastest way to retrieve elements by a certain key
Checkhttp://www.sgi.com/tech/stl/hash_map.htmlorby
MSDNhttp://msdn2.microsoft.com/en-us/library/6x7w9f6z(VS.80).aspxfor
more reference.
The more i think of it i feel we should use a vector with a pair instead
of any associative container. The reasons as explained in item 23 in
Effective Stl by Scott meyers says that associative containers would
lead to page faults and more memory due to the fact that they use 3
pointers internally and they are not stored in a contiguos location in
memory.
So when the decision is
Insert elements
Sort Elements -- skip this in my case Look up elements
the best decision is to use a pair in a vector to achieve the same thing
as a map<string,structurewould do. Do let me know if i'm wrong in my
explanations

My inclination would be to use the data structure that most closely
reflects the problem you are addressing; in your case this sounds like a
map. You won't know until you try it whether efficiency is an issue. If it
is, you could try other ideas, such as a vector of pairs.

--
Lionel B
Agree with you; a map or precisely a hashed map.
Trust me on not using vectors; I've worked on the netflix competition
with a dataset containing over 100 millions of ratings and I used Java
for it, but it won't play a big role if I used C++; for some
processing the code ran and lasted around 2-3 days to finish; when I
decided to switch to HashMap it took less than 6 hours to process.
Vectors vs Maps, I also have the book of scott meyers and it's a great
one and I don't see a contradiction for using Maps or hashed maps; you
worry much about retrieving the elements fast and I guess Maps are
best suited for that.
Let me repeat again:
You need to ensure sorted elements -sets
you need uniqueness -sets and maps
you need fast lookup -maps and hashes
you need insertions and deletions from the beginning, ending or middle
of container -lists
you need random access -vectors
See what's most crucial for your application and decide for a
container.

Good luck

Mar 16 '07 #10
On Mar 16, 11:29 am, "coosa" <coos...@gmail.comwrote:
Agree with you; a map or precisely a hashed map.
Trust me on not using vectors; I've worked on the netflix competition
with a dataset containing over 100 millions of ratings and I used Java
for it, but it won't play a big role if I used C++; for some
processing the code ran and lasted around 2-3 days to finish; when I
decided to switch to HashMap it took less than 6 hours to process.
Were you using sorted vectors with a binary search? Or were you just
scanning through the vector? True, a hash map would still be faster
(in the average case) than a sorted vector, but I would like to know
how you used vectors in this case before I take your advice.
Vectors vs Maps, I also have the book of scott meyers and it's a great
one and I don't see a contradiction for using Maps or hashed maps; you
worry much about retrieving the elements fast and I guess Maps are
best suited for that.
Let me repeat again:
You need to ensure sorted elements -sets
you need uniqueness -sets and maps
you need fast lookup -maps and hashes
you need insertions and deletions from the beginning, ending or middle
of container -lists
you need random access -vectors
See what's most crucial for your application and decide for a
container.

Good luck

I think there is a little bit of misinformation here. Both std::map
and std::set are sorted, and they both have fairly fast (i.e. O(log
N)) lookup. Hash maps have even faster lookup (in the average case,
O(1)).

The reason that the OP might prefer a map over a set his case is that
he wants to modify the data in the records. While this can be done
with std::set (by storing a pointer to the record, and providing a
comparison operator), it isn't a natural fit for the problem.

However, as the data is already sorted, and if you will not be
inserting or deleting data, then using a std::vector is a fine idea.
To search a sorted vector, use std::lower_bound, which should do a
binary search (avoid std::binary_search, as it only returns a boolean
indicating containment).

If you need a reference:
http://www.sgi.com/tech/stl/lower_bound.html

-matt

Mar 16 '07 #11
On Mar 17, 2:58 am, "Matteo" <mah...@ncsa.uiuc.eduwrote:
On Mar 16, 11:29 am, "coosa" <coos...@gmail.comwrote:
Agree with you; a map or precisely a hashed map.
Trust me on not using vectors; I've worked on the netflix competition
with a dataset containing over 100 millions of ratings and I used Java
for it, but it won't play a big role if I used C++; for some
processing the code ran and lasted around 2-3 days to finish; when I
decided to switch to HashMap it took less than 6 hours to process.

Were you using sorted vectors with a binary search? Or were you just
scanning through the vector? True, a hash map would still be faster
(in the average case) than a sorted vector, but I would like to know
how you used vectors in this case before I take your advice.
Vectors vs Maps, I also have the book of scott meyers and it's a great
one and I don't see a contradiction for using Maps or hashed maps; you
worry much about retrieving the elements fast and I guess Maps are
best suited for that.
Let me repeat again:
You need to ensure sorted elements -sets
you need uniqueness -sets and maps
you need fast lookup -maps and hashes
you need insertions and deletions from the beginning, ending or middle
of container -lists
you need random access -vectors
See what's most crucial for your application and decide for a
container.
Good luck

I think there is a little bit of misinformation here. Both std::map
and std::set are sorted, and they both have fairly fast (i.e. O(log
N)) lookup. Hash maps have even faster lookup (in the average case,
O(1)).

The reason that the OP might prefer a map over a set his case is that
he wants to modify the data in the records. While this can be done
with std::set (by storing a pointer to the record, and providing a
comparison operator), it isn't a natural fit for the problem.

However, as the data is already sorted, and if you will not be
inserting or deleting data, then using a std::vector is a fine idea.
To search a sorted vector, use std::lower_bound, which should do a
binary search (avoid std::binary_search, as it only returns a boolean
indicating containment).
Exactly my point Matt. The data I have is already sorted.
In short my operations involve
1) Put the sorted data into a container ( I dont have to sort the
data, its already in sorted form)
2) Provide a look up to the data to the user based on unique identity
3) May have to modify the data (the pointer is stored in the vector).
Modifying the data will not result in a resort as the identity will
retain the same, only the data pointed will be different)
4)As matt pointed out, i'm using a std::lower_bound for lookup with a
predicate logic that compares strings for equality.
Something like
lower_bound(vector_data.begin(),vector_data.end(), identity,DataCompare())
Where vector_data refers to vector containing a pair of
(identity,,pointer) and DataCompare is the predicate logic defining
equality of identity which is in this case a string
If you need a reference:http://www.sgi.com/tech/stl/lower_bound.html

-matt

Mar 20 '07 #12
On Mar 20, 2:06 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 17, 2:58 am, "Matteo" <mah...@ncsa.uiuc.eduwrote:
On Mar 16, 11:29 am, "coosa" <coos...@gmail.comwrote:
Agree with you; a map or precisely a hashed map.
Trust me on not using vectors; I've worked on the netflix competition
with a dataset containing over 100 millions of ratings and I used Java
for it, but it won't play a big role if I used C++; for some
processing the code ran and lasted around 2-3 days to finish; when I
decided to switch to HashMap it took less than 6 hours to process.
Were you using sorted vectors with a binary search? Or were you just
scanning through the vector? True, a hash map would still be faster
(in the average case) than a sorted vector, but I would like to know
how you used vectors in this case before I take your advice.
Vectors vs Maps, I also have the book of scott meyers and it's a great
one and I don't see a contradiction for using Maps or hashed maps; you
worry much about retrieving the elements fast and I guess Maps are
best suited for that.
Let me repeat again:
You need to ensure sorted elements -sets
you need uniqueness -sets and maps
you need fast lookup -maps and hashes
you need insertions and deletions from the beginning, ending or middle
of container -lists
you need random access -vectors
See what's most crucial for your application and decide for a
container.
Good luck
I think there is a little bit of misinformation here. Both std::map
and std::set are sorted, and they both have fairly fast (i.e. O(log
N)) lookup. Hash maps have even faster lookup (in the average case,
O(1)).
The reason that the OP might prefer a map over a set his case is that
he wants to modify the data in the records. While this can be done
with std::set (by storing a pointer to the record, and providing a
comparison operator), it isn't a natural fit for the problem.
However, as the data is already sorted, and if you will not be
inserting or deleting data, then using a std::vector is a fine idea.
To search a sorted vector, use std::lower_bound, which should do a
binary search (avoid std::binary_search, as it only returns a boolean
indicating containment).

Exactly my point Matt. The data I have is already sorted.
In short my operations involve
1) Put the sorted data into a container ( I dont have to sort the
data, its already in sorted form)
2) Provide a look up to the data to the user based on unique identity
3) May have to modify the data (the pointer is stored in the vector).
Modifying the data will not result in a resort as the identity will
retain the same, only the data pointed will be different)
4)As matt pointed out, i'm using a std::lower_bound for lookup with a
predicate logic that compares strings for equality.
Something like
lower_bound(vector_data.begin(),vector_data.end(), identity,DataCompare())
Where vector_data refers to vector containing a pair of
(identity,,pointer) and DataCompare is the predicate logic defining
equality of identity which is in this case a string
If you need a reference:http://www.sgi.com/tech/stl/lower_bound.html
-matt- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
A quick question on predicate logic used in lower_bound. Does it have
to be less than comparitor? What happens if we define the predicate to
be an equal to operator? How do i locate an entry in a vector
effectively. There always seems to be range check algorithms for STL
and not any lookup for vector,list et.al

Mar 21 '07 #13
"Hunk" <sa**************@gmail.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
A quick question on predicate logic used in lower_bound. Does it have
to be less than comparitor?
It has to be a strict weak ordering. < works, as does >; but none of
the other four comparisons are strict weak orderings.
What happens if we define the predicate to
be an equal to operator?
lower_bound will go mildly crazy.
How do i locate an entry in a vector
effectively.
Order the contents of the vector and search it using the same
ordering rule.
There always seems to be range check algorithms for STL
and not any lookup for vector,list et.al
The general algorithms work on pairs of iterators, including pairs
of container iterators. No need for specialized algorithms, in
general.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Mar 21 '07 #14
On Mar 21, 2:51 pm, "P.J. Plauger" <p...@dinkumware.comwrote:
"Hunk" <santosh.udyav...@gmail.comwrote in message

news:11*********************@n59g2000hsh.googlegro ups.com...
A quick question on predicate logic used in lower_bound. Does it have
to be less than comparitor?

It has to be a strict weak ordering. < works, as does >; but none of
the other four comparisons are strict weak orderings.
What happens if we define the predicate to
be an equal to operator?

lower_bound will go mildly crazy.
Yep it did go crazy when i put in an equal to operator;). I had a
sorted vector of strings and i tried to locate a string in that using
lower_bound with a predicate logic defined to return a string
comparison.
>
How do i locate an entry in a vector
effectively.

Order the contents of the vector and search it using the same
ordering rule.
There always seems to be range check algorithms for STL
and not any lookup for vector,listet.al

The general algorithms work on pairs of iterators, including pairs
of container iterators. No need for specialized algorithms, in
general.
I used the lower_bound to do a lookup on the vector items in the
following way
my_vector (contains a pair of identity,pointers) identity is a string
lower_bound(my_vector.begin(),my_vector.end(),iden tity,datacompare())
my datacompare defines a less than comparitor for strings

My question is is this better compared to a map ?
My operations are in the order mentioned below
1) Put an already sorted list in a container (sorted on unique
strings)
2) do some look ups
3) Update the data (This would not modify the string identifiers. Only
the data associated)

So for these set of operations would a vector be more efficient than a
map container?
P.J. Plauger
Dinkumware, Ltd.http://www.dinkumware.com

Mar 22 '07 #15
"Hunk" <sa**************@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
On Mar 21, 2:51 pm, "P.J. Plauger" <p...@dinkumware.comwrote:
>"Hunk" <santosh.udyav...@gmail.comwrote in message

news:11*********************@n59g2000hsh.googlegr oups.com...
A quick question on predicate logic used in lower_bound. Does it have
to be less than comparitor?

It has to be a strict weak ordering. < works, as does >; but none of
the other four comparisons are strict weak orderings.
What happens if we define the predicate to
be an equal to operator?

lower_bound will go mildly crazy.

Yep it did go crazy when i put in an equal to operator;). I had a
sorted vector of strings and i tried to locate a string in that using
lower_bound with a predicate logic defined to return a string
comparison.
>>
How do i locate an entry in a vector
effectively.

Order the contents of the vector and search it using the same
ordering rule.
There always seems to be range check algorithms for STL
and not any lookup for vector,listet.al

The general algorithms work on pairs of iterators, including pairs
of container iterators. No need for specialized algorithms, in
general.

I used the lower_bound to do a lookup on the vector items in the
following way
my_vector (contains a pair of identity,pointers) identity is a string
lower_bound(my_vector.begin(),my_vector.end(),iden tity,datacompare())
my datacompare defines a less than comparitor for strings

My question is is this better compared to a map ?
My operations are in the order mentioned below
1) Put an already sorted list in a container (sorted on unique
strings)
2) do some look ups
3) Update the data (This would not modify the string identifiers. Only
the data associated)

So for these set of operations would a vector be more efficient than a
map container?
If you're not going to change the ordering of the data by updating
it, then a vector works fine:

-- If you can really trust that the data ordered on the way in,
you don't even have to sort it.

-- lower_bound on a random-access series has the same logarithmic
time complexity as lookups in a (nearly) balanced binary tree.

-- vector has lower space overhead than map.

For my part, however, I'd sort the vector after filling it, just in
case. A decent sort will verify that a sequence is already in sort
in linear time.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Mar 22 '07 #16
On Mar 21, 1:45 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 20, 2:06 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 17, 2:58 am, "Matteo" <mah...@ncsa.uiuc.eduwrote:
On Mar 16, 11:29 am, "coosa" <coos...@gmail.comwrote:
Agree with you; a map or precisely a hashed map.
Trust me on not using vectors; I've worked on the netflix competition
with a dataset containing over 100 millions of ratings and I used Java
for it, but it won't play a big role if I used C++; for some
processing the code ran and lasted around 2-3 days to finish; when I
decided to switch to HashMap it took less than 6 hours to process.
Were you using sorted vectors with a binary search? Or were you just
scanning through the vector? True, a hash map would still be faster
(in the average case) than a sorted vector, but I would like to know
how you used vectors in this case before I take your advice.
Vectors vs Maps, I also have the book of scott meyers and it's a great
one and I don't see a contradiction for using Maps or hashed maps; you
worry much about retrieving the elements fast and I guess Maps are
best suited for that.
Let me repeat again:
You need to ensure sorted elements -sets
you need uniqueness -sets and maps
you need fast lookup -maps and hashes
you need insertions and deletions from the beginning, ending or middle
of container -lists
you need random access -vectors
See what's most crucial for your application and decide for a
container.
Good luck
I think there is a little bit of misinformation here. Both std::map
and std::set are sorted, and they both have fairly fast (i.e. O(log
N)) lookup. Hash maps have even faster lookup (in the average case,
O(1)).
The reason that the OP might prefer a map over a set his case is that
he wants to modify the data in the records. While this can be done
with std::set (by storing a pointer to the record, and providing a
comparison operator), it isn't a natural fit for the problem.
However, as the data is already sorted, and if you will not be
inserting or deleting data, then using a std::vector is a fine idea.
To search a sorted vector, use std::lower_bound, which should do a
binary search (avoid std::binary_search, as it only returns a boolean
indicating containment).
Exactly my point Matt. The data I have is already sorted.
In short my operations involve
1) Put the sorted data into a container ( I dont have to sort the
data, its already in sorted form)
2) Provide a look up to the data to the user based on unique identity
3) May have to modify the data (the pointer is stored in the vector).
Modifying the data will not result in a resort as the identity will
retain the same, only the data pointed will be different)
4)As matt pointed out, i'm using a std::lower_bound for lookup with a
predicate logic that compares strings for equality.
Something like
lower_bound(vector_data.begin(),vector_data.end(), identity,DataCompare())
Where vector_data refers to vector containing a pair of
(identity,,pointer) and DataCompare is the predicate logic defining
equality of identity which is in this case a string
If you need a reference:http://www.sgi.com/tech/stl/lower_bound.html
-matt- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -

A quick question on predicate logic used in lower_bound. Does it have
to be less than comparitor? What happens if we define the predicate to
be an equal to operator? How do i locate an entry in a vector
effectively. There always seems to be range check algorithms for STL
and not any lookup for vector,list et.al
Then again,
I stick to using a set since you can overload the greater operator or
the equal operator to make the comparison other than less operator ...

regards

Mar 23 '07 #17
In message <11*********************@l75g2000hse.googlegroups. com>, coosa
<co*****@gmail.comwrites
>On Mar 21, 1:45 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
>On Mar 20, 2:06 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 17, 2:58 am, "Matteo" <mah...@ncsa.uiuc.eduwrote:
On Mar 16, 11:29 am, "coosa" <coos...@gmail.comwrote:
Agree with you; a map or precisely a hashed map.
Trust me on not using vectors; I've worked on the netflix competition
with a dataset containing over 100 millions of ratings and I used Java
for it, but it won't play a big role if I used C++; for some
processing the code ran and lasted around 2-3 days to finish; when I
decided to switch to HashMap it took less than 6 hours to process.
Were you using sorted vectors with a binary search? Or were you just
scanning through the vector? True, a hash map would still be faster
(in the average case) than a sorted vector, but I would like to know
how you used vectors in this case before I take your advice.
Vectors vs Maps, I also have the book of scott meyers and it's a great
one and I don't see a contradiction for using Maps or hashed maps; you
worry much about retrieving the elements fast and I guess Maps are
best suited for that.
Let me repeat again:
You need to ensure sorted elements -sets
you need uniqueness -sets and maps
you need fast lookup -maps and hashes
you need insertions and deletions from the beginning, ending or middle
of container -lists
you need random access -vectors
See what's most crucial for your application and decide for a
container.
Good luck
I think there is a little bit of misinformation here. Both std::map
and std::set are sorted, and they both have fairly fast (i.e. O(log
N)) lookup. Hash maps have even faster lookup (in the average case,
O(1)).
The reason that the OP might prefer a map over a set his case is that
he wants to modify the data in the records. While this can be done
with std::set (by storing a pointer to the record, and providing a
comparison operator), it isn't a natural fit for the problem.
However, as the data is already sorted, and if you will not be
inserting or deleting data, then using a std::vector is a fine idea.
To search a sorted vector, use std::lower_bound, which should do a
binary search (avoid std::binary_search, as it only returns a boolean
indicating containment).
Exactly my point Matt. The data I have is already sorted.
In short my operations involve
1) Put the sorted data into a container ( I dont have to sort the
data, its already in sorted form)
2) Provide a look up to the data to the user based on unique identity
3) May have to modify the data (the pointer is stored in the vector).
Modifying the data will not result in a resort as the identity will
retain the same, only the data pointed will be different)
4)As matt pointed out, i'm using a std::lower_bound for lookup with a
predicate logic that compares strings for equality.
Something like
lower_bound(vector_data.begin(),vector_data.end(), identity,DataCompare())
Where vector_data refers to vector containing a pair of
(identity,,pointer) and DataCompare is the predicate logic defining
equality of identity which is in this case a string
If you need a reference:http://www.sgi.com/tech/stl/lower_bound.html
-matt- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -

A quick question on predicate logic used in lower_bound. Does it have
to be less than comparitor? What happens if we define the predicate to
be an equal to operator? How do i locate an entry in a vector
effectively. There always seems to be range check algorithms for STL
and not any lookup for vector,list et.al

Then again,
I stick to using a set since you can overload the greater operator or
the equal operator to make the comparison other than less operator ...
You can equally well do that on the sort and lower_bound algorithms
(make sure you use the same comparison for both!), so in that regard
they are no different from using an associative container.

--
Richard Herring
Mar 27 '07 #18
On Mar 27, 4:19 pm, Richard Herring <ju**@[127.0.0.1]wrote:
In message <1174638200.580634.57...@l75g2000hse.googlegroups. com>, coosa
<coos...@gmail.comwrites
On Mar 21, 1:45 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 20, 2:06 pm, "Hunk" <santosh.udyav...@gmail.comwrote:
On Mar 17, 2:58 am, "Matteo" <mah...@ncsa.uiuc.eduwrote:
On Mar 16, 11:29 am, "coosa" <coos...@gmail.comwrote:
Agree with you; a map or precisely a hashed map.
Trust me on not using vectors; I've worked on the netflix competition
with a dataset containing over 100 millions of ratings and I used Java
for it, but it won't play a big role if I used C++; for some
processing the code ran and lasted around 2-3 days to finish; when I
decided to switch to HashMap it took less than 6 hours to process.
Were you using sorted vectors with a binary search? Or were you just
scanning through the vector? True, a hash map would still be faster
(in the average case) than a sorted vector, but I would like to know
how you used vectors in this case before I take your advice.
Vectors vs Maps, I also have the book of scott meyers and it's a great
one and I don't see a contradiction for using Maps or hashed maps; you
worry much about retrieving the elements fast and I guess Maps are
best suited for that.
Let me repeat again:
You need to ensure sorted elements -sets
you need uniqueness -sets and maps
you need fast lookup -maps and hashes
you need insertions and deletions from the beginning, ending or middle
of container -lists
you need random access -vectors
See what's most crucial for your application and decide for a
container.
Good luck
I think there is a little bit of misinformation here. Both std::map
and std::set are sorted, and they both have fairly fast (i.e. O(log
N)) lookup. Hash maps have even faster lookup (in the average case,
O(1)).
The reason that the OP might prefer a map over a set his case is that
he wants to modify the data in the records. While this can be done
with std::set (by storing a pointer to the record, and providing a
comparison operator), it isn't a natural fit for the problem.
However, as the data is already sorted, and if you will not be
inserting or deleting data, then using a std::vector is a fine idea.
To search a sorted vector, use std::lower_bound, which should do a
binary search (avoid std::binary_search, as it only returns a boolean
indicating containment).
Exactly my point Matt. The data I have is already sorted.
In short my operations involve
1) Put the sorted data into a container ( I dont have to sort the
data, its already in sorted form)
2) Provide a look up to the data to the user based on unique identity
3) May have to modify the data (the pointer is stored in the vector).
Modifying the data will not result in a resort as the identity will
retain the same, only the data pointed will be different)
4)As matt pointed out, i'm using a std::lower_bound for lookup with a
predicate logic that compares strings for equality.
Something like
lower_bound(vector_data.begin(),vector_data.end(), identity,DataCompare())
Where vector_data refers to vector containing a pair of
(identity,,pointer) and DataCompare is the predicate logic defining
equality of identity which is in this case a string
If you need a reference:http://www.sgi.com/tech/stl/lower_bound.html
-matt- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
A quick question on predicate logic used in lower_bound. Does it have
to be less than comparitor? What happens if we define the predicate to
be an equal to operator? How do i locate an entry in a vector
effectively. There always seems to be range check algorithms for STL
and not any lookup for vector,list et.al
Then again,
I stick to using a set since you can overload the greater operator or
the equal operator to make the comparison other than less operator ...

You can equally well do that on the sort and lower_bound algorithms
(make sure you use the same comparison for both!), so in that regard
they are no different from using an associative container.

--
Richard Herring- Hide quoted text -

- Show quoted text -
Thanks a lot guys for the suggestions. It was a great help. Have
already tried out with vector, have to try out coosa suggestion of a
set.. but i think vector would anyway suffice unless there is an added
advantage in memory and speed in using a set

Mar 29 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave | last post by:
Hello all, I'm pondering why the default underlying container for std::priority_queue<> is std::vector<>. It would seem that inserts are liable to happen anywhere, which would make std::list<>...
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
1
by: J L | last post by:
I want to create a sorted list whose values are themselves sorted lists. I wrote the following simple test program but it does not behave as I would expect. What I wanted to do was have the...
4
by: shrishjain | last post by:
Hi All, I need a type where I can store my items in sorted order. And I want to keep adding items to it, and want it to remain sorted. Is there any type in .net which I can make use of. I see...
13
by: vd12005 | last post by:
Hello, i would like to sort(ed) and reverse(d) the result of many huge dictionaries (a single dictionary will contain ~ 150000 entries). Keys are words, values are count (integer). i'm...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
2
by: Aldarion | last post by:
for example, let from operator import itemgetter items = ), ('c', ), ('b', ), ('d', )] sorted(items, key = itemgetter(1)) get this back: ), ('a', ), ('b', ), ('d', )] but
16
by: Jon Harrop | last post by:
I need a data structure with the following capabilities: 1. Fast access to the smallest element. 2. Fast insertion of a new element. 3. Fast deletion of an existing element. I have been...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.