LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-01-2005, 01:38 PM   #1
g_goblin
Member
 
Registered: Oct 2002
Location: Chitown
Distribution: RH 7.2/3
Posts: 48

Rep: Reputation: 15
retreiving a range of iterators within an stl set


I have an issue where i need to be able to search an stl container for a given key and then retrieve the first three keys before that value and the first three keys after that value.


Example:

Say I have a set with {1,2,3,4,5,6,7,8,9,10}

I search for the value 5. I then want to retrieve 3 three keys before 5 and three keys after 5.

Is there any way to do that with and stl set or is there a more appropriate container for this?

TIA,

-g-

Last edited by g_goblin; 09-01-2005 at 01:39 PM.
 
Old 09-01-2005, 05:50 PM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Re: retreiving a range of iterators within an stl set

Quote:
Originally posted by g_goblin
I have an issue where i need to be able to search an stl container for a given key and then retrieve the first three keys before that value and the first three keys after that value.


Example:

Say I have a set with {1,2,3,4,5,6,7,8,9,10}

I search for the value 5. I then want to retrieve 3 three keys before 5 and three keys after 5.

Is there any way to do that with and stl set or is there a more appropriate container for this?

TIA,

-g-
Since order is important for your needs, I think using a container based on sequences would be more natural. A sorted vector is probably your best bet. You can use standard algorithms to sort and search it, and you can access the elements before and after the one you find fairly easily.

If you need to ensure uniquesness of elements (as in the set), just create a set, then use the copy algorithm to copy it to a vector, then sort the vector and you're good to go.

However, if you are going to be inserting more elements into the container at various intervals, this might not be the best way to go. It will vary with circumstance.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++:Operations with iterators frankie_DJ Programming 12 10-20-2005 03:39 AM
LVM2 on Sarge - HD failure in the volume group container /root Golovko Debian 3 06-20-2005 09:54 PM
vp6 codec in mkv container librano Linux - Software 0 10-19-2004 03:16 PM
Encrypted virtual drive/container magkakape Linux - Software 2 06-22-2004 07:27 AM
Qworkspace? and container? kalleanka Programming 0 03-01-2004 05:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration