7
anolis
74d

I think vector is the coolest type in c++

Comments
  • 6
    But use it only if you really can't figure out how much memory you actually need, and even then don't use it in time critical code parts.
  • 2
    @Fast-Nop thank you for this insight, I'm still very much a noob with c/++
  • 6
    @Fast-Nop vector can be used effectively in time critical parts. You can bypass bounds checking if you really need to; but the point of stl is to get buffer safety.
  • 2
    Disagree: look for shared_ptr and weak_ptr, and say goodbye to memory leaks and segfaults.
  • 2
    The coolest part of modern C++ is having regular expressions in the standard. I've waited two decades for it to happen!
    Why are people so obsessed with vector?
    If you really need continuous memory for the access speed, use array.
    If you just need a comfortable container, use list.
    There aren't that many reasons to use vector...

    ... And hash containers are much cooler! 😁
  • 1
    @FrodoSwaggins Depends. The thing that must not happen in time critical parts is putting in another element that triggers malloc behind the scenes. Not only that this can be slow, the runtime even varies a lot. But that's the one feature that discerns std::vector from std::array, not bounds checking.
  • 3
    @Fast-Nop the malloc and memcpy are theoretically amortized constant time, because the scheme is to double the size any time you exceed capacity.

    There are also other tricks you can use such as calling .reserve() to realloc the size of the backing buffer manually to something big enough if you know you’re going to need a lot you can do that at startup.

    If you absolutely know the size of elements you need, you can use array<> which can do both runtime and static bounds checking depending on whether your fetch is static or runtime.
  • 1
    @Yamakuzure I didn't even know about list lol
Your Job Suck?
Get a Better Job
Add Comment