-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharrayList.cpp
More file actions
84 lines (77 loc) · 1.99 KB
/
arrayList.cpp
File metadata and controls
84 lines (77 loc) · 1.99 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Vector.h"
using namespace std;
struct myNode
{
double sum;
int id;
myNode() {}
myNode(double sum, double id)
{
this->sum = sum;
this->id = id;
}
bool operator<(const myNode &temp) const
{
if (sum != temp.sum)
return sum < temp.sum;
else
return id < temp.id;
}
friend ostream &operator<<(ostream &o, const myNode &temp);
};
ostream &operator<<(ostream &o, const myNode &temp)
{
o << "[sum = " << temp.sum << ",id = " << temp.id << "]";
return o;
}
int main()
{
int array[] = {1, 4, -9, 0, 30};
ArrayList<int> list(array, sizeof(array) / sizeof(int));
for (int i = 0; i < 15; ++i)
{
list.insert(0, i + 1);
}
list.printArray();
list.erase(list.getSize() - 1);
list.printArray();
cout << "list.getCapacity() = " << list.getCapacity() << endl;
for (int i = 0; i < 12; ++i)
{
list.push_back(i);
cout << "list.getCapacity() = " << list.getCapacity() << endl;
if (i % 10 == 0)
list.printArray();
}
list.printArray();
ArrayList<int> copy(list);
copy.printArray();
copy.reverse();
copy.printArray();
cout << "copy[0] = " << copy[0] << endl;
copy.sort();
copy.printArray();
/*
参考:
C++ 11 Lambda表达式:
https://blog.csdn.net/lcalqf/article/details/79401210
*/
list.sort([](const int &a, const int &b) -> bool { return a > b; });
list.printArray();
list.sort();
list.printArray();
//结构体向量 需自定义运算符重载
ArrayList<myNode> nodeLists;
nodeLists.push_front(myNode(12.34, 5));
nodeLists.insert(0, myNode(12.34, 3));
nodeLists.insert(0, myNode(-12.34, 0));
nodeLists.insert(0, myNode(129.34, 6));
nodeLists.insert(0, myNode(-1265.34, -5));
nodeLists.printArray();
nodeLists.sort();
nodeLists.printArray();
return 0;
}