Thread: Containers

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by King Mir View Post
    So in most cases list is a bad choice.
    I don't think I've actually ever used one in anything real. Just trying to temper my (hopefully obvious) bias toward std::vector.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #17
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    well, even though theoratically std::list should be faster than std::vector, in practice this theory breaks down.

    std::vector is contiguous, which gives it the following features vs. std::list:
    1) you use one packed memory block over many small pieces - the heap doesn't get fragmanted like std::list makes it
    2) there is no pointers involved so each object stay as lean as the sum of it's inner members - smaller size = more memory available
    3) because the vector is packed, it works much much better with the cache than std::list. the amount of cache hits with std::vector is significally higher than std::list.

    de-facto, you will probably use std::vector in most of the cases.

    but for the original quesion - use whatever you need for the job. I use the same amount of std::vector /std::map/std::unordered_map/std::set in my code. do you use more cups than glasses? this question is pointless.

    EDIT:
    I will admit that I don't use tuples so much. the compile-time-constaint is too much annoying that simply pack the members in one class
    Last edited by Dave11; 10-11-2015 at 03:11 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, a former lecturer of mine once recommended std::list over std::vector for a default container for general purpose lists in his website on competitive programming (he taught, and perhaps still teaches, a module dedicated to the topic), on the basis that insertion takes constant time once you have an iterator to the insertion point, whereas std::vector only takes amortised constant time for appending. I never got round to asking him if he really had evidence that this mattered more than other pros of using std::vector for the various competitive programming problems that he was concerned about.
    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

  4. #19
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    well, yes, accademically speaking.
    std::vector has some optimizations tricks deep inside like expanding the capacity by 1.5* when reallocation happens, and many more.
    and also, seasoned C++ developer will reserve the memory if he know even remotly what is the needed size. so that REALLY makes everything faster.
    overall, the cons are much less than the pros, at least for most of the cases..

    EDIT: this have being said, proper benchmarking is the right thing to do

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dave11
    well, yes, accademically speaking.
    std::vector has some optimizations tricks deep inside like expanding the capacity by 1.5* when reallocation happens, and many more.
    The "optimization tricks deep inside" are necessary to meet the standard's requirement of amortised constant time, though the exact factor is implementation defined, i.e., it need not be 1.5, and it is possible that the implementation might not do this if the container is sufficiently small or sufficiently large (i.e., expanding by a fixed amount might be better as expanding by a factor might be too small or consume too much potentially unused space).

    Quote Originally Posted by Dave11
    seasoned C++ developer will reserve the memory if he know even remotly what is the needed size. so that REALLY makes everything faster.
    Stroustrup recommends against using this optimisation unnecessarily:
    Quote Originally Posted by Bjarne Stroustrup
    People sometimes worry about the cost of std::vector growing incrementally. I used to worry about that and used reserve() to optimize the growth. After measuring my code and repeatedly having trouble finding the performance benefits of reserve() in real programs, I stopped using it except where it is needed to avoid iterator invalidation (a rare case in my code). Again: measure before you optimize.
    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

  6. #21
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    I wrote the * on top of 1.5 because I wanted to a comment ("on MSVC++") but I missed that.

    and about reserve - I'm talking about extremly big vector size, like <1000 elements, for example.

  7. #22
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think all these implementation details of current machines are ruining the pure abstraction of list vs vector.

  8. #23
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19
    Quote Originally Posted by Dave11 View Post
    std::vector has some optimizations tricks deep inside like expanding the capacity by 1.5* when reallocation happens, and many more.
    and also, seasoned C++ developer will reserve the memory if he know even remotly what is the needed size. so that REALLY makes everything faster.
    I'm talking about extremly big vector size, like <1000 elements, for example.
    How about multi dimensional vectors? More to the point... Does it (and how so) effect the compiler if you have your extremely large vector of 1000 elements, vs a two dimensional vector of 100x100, or even a 3 dimensional vector of 333x3 of say... type string... To me, 1000 elements is 1000 elements... how would this be seen by the compiler?

  9. #24
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Isn't most of a vector runtime dependent? I'm not sure how much the compiler cares.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ghostrider
    How about multi dimensional vectors? More to the point... Does it (and how so) effect the compiler if you have your extremely large vector of 1000 elements, vs a two dimensional vector of 100x100, or even a 3 dimensional vector of 333x3 of say... type string... To me, 1000 elements is 1000 elements... how would this be seen by the compiler?
    Recall that a "multi dimensional" vector isn't really: it is just a vector of vectors, so if you consider just one level it is just a vector of 100 elements such that each element happens to be a vector of 100 elements. So, at the level of the standard library implementation, there is no difference, 1000 elements is indeed just 1000 elements.

    That said, 1000 elements does not sound like "extremely big" to me, or even "big", though of course if we're talking about how much storage is used rather than the number of elements, it could be "extremely big" if the objects that are stored require a considerable amount of memory per object. I think that this is what Dave11 is getting at, i.e., if the objects are expensive to copy, then doing the reserve() should become more important because it avoids what is expensive. However, Stroustrup's observation could well apply even so: after all, we call push_back for std::vector as running in amortised constant time precise because in the long run, the cost of the occasional copying (and with C++11+: moving) is amortised over the repeated appending of many elements.
    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

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    This is one point where I disagree. Even though it might not matter that much performance-wise, we still have devices that run on battery, and the world is becoming increasingly mobile, and on those devices, performance matters because the faster you get work done, the faster the processor can go back to idle, which translates to longer battery life. For such a simple optimization, I'd argue that you might just as well do it. Like you should worry about picking the right algorithm for your program, you should take time to apply simple optimizations that don't make your program harder to read, harder to debug or maintain.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    This is one point where I disagree. Even though it might not matter that much performance-wise, we still have devices that run on battery, and the world is becoming increasingly mobile, and on those devices, performance matters because the faster you get work done, the faster the processor can go back to idle, which translates to longer battery life. For such a simple optimization, I'd argue that you might just as well do it. Like you should worry about picking the right algorithm for your program, you should take time to apply simple optimizations that don't make your program harder to read, harder to debug or maintain.
    I'd argue that if you know the size beforehand, you should be using std::array instead of std::vector.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #28
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by laserlight View Post
    Incidentally, a former lecturer of mine once recommended std::list over std::vector for a default container for general purpose lists in his website on competitive programming (he taught, and perhaps still teaches, a module dedicated to the topic), on the basis that insertion takes constant time once you have an iterator to the insertion point, whereas std::vector only takes amortised constant time for appending. I never got round to asking him if he really had evidence that this mattered more than other pros of using std::vector for the various competitive programming problems that he was concerned about.
    I suspect that this was once a good idea, but no longer so with modern CPUs.

    Quote Originally Posted by brewbuck View Post
    I'd argue that if you know the size beforehand, you should be using std::array instead of std::vector.
    You can know the size when the vector is initialized, but not at compile time. In this case, an array would not work.
    Last edited by King Mir; 10-11-2015 at 06:29 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #29
    Registered User
    Join Date
    Sep 2015
    Location
    Virginia
    Posts
    19
    @ the Gurus...

    I saw this video on vectors vs lists https://www.youtube.com/watch?v=YQs6IC-vgmo

    Do you guys agree or disagree?










  15. #30
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There's not much to disagree about, but I'm not sure that the point he makes with true OO vs. C++ diagram is realistic. For one thing, a language that works with references the way that he describes is garbage collected, and the collector could decide that the objects in the container will all die at the same time. It would make sense then that the contained objects would be allocated near each other, mitigating the risk of a cache miss. I would guess that collectors used in many languages work this way.
    Last edited by whiteflags; 10-18-2015 at 10:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me figure out how to use STL map containers
    By Bozebo in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2011, 09:48 PM
  2. Map containers
    By Know_Your_Role in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 07:48 AM
  3. When to use vector containers?
    By thetinman in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 05:23 PM
  4. Bit arrays with STL containers
    By Mario F. in forum C++ Programming
    Replies: 23
    Last Post: 07-03-2007, 09:50 AM
  5. Question about containers use
    By Mortissus in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2005, 02:25 PM