mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
revision #1: 更新第二章中已维护的代码
This commit is contained in:
23
code/2/2.11.for.loop.cpp
Normal file
23
code/2/2.11.for.loop.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// 2.11.for.loop.cpp
|
||||
// chapter 2 language usability
|
||||
// modern cpp tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
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 only
|
||||
for (auto &element : vec) {
|
||||
element += 1; // writeable
|
||||
}
|
||||
for (auto element : vec)
|
||||
std::cout << element << std::endl; // read only
|
||||
}
|
||||
Reference in New Issue
Block a user