revision #1: 更新第二章中已维护的代码

This commit is contained in:
Changkun Ou
2018-04-02 00:28:05 +02:00
parent 11efa38dba
commit 838d30ef5a
28 changed files with 460 additions and 111 deletions

31
code/2/2.3.if.switch.cpp Normal file
View File

@@ -0,0 +1,31 @@
//
// 2.3.if.switch.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};
// before c++17, can be simplefied by using `auto`
const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2);
if (itr != vec.end()) {
*itr = 3;
}
// after c++17, can be simplefied by using `auto`
if (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
itr != vec.end()) {
*itr = 4;
}
// should output: 1, 4, 3, 4. can be simplefied using `auto`
for (std::vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
std::cout << *element << std::endl;
}