Skip to content

Make the examples for init-statements in if-statements consistent between Chinese and English versions #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions book/en-us/02-usability.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,19 @@ E.g:
int main() {
std::vector<int> vec = {1, 2, 3, 4};

// since c++17, can be simplified by using `auto`
// before C++17, can be simplified by using `auto`
const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2);
if (itr != vec.end()) {
*itr = 3;
}

if (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
itr != vec.end()) {
*itr = 4;
// need to define a new variable
const std::vector<int>::iterator itr2 = std::find(vec.begin(), vec.end(), 3);
if (itr2 != vec.end()) {
*itr2 = 4;
}

// should output: 1, 4, 3, 4. can be simplified using `auto`
// will output: 1, 4, 3, 4; can be simplified using `auto`
for (std::vector<int>::iterator element = vec.begin(); element != vec.end();
++element)
std::cout << *element << std::endl;
Expand All @@ -227,6 +228,7 @@ the entire `std::vector` again. C++17 eliminates this limitation so that
we can do this in if(or switch):

```cpp
// put the temporary variable into the if-statement
if (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
itr != vec.end()) {
*itr = 4;
Expand Down
4 changes: 2 additions & 2 deletions book/zh-cn/02-usability.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ constexpr int fibonacci(const int n) {
int main() {
std::vector<int> vec = {1, 2, 3, 4};

// 在 c++17 之前
// 在 C++17 之前,能用 `auto` 简化
const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2);
if (itr != vec.end()) {
*itr = 3;
Expand All @@ -175,7 +175,7 @@ int main() {
*itr2 = 4;
}

// 将输出 1, 4, 3, 4
// 将输出 1, 4, 3, 4;能用 `auto` 简化
for (std::vector<int>::iterator element = vec.begin(); element != vec.end();
++element)
std::cout << *element << std::endl;
Expand Down