Hello,
I am writing a program where I have a vector (std::vector<std:string> list)
that I need to search many times. To accomplish this efficiently, I plan to
sort the list using std::sort(list.begin(),list.end()), then run binary
searches. I need to get the indices of the elements found so I can
construct a matrix where I allocate a row of a matrix for each string in the
list (row one for the first item in the list, etc). However the binary
search algorithm in the STL returns an iterator that points to the object in
the vector rather than the index. Is there some way I can use STL to get
the index of an element without running a linear search?
Thanks,
Alex Gerdemann
University of Illinois at Urbana-Champaign 6 8830
Alex Gerdemann wrote: Hello,
I am writing a program where I have a vector (std::vector<std:string> list) that I need to search many times. To accomplish this efficiently, I plan to sort the list using std::sort(list.begin(),list.end()), then run binary searches. I need to get the indices of the elements found so I can construct a matrix where I allocate a row of a matrix for each string in the list (row one for the first item in the list, etc). However the binary search algorithm in the STL returns an iterator that points to the object in the vector rather than the index. Is there some way I can use STL to get the index of an element without running a linear search?
I believe that std:vector guatentees contiguous elements hence the
follwoing holds true:
std::vector<T> arr;
std::vector<T>::iterator i1;
....
int indexof_i1 = &(*i1) - &(*arr.begin());
(iff i1 is an iterator of the arr vector and the vector is not empty)
You can often use pointers instead of iterators in many stl algorithms.
If you're going to be searching for elements very frequently, it may be
that you can use a hashing algorithm and that might be faster.
Hope this helps.
"Alex Gerdemann" <nu*******@hotmail.com> wrote in message
news:_wK9d.358075$Fg5.220754@attbi_s53... I am writing a program where I have a vector (std::vector<std:string> list) that I need to search many times. To accomplish this efficiently, I plan to sort the list using std::sort(list.begin(),list.end()), then run binary searches. I need to get the indices of the elements found so I can construct a matrix where I allocate a row of a matrix for each string in the list (row one for the first item in the list, etc). However the binary search algorithm in the STL returns an iterator that points to the object in the vector rather than the index.
Actually, std::binary_search returns a bool telling whether the item
was found or not.
Is there some way I can use STL to get the index of an element without running a linear search?
Yes, you can use std::lower_bound in <algorithm>.
This one returns an iterator, which can readily be converted to an index:
index = lower_bound( list.begin(), list.end(), valToBeFound ) -
list.begin();
Note that if you are not sure that the string to be found is in the list,
you need to (1) verify that the resut of lower_bound is not list.end()
and (2) verify that the result is actually equal to the searched string.
Cheers,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
* Alex Gerdemann: I am writing a program where I have a vector (std::vector<std:string> list) that I need to search many times. To accomplish this efficiently, I plan to sort the list using std::sort(list.begin(),list.end()),
This looks like a bit of confusion regarding choice of data structure.
Do you want a vector, a list, or an efficiently searchable structure?
Is there some way I can use STL to get the index of an element without running a linear search?
Try a std::map, a std::multimap, a std::set or a std::multiset, depending on
what the requirements really are.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"Alex Gerdemann" <nu*******@hotmail.com> wrote in message
news:_wK9d.358075$Fg5.220754@attbi_s53... Hello,
I am writing a program where I have a vector (std::vector<std:string> list) that I need to search many times. To accomplish this efficiently, I plan to sort the list using std::sort(list.begin(),lis t.end,thenrunbinary searches. I need to get the indices of the elements found so I can construct a matrix where I allocate a row of a matrix for each string in the list (row one for the first item in the list, etc). However the binary search algorithm in the STL returns an iterator
You must be thinking of lower_bound, binary_search returns a bool.
that points to the object in the vector rather than the index. Is there some way I can use STL to get the index of an element without running a linear search?
Of course
int index = iter - list.begin();
Just subtract the iterator pointing to the beginning of a vector from
another iterator pointing to the vector and you have the index.
john
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41****************@news.individual.net... Is there some way I can use STL to get the index of an element without running a linear search?
Try a std::map, a std::multimap, a std::set or a std::multiset, depending on what the requirements really are.
I remember from personal experiments that searching a sorted vector
is typically faster than using any of these containers.
This was also reported by Scott Meyers and others (Andrei) IIRC.
The associative containers only make sense if the list changes often.
Of course, a hash table will work best, and while non-standard, hash_set
is available in some form on most platforms. [if the list of words is
known at compile-time, a tool like GNU gperf is an ultimate solution]
Regards,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Alex Gerdemann wrote: list (row one for the first item in the list, etc). However the binary search algorithm in the STL returns an iterator that points to the object in the vector rather than the index. Is there some way I can use STL to > get the index of an element without running a linear search?
index= std::distance (container.begin (), found);
--
Salu2 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ravi |
last post by:
I recently had to write some code which required me to use a binary
search tree (BST) due to running time concerns. I found that there is no
BST class in the Standard C++ library. I have been told...
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working,...
|
by: Hemang Shah |
last post by:
Hello fellow Coders!
ok, I"m trying to write a very simple application in C#. (Yes its my first
program)
What I want to do is :
1) Open a binary file
2) Search this file for a particular...
|
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...
|
by: Andrew |
last post by:
Hi, im trying to create a small function which can create a binary tree
from the entries in a text file and after that we can perform all the
usual operations like search, insert and delete etc....
|
by: free2cric |
last post by:
Hi,
I have a single link list which is sorted.
structure of which is like
typedef struct mylist
{
int num;
struct mylist *next;
|
by: Timmy |
last post by:
The bigger problem is with the Binary Search. The program crashes when it's
excuted. and Visual Studio 2005 indicates stack over flow and shows a break
at that function.
Sequential search is...
|
by: Vince Filby |
last post by:
Hi,
We are working with distributing Lucene.net. We have Master Index
Server which takes responsibility of distributing the index searching
to multiple Index Servers by calling the remote...
|
by: jhansi |
last post by:
create a binary file and insert the records and query on those inserted record..
queries are: query based on search by name,search by age and search by designation.
the problem is i have...
|
by: Atos |
last post by:
Binary search is used to locate a value in a sorted list of values.
It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
|
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...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |