//// 2.11.for.loop.cpp// chapter 2 language usability// modern cpp tutorial//// created by changkun at changkun.de// https://github.com/changkun/modern-cpp-tutorial//#include <iostream>#include <vector>#include <algorithm>#include <cstddef>// A minimal custom container: any type that provides usable begin()/end()// (whose iterators support !=, * and pre-++) can be used with range-based for,// because the loop is just syntactic sugar over those operations.struct IntRange {int* data;std::size_t n;int* begin() const { return data; }int* end() const { return data + n; }};int main() {std::vector<int> vec = {1, 2, 3, 4};if (auto itr = std::find(vec.begin(), vec.end(), 3); itr != vec.end()) *itr = 4;for (auto element : vec)std::cout << element << std::endl; // read onlyfor (auto &element : vec) {element += 1; // writeable}for (auto element : vec)std::cout << element << std::endl; // read only// range-based for over a user-defined containerint arr[] = {10, 20, 30};IntRange range{arr, 3};for (auto&& x : range)std::cout << x << ' ';std::cout << std::endl;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。