473,651 Members | 3,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

searching a sorted list

Hello,

I have a string list of the format

rangeA rangeB 001
rangeA rangeB1002
rangeA rangeC 003
rangeB rangeC 004

This list is sorted according to the ranges in column 1. Now i have a
value rangeA1B. What is the efficient way to search this value? I need
to select all the three rangeA in the search. Using std::search or
std::find is a best choice here? Or if i need to write any algorithm,
what is that?

Though this seems like a programming question, i have posted here
because i need to write the solution in c++.

Thanks,
Balaji.
Aug 23 '08 #1
6 1571
ka************* ******@gmail.co m wrote:
Hello,

I have a string list of the format

rangeA rangeB 001
rangeA rangeB1002
rangeA rangeC 003
rangeB rangeC 004

This list is sorted according to the ranges in column 1. Now i have a
value rangeA1B. What is the efficient way to search this value? I need
to select all the three rangeA in the search. Using std::search or
std::find is a best choice here? Or if i need to write any algorithm,
what is that?

Though this seems like a programming question, i have posted here
because i need to write the solution in c++.
Well, then your answer can be found in the FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

and

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Because the solution is independent of the language.
Aug 23 '08 #2
ka************* ******@gmail.co m wrote:

[snip]
I have a string list of the format

rangeA rangeB 001
rangeA rangeB1002
rangeA rangeC 003
rangeB rangeC 004

This list is sorted according to the ranges in column 1. Now i have a
value rangeA1B. What is the efficient way to search this value? I need
to select all the three rangeA in the search. Using std::search or
std::find is a best choice here? Or if i need to write any algorithm,
what is that?
[snip]
You might consider storing your data as a std::multimap. The first column
would be the key_type and the second and third need to be combined into the
mapped_type. Then, you could use lower_bound() and upper_bound().
Best

Kai-Uwe Bux
Aug 23 '08 #3
On Aug 23, 12:02*am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
kasthurirangan. bal...@gmail.co m wrote:

[snip]I have a string list of the format
rangeA rangeB 001
rangeA rangeB1002
rangeA rangeC 003
rangeB rangeC 004
This list is sorted according to the ranges in column 1. Now i have a
value rangeA1B. What is the efficient way to search this value? I need
to select all the three rangeA in the search. Using std::search or
std::find is a best choice here? Or if i need to write any algorithm,
what is that?

[snip]

You might consider storing your data as a std::multimap. The first column
would be the key_type and the second and third need to be combined into the
mapped_type. Then, you could use lower_bound() and upper_bound().

Best

Kai-Uwe Bux
Hello Kai,

Thanks. Your answer seems to be an apt fit. Actually, i was planning
to use multimap, but the key being a pair of the ranges and was
looking for already available algorithms where i can pass a predicate,
rather than reinvent. Once again, thanks to you.

Thanks,
Balaji.
Aug 23 '08 #4
On Aug 22, 11:08*pm, red floyd <no.spam.h...@e xample.comwrote :
kasthurirangan. bal...@gmail.co m wrote:
Hello,
I have a string list of the format
rangeA rangeB 001
rangeA rangeB1002
rangeA rangeC 003
rangeB rangeC 004
This list is sorted according to the ranges in column 1. Now i have a
value rangeA1B. What is the efficient way to search this value? I need
to select all the three rangeA in the search. Using std::search or
std::find is a best choice here? Or if i need to write any algorithm,
what is that?
Though this seems like a programming question, i have posted here
because i need to write the solution in c++.

Well, then your answer can be found in the FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

and

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

Because the solution is independent of the language.- Hide quoted text -

- Show quoted text -
Hello Red,

Special thanks to you for pointing me that.:-) Re-read again.

Actually i was looking for solution in c++(something already
available, rather than reinvent). I was also planning to post in
comp.programmin g but didn't want to cross/double post in the beginning
itself.

Thanks,
Balaji.
Aug 23 '08 #5
ka************* ******@gmail.co m wrote in news:9dc89336-659c-4aed-89f8-
34**********@56 g2000hsm.google groups.com:
Hello,

I have a string list of the format

rangeA rangeB 001
rangeA rangeB1002
rangeA rangeC 003
rangeB rangeC 004

This list is sorted according to the ranges in column 1. Now i have a
value rangeA1B. What is the efficient way to search this value? I need
to select all the three rangeA in the search. Using std::search or
std::find is a best choice here? Or if i need to write any algorithm,
what is that?

Though this seems like a programming question, i have posted here
because i need to write the solution in c++.

Thanks,
Balaji.
If you are sorted by all three fields, then you should be able to use
std::lower_boun d() which has a predicate version. This will do a binary
search on the data.

Hope that helps,
joe
Aug 23 '08 #6
ka************* ******@gmail.co m wrote:
On Aug 23, 12:02*am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>kasthurirangan .bal...@gmail.c om wrote:

[snip]I have a string list of the format
rangeA rangeB 001
rangeA rangeB1002
rangeA rangeC 003
rangeB rangeC 004
This list is sorted according to the ranges in column 1. Now i have a
value rangeA1B. What is the efficient way to search this value? I need
to select all the three rangeA in the search. Using std::search or
std::find is a best choice here? Or if i need to write any algorithm,
what is that?

[snip]

You might consider storing your data as a std::multimap. The first column
would be the key_type and the second and third need to be combined into the
mapped_type. Then, you could use lower_bound() and upper_bound().

Best

Kai-Uwe Bux

Hello Kai,

Thanks. Your answer seems to be an apt fit. Actually, i was planning
to use multimap, but the key being a pair of the ranges and was
looking for already available algorithms where i can pass a predicate,
rather than reinvent. Once again, thanks to you.
It's really starting to look like the prof found a problem with no
answer posted. Sorry.

Aug 23 '08 #7

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

Similar topics

57
3591
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
1
1317
by: michael Rogowski | last post by:
I've been looking for a fast search-algorithm to find a point x/y in a ordered stl-list. How can I find sources?
8
2353
by: Gordon Knote | last post by:
Hi can anyone tell me what's the best way to search in binary content? Best if someone could post or link me to some source code (in C/C++). The search should be as fast as possible and it would be great if the engine (or so) would accept multiple parameters (like a search offset, a max number of bytes to search in etc.) Any ideas Thanks a lot again Gordon
1
1633
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 doorConflictList be keyed on a door ID and contain a sorted list called conFlictList. I wrote this expecting the following doorConflictList should end up with 3 items. Item 1 would be a sorted list with 1 item
6
1825
by: AAJ | last post by:
Hi all I have some objects that I add to an ArrayList. Each object is from the same class and has methods etc. Each can be identified by a unique property, in this case PK e.g. PK =1 for Instance1.PK PK =2 for Instance2.PK
4
1849
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 there is SortedList<key, value> for hash tables, but could find anything for a sorted list. Currently I am using List<string> and whenever I add an item, I need to
4
5336
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given the Identifier. The logical way would be to read the record once and put it in an STL container such as vector and then use lower_bound to search for a given identifier. But for some strange reason i'm asked to not use a container but instead...
20
2414
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001: ,
5
3179
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.