Taking out duplicate items in a vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ejack
    New Member
    • Mar 2008
    • 12

    Taking out duplicate items in a vector

    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.
    Code:
    	int total = static_cast<int>(vComp.size()) - 1;
    	int tTotalUnits = 0;
    	float tTotalCostJob = 0;
    	int i = 0;
    	int j = 0;
    	int counter = 0;
    	bool foundFlag;
    
    	vector<string> vItems;
    	vector<string> vHolding;
    
    	for(i = 0; i < total; ++i)
    	{
    	// Push components into the vItems vector
    	   vHolding.push_back(vComp[i].GetType());
    
    	if(vItems.empty())
    	{
    		vItems.push_back(vComp[i].GetType());
    	}
    
    		for (j = 0; j < vItems.size(); j++)
    		{
    			if(vHolding[i] == vItems[j])
    			{
    				foundFlag = true;
    			}
    			else
    			{
    				foundFlag = false;
    			}
    		}
    		if(foundFlag == false)
    		{
    			vItems.push_back(vHolding[i]);
    		}
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • ejack
      New Member
      • Mar 2008
      • 12

      #3
      How do you use a set container? I'm very new to C++ Programming. Thank you for your help.

      Comment

      • ejack
        New Member
        • Mar 2008
        • 12

        #4
        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

        Comment

        • ejack
          New Member
          • Mar 2008
          • 12

          #5
          Well, it took a week, but I figured it out on my own. Thanks anyway.

          Comment

          Working...