Is it safe to push_back an element from the same vector?

It looks like http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#526 addressed this problem (or something very similar to it) as a potential defect in the standard: 1) Parameters taken by const reference can be changed during execution of the function Examples: Given std::vector v: v.insert(v.begin(), v[2]); v[2] can be changed by moving elements of vector The proposed resolution was that this … Read more

Iterating C++ vector from the end to the beginning

One way is: for (vector<my_class>::reverse_iterator i = my_vector.rbegin(); i != my_vector.rend(); ++i ) { } rbegin()/rend() were especially designed for that purpose. (And yes, incrementing a reverse_interator moves it backward.) Now, in theory, your method (using begin()/end() & –i) would work, std::vector‘s iterator being bidirectional, but remember, end() isn’t the last element — it’s one … Read more

Why isn’t vector a STL container?

For space-optimization reasons, the C++ standard (as far back as C++98) explicitly calls out vector<bool> as a special standard container where each bool uses only one bit of space rather than one byte as a normal bool would (implementing a kind of “dynamic bitset”). In exchange for this optimization it doesn’t offer all the capabilities … Read more

What are the differences between vector and list data types in R?

Technically lists are vectors, although very few would use that term. “list” is one of several modes, with others being “logical”, “character”, “numeric”, “integer”. What you are calling vectors are “atomic vectors” in strict R parlance: aaa <- vector(“list”, 3) is.list(aaa) #TRUE is.vector(aaa) #TRUE Lists are a “recursive” type (of vector) whereas atomic vectors are … Read more

Convert a row of a data frame to vector

When you extract a single row from a data frame you get a one-row data frame. Convert it to a numeric vector: as.numeric(df[1,]) As @Roland suggests, unlist(df[1,]) will convert the one-row data frame to a numeric vector without dropping the names. Therefore unname(unlist(df[1,])) is another, slightly more explicit way to get to the same result. … Read more

In Clojure, when should I use a vector over a list, and the other way around?

Once again, it seems I’ve answered my own question by getting impatient and asking it in #clojure on Freenode. Good thing answering your own questions is encouraged on Stackoverflow.com 😀 I had a quick discussion with Rich Hickey, and here is the gist of it. [12:21] <Raynes> Vectors aren’t seqs, right? [12:21] <rhickey> Raynes: no, … Read more

Initializing a two dimensional std::vector

Use the std::vector::vector(count, value) constructor that accepts an initial size and a default value: std::vector<std::vector<int> > fog( ROW_COUNT, std::vector<int>(COLUMN_COUNT)); // Defaults to zero initial value If a value other than zero, say 4 for example, was required to be the default then: std::vector<std::vector<int> > fog( ROW_COUNT, std::vector<int>(COLUMN_COUNT, 4)); I should also mention uniform initialization was … Read more