Skip to content

Commit aaa795d

Browse files
committed
add algorithm comments
1 parent 13d0ba2 commit aaa795d

23 files changed

+37
-18
lines changed

6_STL_algorithms/6_3_1_numeric.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
using namespace std;
1010

11-
int main() {
11+
int main()
12+
{
1213
int ia[5] = {1, 2, 3, 4, 5};
1314
vector<int> iv(ia, ia + 5);
1415
// 0+1+2+3...
1516
cout << accumulate(iv.begin(), iv.end(), 0) << endl;
16-
17+
1718
// 0-1-2-3
1819
cout << accumulate(iv.begin(), iv.end(), 0, minus<int>()) << endl;
1920

@@ -22,7 +23,8 @@ int main() {
2223

2324
// 10 - 1+1 - 2+2 - ...
2425
cout << inner_product(iv.begin(), iv.end(), iv.begin(), 10,
25-
minus<int>(), plus<int>()) << endl;
26+
minus<int>(), plus<int>())
27+
<< endl;
2628

2729
// 将迭代器绑定到cout,作为输出用
2830
ostream_iterator<int> oite(cout, " ");
@@ -50,5 +52,8 @@ int main() {
5052
int n = 3;
5153
iota(iv.begin(), iv.end(), n); // 填入n, n+1, n+2
5254
for (int i = 0; i < iv.size(); ++i)
53-
cout << iv[i] << ' ';
55+
cout << iv[i] << " ";
56+
cout << endl;
57+
58+
return 0;
5459
}

6_STL_algorithms/6_4_1_algobase.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,50 @@
1010
using namespace std;
1111

1212
template <class T>
13-
struct display {
14-
void operator() (const T& x) const {
13+
struct display
14+
{
15+
void operator()(const T &x) const
16+
{
1517
cout << x << ' ';
1618
}
1719
};
1820

19-
20-
int main() {
21+
int main()
22+
{
2123
int ia[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
2224
// int ib = iota(ia, ia + 9, 0); // include numeric
2325

2426
vector<int> iv1(ia, ia + 5);
2527
vector<int> iv2(ia, ia + 9);
2628

2729
// 判断两个序列第一个不匹配点 pair(第一个序列的不匹配点,第二个序列的不匹配点)
28-
cout << *(mismatch(iv1.begin(), iv1.end(), iv2.begin()).first);
29-
cout << *(mismatch(iv1.begin(), iv1.end(), iv2.begin()).second);
30+
cout << *(mismatch(iv1.begin(), iv1.end(), iv2.begin()).first) << " ";
31+
cout << *(mismatch(iv1.begin(), iv1.end(), iv2.begin()).second) << endl;
3032

3133
// 如果序列在区间内相等,equal返回true
32-
cout << equal(iv1.begin(), iv1.end(), iv2.begin());
33-
cout << equal(iv1.begin(), iv1.end(), &ia[3]);
34-
cout << equal(iv1.begin(), iv1.end(), &ia[3], less<int>());
34+
cout << equal(iv1.begin(), iv1.end(), iv2.begin()) << " ";
35+
cout << equal(iv1.begin(), iv1.end(), &ia[3]) << " ";
36+
cout << equal(iv1.begin(), iv1.end(), &ia[3], less<int>()) << endl;
3537

38+
// 区间区间内全部填 9
3639
fill(iv1.begin(), iv1.end(), 9);
3740
for_each(iv1.begin(), iv1.end(), display<int>());
41+
cout << endl;
3842

39-
fill_n(iv1.begin(), 3, 7); // 从迭代器开始填3个7
43+
// 从迭代器开始填3个7
44+
fill_n(iv1.begin(), 3, 7);
4045
for_each(iv1.begin(), iv1.end(), display<int>());
46+
cout << endl;
4147

4248
vector<int>::iterator ite1 = iv1.begin();
4349
vector<int>::iterator ite2 = ite1; // 指向7
44-
advance(ite2, 3); // 指向9,前进
50+
advance(ite2, 3); // 指向9,前进3
4551

52+
// 将迭代器所指元素对调
4653
iter_swap(ite1, ite2);
4754
cout << *ite1 << ' ' << *ite2 << endl;
4855
for_each(iv1.begin(), iv1.end(), display<int>());
56+
cout << endl;
4957

5058
cout << max(*ite1, *ite2) << endl;
5159
cout << min(*ite1, *ite2) << endl;
@@ -56,13 +64,17 @@ int main() {
5664

5765
swap(*iv1.begin(), *iv2.begin());
5866
for_each(iv1.begin(), iv1.end(), display<int>());
67+
cout << endl;
5968
for_each(iv2.begin(), iv2.end(), display<int>());
69+
cout << endl;
6070

6171
string stra1[] = {"Jamie", "JJHou", "Jason"};
6272
string stra2[] = {"Jamie", "JJHou", "Jerry"};
6373

6474
cout << lexicographical_compare(stra1, stra1 + 2, stra2, stra2 + 2);
65-
cout << lexicographical_compare(stra1, stra1 + 2, stra2, stra2 + 2,
66-
greater<string>());
67-
75+
cout << endl;
76+
cout << lexicographical_compare(stra1, stra1 + 2, stra2, stra2 + 2, greater<string>());
77+
78+
cout << endl;
79+
return 0;
6880
}

6_STL_algorithms/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 算法
2+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)