Thread: List and Vector

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    List and Vector

    What are the differences between list and vector? Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One is usually spelt with 4 letters, and the other with 6 letters. For other differences, you could try reading SGI's Standard Template Library Programmer's Guide.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Depends on their implementations....

    Generally Linked Lists are not contiguous in memory and thus require require linear access time in the worst case. Vectors can provide constant time access, but that depends on the implementation. Both are dynamically sized, but are used in different situations depending on the intented use.

    Perhaps you could expand your question a little.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by laserlight
    One is usually spelt with 4 letters, and the other with 6 letters.

    Man, how could I miss that! Back to the books for me....

  5. #5
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Unhappy

    By the way, why am i having a error here? And is it apropriate to use list or vector in this situation?
    Code:
    list<string> Tokens;
    	buf.erase((buf.length()-1), 1);
    	Tokenize(buf, Tokens, "(");
    	list<unsigned long int> Sizes;
    	Tokens.pop_front();
    
    	/*list<string>::const_iterator it;
    	for (;it != Tokens.end();it++)
    	{
    		Sizes.push_back(atoi((*it).c_str()));
    	}*/ //REMOVED CONST ITERATOR -- NOT WORKING
    
    	while (Tokens.size() > 0)
    	{
    		Sizes.push_back(atoi(Tokens.front().c_str()));
    		Tokens.pop_front();
    	}
    	int index = 0;
    	// A MUST HAVE ITERATOR BUT NOT WORKING AS WELL
    	for (list<unsigned long int>::const_iterator it;it != Sizes.end();it++)
    	{
    		buf = sock->ReceiveLine();
    		if (buf[0] == '.')
    			break;
    
    		Email EmailBuf;
    		EmailBuf.ID = buf[0];
    		if (index == buf[0])
    			break;
    
    		buf = "RETR ";
    		buf += EmailBuf.ID;
    		sock->SendLine(buf);
    
    		sock->ReceiveLine(buf, (*it));
    	}
    The error i get is: List Iterators incompatible
    MS Visual C++ 2005, Windows XP Professional SP2

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The only problem I see with that code is that you never assigned the variable "it" to Tokens.begin() or Sizes.begin(), which is necessary when you iterate. Fix that, then give both iterators meaningful and different names and see if it works and what the error is that you get if any.

    You generally want to use vector by default. One reason to use list is if you are inserting and erasing in the middle of the container a lot, which you don't appear to be doing here. So I would use vector.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Thank you, i got some lack of attention, didn&#180;t realized it was unassigned

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Containers: List vs Vector
    By PetrolMan in forum C++ Programming
    Replies: 8
    Last Post: 04-09-2009, 02:14 AM
  2. Connection between 2D array and list vector?
    By glo in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2008, 01:53 PM
  3. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  4. set, vector & list
    By Luigi in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2002, 10:22 AM
  5. vector and list
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2001, 01:56 PM