473,325 Members | 2,442 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,325 software developers and data experts.

Adding unique data to a vector

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
Jul 22 '05 #1
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 ?
Jul 22 '05 #2

"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
Jul 22 '05 #3
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
Jul 22 '05 #4

"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

Jul 22 '05 #5
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-

Jul 22 '05 #6

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

Similar topics

5
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.......
34
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...
3
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...
9
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...
4
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...
2
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...
2
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...
10
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...
8
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...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
0
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...
1
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)...
1
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...
0
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
0
isladogs
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...

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.