473,511 Members | 16,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete duplicate elements in vector

48 New Member
Hi,

vector<string> str;

I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.

Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".

How can I do that??

Please help

Thanks!!
Sep 14 '07 #1
10 22088
sicarie
4,677 Recognized Expert Moderator Specialist
Hi,

vector<string> str;

I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.

Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".

How can I do that??

Please help

Thanks!!
I would recommend using a second vector and a for loop to iterate through the elements (vector iterator). Then you can compare the current vector against all the elements of the second vector. Does that make sense, or do I need to get into a bit more detail?
Sep 14 '07 #2
forumsaregreat
9 New Member
Hi,

vector<string> str;

I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.

Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".

How can I do that??

Please help

Thanks!!
Do you really need a vector ??
If you have to eliminate duplicates then using a set is most useful as every element is added only once.

/ Sid
Sep 14 '07 #3
flavourofbru
48 New Member
Hi Sicarie,

Can you please give me more details on what you have said, as I am not able to exactly grasp what you meant.

I have a large set of code where I have been using vectors, so now I will not be able to switch from vectors to sets.

What is the possible way to achieve this using vectors??

Thanks!!
Sep 14 '07 #4
sicarie
4,677 Recognized Expert Moderator Specialist
Hi,

vector<string> str;

I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.

Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".

How can I do that??

Please help

Thanks!!
my algorithm would probably look something like this
Expand|Select|Wrap|Line Numbers
  1. vector str
  2. vector str_noDuplicates
  3. str Iterator
  4. element_str
  5. element_strNoDuplicates
  6.  
  7. for each element in str
  8.     element_str = next element
  9.     for each element in str_noDuplicates
  10.         element_strNoDuplicates = next element
  11.         if element_str == element_strNoDuplicates
  12.             set duplicateFlag to true
  13.         end if
  14.     end for
  15.     if duplicateFlag == false
  16.         add element_strNoDuplicates to str_noDuplicates
  17.     else
  18.         do nothing
  19. end for
  20.  
Does that help?
Sep 14 '07 #5
RRick
463 Recognized Expert Contributor
This is where the builtin STL functions come in handy.

First you sort the array and then use unique to remove adjacent duplicates.
Expand|Select|Wrap|Line Numbers
  1. vector<int> ivec;
  2.  
  3. .....  add all the entries you like ......
  4.  
  5. sort( ivec.begin(), ivec.end());
  6. unique( ivec.begin), ivec.end());
  7.  
I'm not going to say this is efficient because removing entries in the middle of a vector can be expensive.
Sep 14 '07 #6
sicarie
4,677 Recognized Expert Moderator Specialist
This is where the builtin STL functions come in handy.

First you sort the array and then use unique to remove adjacent duplicates.
Expand|Select|Wrap|Line Numbers
  1. vector<int> ivec;
  2.  
  3. .....  add all the entries you like ......
  4.  
  5. sort( ivec.begin(), ivec.end());
  6. unique( ivec.begin), ivec.end());
  7.  
I'm not going to say this is efficient because removing entries in the middle of a vector can be expensive.
Psh, well, if you want to do it the easy way.

@RRick - you ever going to answer my PM? ;)
Sep 14 '07 #7
RRick
463 Recognized Expert Contributor
Ironically, I have never used these functions, but I have a very good book.

A lot of us get stuck at the container level of STL and rarely venture beyond the iterators and containers. I consider the STL algorithms/functions to be the next step in my STL coding.
Sep 15 '07 #8
gpkaur
1 New Member
There is no unique function available for vectors in STL.
It is for list
Jan 25 '08 #9
hsn
237 New Member
if you want to do what you want to do by using vectors it will take you a while to do it. also it will be slow way.
by using set you only enter the values to the set and it will have only unique values.
for example
if you entered 'a' 'b' 'a' 'c'
in the set it will be 'a' 'b' 'c'. without calling any functions.

GOOD LUCK
Jan 25 '08 #10
RRick
463 Recognized Expert Contributor
Gpkaur, there is an STL unique function, but it's found in the <algorithm> header file, not in each object. List is "special" because it does have its own unique function.

The concept of the algorithm functions is that they work for all containers. Separate containers add their own algorithms when a specific need (usually speed) is required.
Jan 25 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2320
by: Martin Magnusson | last post by:
I'm getting segmentation faults when trying to fix a memory leak in my program. The problem is related to lists of pointers which get passed around between objects. Here is a description of how...
11
5019
by: Peter Olcott | last post by:
I have just built a class that provides the most useful subset of std::vector functionality for use by compilers that lack template capability. http://home.att.net/~olcott/std_vect.html ...
9
3592
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
35
2595
by: Jon Slaughter | last post by:
I'm having a problem allocating some elements of a vector then deleting them. Basicaly I have something like this: class base { private: std::vector<object> V;
3
2712
by: Amit | last post by:
Hi, I have a list of integers. At each iteration, I remove some element from it and then insert new elements in it. The order of elements is not important. So I guess I could use a vector also for...
14
18565
by: cayblood | last post by:
I want to iterate through a vector and erase elements that meet a certain criteria. I know there is an algorithmic way of doing this, but first I would like to know how to do it with normal...
10
3203
by: Whybother | last post by:
I have this typedef map<__int64, int> Results_Map; __int64 is defined as 8 bytes ranging from -9,223,372,036.854,775,808 to 9,223,372,036.854,775,807 I have loaded it with approx 34 million...
5
12021
by: streamkid | last post by:
i have a class table, which has a vector of records(-db). i 'm trying to remove an element, but it doesn't seem to work.. i read this http://www.cppreference.com/cppvector/erase.html] and that's...
12
2684
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
0
7245
marktang
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,...
0
7144
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...
0
7356
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,...
0
5671
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,...
1
5069
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...
0
3227
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...
0
1577
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 ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
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...

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.