Posts

Showing posts with the label Stl

Advance Iterator For The Std::vector Std::advance VS Operator +?

Answer : Adding will only work with random access iterators. std::advance will work with all sorts of iterators. As long as you're only dealing with iterators into vectors, it makes no real difference, but std::advance keeps your code more generic (e.g. you could substitute a list for the vector , and that part would still work). For those who care, the standard describes advance and distance as follows (§24.3.4/1): Since only random access iterators provide + and - operators, the library provides two function templates advance and distance . These function templates use + and - for random access iterators (and are, therefore, constant time for them); for input, forward and bidirectional iterators they use ++ to provide linear time implementations. Also note that starting with C++11, the standard added a parameter to std::next , so you can advance by a specified amount using it (and std::prev similarly). The difference from std::advance is that it returns th...

C++ Erase Vector Element By Value Rather Than By Position?

Answer : How about std::remove() instead: #include <algorithm> ... vec.erase(std::remove(vec.begin(), vec.end(), 8), vec.end()); This combination is also known as the erase-remove idiom. You can use std::find to get an iterator to a value: #include <algorithm> std::vector<int>::iterator position = std::find(myVector.begin(), myVector.end(), 8); if (position != myVector.end()) // == myVector.end() means the element was not found myVector.erase(position); You can not do that directly. You need to use std::remove algorithm to move the element to be erased to the end of the vector and then use erase function. Something like: myVector.erase(std::remove(myVector.begin(), myVector.end(), 8), myVec.end()); . See this erasing elements from vector for more details.

Advantages Of Using Arrays Instead Of Std::vector?

Answer : In general, I strongly prefer using a vector over an array for non-trivial work; however, there are some advantages of arrays: Arrays are slightly more compact: the size is implicit. Arrays are non-resizable; sometimes this is desirable. Arrays don't require parsing extra STL headers (compile time). It can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using). Fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed. Because C++03 has no vector literals. Using arrays can sometime produce more succinct code. Compared to array initialization: char arr[4] = {'A', 'B', 'C', 'D'}; vector initialization can look somewhat verbose std::vector<char> v; v.push_back('A'); v.push_back('B'); ... I'd go for std::array available in C++0x instead of plain arrays which can...

Can Std::transform Be Replaced By Std::accumulate?

Answer : Those two algorithms have completely different purpose. std::accumulate is known as fold in functional programming world, and it's purpose is iterate over elements of a sequence and apply two-argument folding operation to those elements, with one argument being result of previous fold and other being element of the sequence. It naturally returns the a single result - a fold of all elements of a sequence into one value. On the other hand, std::transform copies values from one sequence to another, applying a unary operation to each element. It returns an iterator to the end of sequence. The fact that you can supply any code as the fold operation allows us to use std::accumulate as a generic loop replacement, including an option to copy values into some other container, but that it is ill-advised, as the whole reason for introducing those (rather simple) algorithms was to make programs more explicit. Making one algo to perform the task which is normally associat...

Can I Use Std::transform In Place With A Parallel Execution Policy?

Answer : I believe that it's talking about a different detail. The unary_op takes an element of the sequence and returns a value. That value is stored (by transform ) into the destination sequence. So this unary_op would be fine: int times2(int v) { return 2*v; } but this one would not: int times2(int &v) { return v*=2; } But that's not really what you're asking about. You want to know if you can use the unary_op version of transform as a parallel algorithm with the same source and destination range. I don't see why not. transform maps a single element of the source sequence to a single element of the destination sequence. However, if your unary_op isn't really unary, (i.e, it references other elements in the sequence - even if it only reads them, then you will have a data race). To quote the standard here [alg.transform.1] op [...] shall not invalidate iterators or subranges, or modify elements in the ranges this forbids y...