Is std::vector so much slower than plain arrays?

I’ve always thought it’s the general wisdom that std::vector is “implemented as an array,” blah blah blah. Today I went down and tested it, and it seems to be not so: Here’s some test results: UseArray completed in 2.619 seconds UseVector completed in 9.284 seconds UseVectorPushBack completed in 14.669 seconds The whole thing completed in … Read more

std::vector performance regression when enabling C++11

I have found an interesting performance regression in a small C++ snippet, when I enable C++11: #include <vector> struct Item { int a; int b; }; int main() { const std::size_t num_items = 10000000; std::vector<Item> container; container.reserve(num_items); for (std::size_t i = 0; i < num_items; ++i) { container.push_back(Item()); } return 0; } With g++ (GCC) … Read more

Split a vector into chunks

I have to split a vector into n chunks of equal size in R. I couldn’t find any base function to do that. Also Google didn’t get me anywhere. Here is what I came up with so far; x <- 1:10 n <- 3 chunk <- function(x,n) split(x, factor(sort(rank(x)%%n))) chunk(x,n) $`0` [1] 1 2 3 … Read more

vector vs. list in STL

I noticed in Effective STL that vector is the type of sequence that should be used by default. What’s does it mean? It seems that ignore the efficiency vector can do anything. Could anybody offer me a scenario where vector is not a feasible option but list must be used? 17 Answers 17