473,800 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Taking out duplicate items in a vector

12 New Member
Hello, I need some help with vectors . . . again. I am pushing component "types" from one vector into another vector. That part is fine. Then what I'm trying to do is take the items in the new vector and "weed out" the duplicate entries by pushing unique values into, yet, another vector. For some reason, it's pushing some of the duplicate items in the newest vector anyway. Here is my code. NOTE: Not all of the code is listed here, just the little part I am having trouble with so the {} may not all add up. Any help is greatly appreciated.
Expand|Select|Wrap|Line Numbers
  1.     int total = static_cast<int>(vComp.size()) - 1;
  2.     int tTotalUnits = 0;
  3.     float tTotalCostJob = 0;
  4.     int i = 0;
  5.     int j = 0;
  6.     int counter = 0;
  7.     bool foundFlag;
  8.  
  9.     vector<string> vItems;
  10.     vector<string> vHolding;
  11.  
  12.     for(i = 0; i < total; ++i)
  13.     {
  14.     // Push components into the vItems vector
  15.        vHolding.push_back(vComp[i].GetType());
  16.  
  17.     if(vItems.empty())
  18.     {
  19.         vItems.push_back(vComp[i].GetType());
  20.     }
  21.  
  22.         for (j = 0; j < vItems.size(); j++)
  23.         {
  24.             if(vHolding[i] == vItems[j])
  25.             {
  26.                 foundFlag = true;
  27.             }
  28.             else
  29.             {
  30.                 foundFlag = false;
  31.             }
  32.         }
  33.         if(foundFlag == false)
  34.         {
  35.             vItems.push_back(vHolding[i]);
  36.         }
  37. }
  38.  
  39.  
Mar 16 '08 #1
4 2446
weaknessforcats
9,208 Recognized Expert Moderator Expert
Is there a reason why you cannot use a set container? That way you aren't allowed uplicates in the first place.

I mean, if your vector has 500,000 entires, you will have to scan all 500,000 just to add a new unique item. Not good.
Mar 16 '08 #2
ejack
12 New Member
How do you use a set container? I'm very new to C++ Programming. Thank you for your help.
Mar 16 '08 #3
ejack
12 New Member
I guess I had better mention that I am in a C++ class and we haven't learned how to use set containers yet. My vector will hold no more than 15 items so is there a way to do it using vectors?

Thank you for your help.l
Mar 16 '08 #4
ejack
12 New Member
Well, it took a week, but I figured it out on my own. Thanks anyway.
Mar 16 '08 #5

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

Similar topics

44
4074
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are identical with which. Write a program that prints out which file are redundant copies. Here's the spec. -------------------------- The program is to be used on the command line. Its arguments are one or
1
4240
by: pcb | last post by:
Hi All, I have a multi-select listbox, that can have duplicate items (i.e samples numbers that are analyzed in duplicate - the sample IDs are not the same, but these IDs are hidden from the user). I want to loop through the items on the listbox, but only use unique sample numbers. Here is what I have:
10
3305
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
4
2838
by: gurvar | last post by:
Hi I'm trying to remove duplicate elements from a Drop Down List Fill in VB.net. Following code worked well with vb6. But I'm getting index out of range if I try to translate it to vb.net code at line#4. Any possible clues will be appreciated. Thanks. 'To distill the Combo Element NumY = Combo5.ListCount + 1 '''''''''''''''Line1 For NumX = 1 To Combo5.ListCount '''''''line2
4
3634
by: Shi Mu | last post by:
I used the following method to remove duplicate items in a list and got confused by the error. >>> a , , ] >>> noDups='] ] Traceback (most recent call last): File "<interactive input>", line 1, in ? TypeError: iterable argument required
3
2477
by: Jim Langston | last post by:
I really am not sure if this question belongs in this newsgroup, but not sure where else to ask it. There is someone working on a game that I tested, and it was taking >30 seconds to load. He stated that everyone else was taking 2 or 3 seconds. Then he found one other person taking >30 seconds, and it turns out the common denominator was both of us have Intel chips (Celeron) where the other people have AMD. I had him send me his code...
1
3486
by: gaikokujinkyofusho | last post by:
Hi, I have been enjoying being able to subscribe to RSS (http://kinja.com/user/thedigestibleaggie) for awhile and have come up with a fairly nice list of feeds but I have run into an annoying (though not critical) problem, duplicate stories. Apparently there is overlap with some of the sites I subscribe to so I get duplicate stories. Does anyone know of some sort of filter (software or online service) that can remove duplicate stories? Any...
3
2098
by: John Shell | last post by:
Hello, all. The following code results in a C2666 error (2 overloads have similar conversions). class FSVec2D { public: FSVec2D() { // code omitted }
10
22122
by: flavourofbru | last post by:
Hi, vector<string> str; I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a". i.e str = a; str = b; str = 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".
0
9690
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
9551
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
10504
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...
0
10274
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10251
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
6811
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();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.