Split a vector into chunks

A one-liner splitting d into chunks of size 20: split(d, ceiling(seq_along(d)/20)) More details: I think all you need is seq_along(), split() and ceiling(): > d <- rpois(73,5) > d [1] 3 1 11 4 1 2 3 2 4 10 10 2 7 4 6 6 2 1 1 2 3 8 3 10 7 … Read more

Erasing elements from a vector

Use the remove/erase idiom: std::vector<int>& vec = myNumbers; // use shorter name vec.erase(std::remove(vec.begin(), vec.end(), number_in), vec.end()); What happens is that remove compacts the elements that differ from the value to be removed (number_in) in the beginning of the vector and returns the iterator to the first element after that range. Then erase removes these elements … Read more

Why is Java Vector (and Stack) class considered obsolete or deprecated?

Vector synchronizes on each individual operation. That’s almost never what you want to do. Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at … Read more

Array of vectors

If you want to make an array of vectors while reading it’s size from a file you have to do this: int size; in>>size; vector<A>[size] vector_array; I’m not sure what you want. If you want to read it’s value from a file you can do this: for(int i = 0;i<array.size();i++){ //get data from file and … Read more