473,811 Members | 2,867 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 22124
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

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

Similar topics

4
2339
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 the code works: I have a stack consisting of nodes and arguments, much like a function execution stack. In this example there are only three nodes on the stack. For each run through the loop, a copy of the current sensation is written to the...
11
5050
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 Through suggestions from this news group I was able to exactly duplicate the interface of std::vector, except for one aspect. What I need is a way to invoke the destructor on elements of an array without de-allocating the memory of this array. This...
9
3616
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 even numbers. My questions is actullay I have to put pos++ in the body of the loop(also in if, else, statement). Can I somehow put it in for(...;...;...). Or can I use index instead of iterator in order to delete elements from
35
2654
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
2731
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 this purpose. However, I am also interested in having no duplicacy in elements of the vector. So I do not want to have any integer repeated more than once in the list. One very naive approach could be to compare the new integer being added to...
14
18588
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 iteration. I'm am trying to do something like: vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4);
10
3233
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 hash keys and when the program is shutting down it takes forever (10+ mins) while its deleting the objects. The memory it uses according to task manager is varies from 500,000 k then
5
12038
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 the function i 've written: void table::delel( int index ) { vector< record >::iterator rm = db.begin(); for( int i = 0; i < index; i++ )
12
2728
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
0
9734
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, 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...
0
9607
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,...
0
10662
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, 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...
1
10416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5567
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...
0
5702
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
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
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3028
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.