forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.31.cpp
27 lines (22 loc) · 721 Bytes
/
4.31.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <vector>
int main() {
std::vector<int> ivec(10);
std::vector<int>::size_type cnt = ivec.size();
for (std::vector<int>::size_type ix = 0; ix != ivec.size(); ++ix, --cnt)
ivec[ix] = cnt;
for (const auto &e : ivec)
std::cout << e << " ";
std::cout << std::endl;
// Because we don't need the value returned by postfix operator, and making a
// copy of an object may be a heavy operation, so prefix operator is prefered
// here.
cnt = ivec.size();
for (std::vector<int>::size_type ix = 0; ix != ivec.size(); ix++, cnt--)
ivec[ix] = cnt;
for (const auto &e : ivec)
std::cout << e << " ";
std::cout << std::endl;
// The results are same.
return 0;
}