C++11中for的使用
2016-08-18 21:39
387 查看
range-based for loops: Strings, arrays, and all STL containers can be iterated over with the new range-based for loop already.
C++11 augmented the for statement to support the "foreach" paradigm of iterating over collections. In the new form, it is possible to iterate over C-like arrays, initializer lists and anything for which the non-member begin() and end() functions are overloaded.
Ranged Based Loops, are like the C# foreach loop. The idea is to simplify iteration syntax on containers that have a beginning, an ending, and a forward iterator.
In order to make a data structure iterable, it must work similarly to the existing STL iterators:
(1)、There must be begin and end methods that operate on that structure, either as members or as stand-alone functions, and that return iterators to the beginning and end of the structure.
(2)、The iterator itself must support an operator* method, an operator != method, and an operator++ method, either as members or as stand-alone functions.
C++11中支持基于范围的for循环。
下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:
GitHub:https://github.com/fengbingchun/Messy_Test
C++11 augmented the for statement to support the "foreach" paradigm of iterating over collections. In the new form, it is possible to iterate over C-like arrays, initializer lists and anything for which the non-member begin() and end() functions are overloaded.
Ranged Based Loops, are like the C# foreach loop. The idea is to simplify iteration syntax on containers that have a beginning, an ending, and a forward iterator.
In order to make a data structure iterable, it must work similarly to the existing STL iterators:
(1)、There must be begin and end methods that operate on that structure, either as members or as stand-alone functions, and that return iterators to the beginning and end of the structure.
(2)、The iterator itself must support an operator* method, an operator != method, and an operator++ method, either as members or as stand-alone functions.
C++11中支持基于范围的for循环。
下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:
#include "for.hpp" #include <iostream> #include <vector> //////////////////////////////////////////////// // reference: http://en.cppreference.com/w/cpp/language/range-for int test_for1() { std::vector<int> v = { 0, 1, 2, 3, 4, 5 }; for (const int& i : v) // access by const reference std::cout << i << ' '; std::cout << '\n'; for (auto i : v) // access by value, the type of i is int std::cout << i << ' '; std::cout << '\n'; for (auto&& i : v) // access by reference, the type of i is int& std::cout << i << ' '; std::cout << '\n'; for (int n : {0, 1, 2, 3, 4, 5}) // the initializer may be a braced-init-list std::cout << n << ' '; std::cout << '\n'; int a[] = { 0, 1, 2, 3, 4, 5 }; for (int n : a) // the initializer may be an array std::cout << n << ' '; std::cout << '\n'; for (int n : a) std::cout << 1 << ' '; // the loop variable need not be used std::cout << '\n'; return 0; } /////////////////////////////////////////////////////// // reference: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html int test_for2() { // Modifying the Contents of the Vector std::vector<int> vec; vec.push_back(1); vec.push_back(2); for (int& i : vec) { i++; // increments the value in the vector } for (int i : vec) { // show that the values are updated std::cout << i << std::endl; } return 0; } ////////////////////////////////////////////////////////////////// // reference: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html // forward-declaration to allow use in Iter class IntVector; class Iter { public: Iter(const IntVector* p_vec, int pos) : _pos(pos), _p_vec(p_vec) { } // these three methods form the basis of an iterator for use with a range-based for loop bool operator!= (const Iter& other) const { return _pos != other._pos; } // this method must be defined after the definition of IntVector since it needs to use it int operator* () const; const Iter& operator++ () { ++_pos; // although not strictly necessary for a range-based for loop // following the normal convention of returning a value from // operator++ is a good idea. return *this; } private: int _pos; const IntVector *_p_vec; }; class IntVector { public: IntVector() { } int get(int col) const { return _data[col]; } Iter begin() const { return Iter(this, 0); } Iter end() const { return Iter(this, 100); } void set(int index, int val) { _data[index] = val; } private: int _data[100]; }; int Iter::operator* () const { return _p_vec->get(_pos); } int test_for3() { IntVector v; for (int i = 0; i < 100; i++) { v.set(i, i); } for (int i : v) { std::cout << i << std::endl; } return 0; }
GitHub:https://github.com/fengbingchun/Messy_Test
相关文章推荐
- C++学习-开班简介
- C++ 将整数、浮点数转为字符串
- 6. ZigZag Conversion
- C\C++代码优化的27个建议
- C++和java对比
- 素数判断
- leetcode 165 Compare Version Numbers C++
- 7. Reverse Integer
- 三五班毕业查询
- 9. Palindrome Number
- 第二周《C语言及程序设计》实践项目35 问题求解方法——迭代
- C语言之优先级、结合性与自增运算
- ASCII码转换
- c++实现字符串分隔
- 华为上机题汇总(二十一)
- 阻止关机
- cpu测试
- Longest Palindromic Substring
- 值得推荐的C/C++框架和库 (真的很强大)
- 10秒数