Thread: Efficiently scanning through a vector.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147

    Efficiently scanning through a vector.

    Hello all, again.

    I have the following code that searches a vector for a particular item. If it exists already in the vector, it doesn't add the current entry. Otherwise, it adds it:

    Code:
    if(mMapLinks.size() == 0)
    	mMapLinks.push_back(new mapLink(mFilesystem, target, type, x, y, width, height, dstx, dsty));
    else
    {
    	vector<mapLink*>::iterator i = mMapLinks.begin();
    	bool exists = false;
    	for(; i != mMapLinks.end(); i++)
    	{
    		if((*i)->getTargetMap() == target)
    		{
    			exists = true;
    			break;
    		}
    	}
    
    	if(!exists)
    		mMapLinks.push_back(new mapLink(mFilesystem, target, type, x, y, width, height, dstx, dsty));
    }
    where mMapLinks is defined as 'vector<mapLink*> mMapLinks' and target is defined as 'string'.

    This code does the job but feels clunky and potentially inefficient. I'd like to stick with a vector as I need very fast linear iteration although I'm not opposed to other suggestions.
    Last edited by leeor_net; 09-24-2009 at 08:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM