Hi,
I have a vector of strings. I want to hold it only unique strings.
That is, before adding any new string, I want to check if it is already
present, and if yes, then I will not add the new item.
Is there a simpler and efficient way of doing it (instead of traversing
linearly through the whole list and comparing strings everytime I want to
add new item).
Thanks in advance,
Abhishek 5 4006
Abhishek Pandey wrote: Hi, I have a vector of strings. I want to hold it only unique strings. That is, before adding any new string, I want to check if it is already present, and if yes, then I will not add the new item. Is there a simpler and efficient way of doing it (instead of traversing linearly through the whole list and comparing strings everytime I want to add new item).
std::map should be an obvious alternative to vector.
Perhaps the best alternative would be a hash container, but that's not
part of the standard stl, although many stl implementations have one.
So, *must* it be a vector, or can you change the container ?
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:nN********************@speakeasy.net... Abhishek Pandey wrote: Hi, I have a vector of strings. I want to hold it only unique strings. That is, before adding any new string, I want to check if it is already present, and if yes, then I will not add the new item. Is there a simpler and efficient way of doing it (instead of traversing linearly through the whole list and comparing strings everytime I want
to add new item).
std::map should be an obvious alternative to vector.
Perhaps the best alternative would be a hash container, but that's not part of the standard stl, although many stl implementations have one.
So, *must* it be a vector, or can you change the container ?
Thanks for the reply,
Changing the container will require more code changes, which I was trying to
avoid if possible.
-Abhishek
Abhishek Pandey wrote: "Gianni Mariani" <gi*******@mariani.ws> wrote in message news:nN********************@speakeasy.net... Abhishek Pandey wrote: Hi, I have a vector of strings. I want to hold it only unique strings. That is, before adding any new string, I want to check if it is already present, and if yes, then I will not add the new item. Is there a simpler and efficient way of doing it (instead of traversing linearly through the whole list and comparing strings everytime I want to add new item).
std::map should be an obvious alternative to vector.
Perhaps the best alternative would be a hash container, but that's not part of the standard stl, although many stl implementations have one.
So, *must* it be a vector, or can you change the container ?
Thanks for the reply, Changing the container will require more code changes, which I was trying to avoid if possible.
How do you build up the vector?
Is it built in one rush or are things added as needed?
If the former then a possible solution is:
Just add all of the strings (you will have duplicates now).
Then sort the vector. In this way all the duplicates will be
at consecutive vector positions. Walk through the vector and
remove the duplicates.
Look up: std::sort std::unique
--
Karl Heinz Buchegger kb******@gascad.at
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at... Abhishek Pandey wrote: "Gianni Mariani" <gi*******@mariani.ws> wrote in message news:nN********************@speakeasy.net... Abhishek Pandey wrote: > Hi, > I have a vector of strings. I want to hold it only unique strings. > That is, before adding any new string, I want to check if it is
already > present, and if yes, then I will not add the new item. > Is there a simpler and efficient way of doing it (instead of
traversing > linearly through the whole list and comparing strings everytime I
want to > add new item).
std::map should be an obvious alternative to vector.
Perhaps the best alternative would be a hash container, but that's not part of the standard stl, although many stl implementations have one.
So, *must* it be a vector, or can you change the container ? Thanks for the reply, Changing the container will require more code changes, which I was
trying to avoid if possible.
How do you build up the vector? Is it built in one rush or are things added as needed?
If the former then a possible solution is: Just add all of the strings (you will have duplicates now). Then sort the vector. In this way all the duplicates will be at consecutive vector positions. Walk through the vector and remove the duplicates.
I add the things as and when needed and not in one go. :-(
Thanks
Look up: std::sort std::unique
-- Karl Heinz Buchegger kb******@gascad.at
make sure the vector is sorted at all times, then before you insert, do
a binary search.
or just use std::set.
If you cannot change the order of elements in the vector, then maintain
a separate std::set using the minimal data needed to define uniqueness.
-shez- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lallous |
last post by:
Hello,
This code works fine when 'size' is less than 32768 however when size is
bigger this function never returns.
Can't find out why?!
If I break into the code I can see that 'i' is 32768.......
|
by: Adam Hartshorne |
last post by:
Hi All,
I have the following problem, and I would be extremely grateful if
somebody would be kind enough to suggest an efficient solution to it.
I create an instance of a Class A, and...
|
by: gouqizi.lvcha |
last post by:
Hi all
I have a large vector with float point numbers in it, for example
(1.1, 2.1 , 3.2 , 3.3 , 4, 6, 3.2, 8). Is there an easy way to
determine
how many uique elements in the array?
Thanks...
|
by: Rich S. |
last post by:
Hi
In an earlier posting I was asking how to read thru millions of data records
keeping the top 2000 (where the top values are based upon a field in the
record) and niklasb suggested using a...
|
by: DotNetJunky |
last post by:
I have built a control that runs an on-line help system. Depending on the
category you selected via dropdownlist, it goes out and gets the child
subcategories, and if there are any, adds a new...
|
by: Suma |
last post by:
A newbie question :
I have a vector of objects(pointers) . I have a function which
overloads the less than operator . I can sort the objects without a
problem. However when I pass the same...
|
by: silversurfer |
last post by:
Hello,
I am a little unsure whether this method really makes sense. The goal
is to add an element to a vector. This is the struct and method I am
using:
std::vector<Entry> models;
struct...
|
by: oktayarslan |
last post by:
Hi all;
I have a problem when inserting an element to a vector. All I want is
reading some data from a file and putting them into a vector. But the
program is crashing after pushing a data which...
|
by: drjay1627 |
last post by:
hello,
This is my 1st post here!
*welcome drjay*
Thanks! I look answering questions and getting answers to other!
Now that we got that out of the way. I'm trying to read in a string and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |