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 4037
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*******@mari ani.ws> wrote in message
news:nN******** ************@sp eakeasy.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*******@mari ani.ws> wrote in message news:nN******** ************@sp eakeasy.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******@gasca d.at> wrote in message
news:41******** *******@gascad. at... Abhishek Pandey wrote: "Gianni Mariani" <gi*******@mari ani.ws> wrote in message news:nN******** ************@sp eakeasy.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....
void MakeRandomArray(unsigned long **a, unsigned long size)
{
unsigned long *data = new unsigned long ;
double sizef = (double)(size - 1);
|
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 "push_back" a copy of this into a
vector V. This is repeated many times in an iterative process.
Ok whenever I "push_back" a copy of Class A, I also want to assign a
pointer contained in an exisiting instance of a Class B to this
|
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
Rick
|
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 priority_queue like so
------------------- start old message ---------------------
A more efficient way than what you describe would be
to use priority_queue. Define the predicate in such
|
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 dropdownlist to the screen
for selection. This continues until there are no children, and then it
checks for a help article list based on that last selection and displays
actual articles for display.
Adding the controls and getting everything...
| |
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 function as an arg to the unique
function, I still see the duplicate objects.
Shouldnt unique be aple to operate with just the less_than overload-
since if both the predicates constant1 < constant2 and constan2 <
constant1 return false implies...
|
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 Entry{
int index;
FeatureVector* value;
|
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 has string value. I
really do not understand why push_back() function is trying to remove
previously inserted data.
Thanks for any help
|
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 add the unique words in the string to a map. Eg:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
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...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |