|
Comment on Visual Studio 2013 RC is now available by How to: What is the lifetime of a static variable in a C++ function? #computers #programming #it | IT Info
[…] [2] In C++11 statics are initialized in a thread safe way, this is also known as Magic Statics. […] Read More »
Comment on Reader Q&A: auto and for loop index variables by Vaughn Cato
Although this isn’t much different than other suggestions, I would suggest something like: for (auto i : indices_of(someObject)) { ... }
where `indices_of` is a function which returns a suitable range object. For example: template <typename T> struct index_range { T n; struct iterator { T index; bool operator!=(iterator that) const { return index!=that.index; } iterator operator++() { ++index; return *this; } T operator*() const { return index; } }; iterator begin() const { return {0}; } iterator end() const { return {n}; } }; template <typename T, typename Index = typename T::size_type> index_range<Index> indices_of(const T& container) { return {container.size()}; } int main() { std::vector<int> v = {1,2,3}; for (auto i : indices_of(v)) { std::cout << i << "\n"; } }
One nice thing about this approach is that `indices_of` could even be generalized to work with `std::map` or other containers whose indices aren’t integers, or whose indices don’t start at zero.Read More » | ||
Wednesday, January 21, 2015
Comment on Reader Q&A: auto and for loop index variables by Vaughn Cato
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment