forked from PhysicsX/ExampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.cpp
More file actions
180 lines (137 loc) · 3.49 KB
/
vec.cpp
File metadata and controls
180 lines (137 loc) · 3.49 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Example vector usage
* ulasdikme.com
*/
#include <iostream>
#include <vector>
#include <algorithm>
class Example
{
private:
int i, y;
public:
Example(int i, int y):i(i),y(y){}
bool operator < (const Example &ex) const
{
return i>ex.i;
}
//Binary operators can not modify their left operands that is why method should be friend. Note: friend methods are non-member methods even if they implemented in the definition of class.
friend bool operator > (const Example &lhs, const Example &rhs)
{
return lhs.i > rhs.i;
}
friend std::ostream &operator <<(std::ostream &out, const Example &ex)
{
out<<"i "<<ex.i<<" y "<<ex.y<<std::endl;
return out;
}
static bool comp(const Example &lhs, const Example &rhs)
{
return lhs.i > rhs.i;
}
int getI()const{ return i; };
class Comp
{
public:
bool operator ()(const Example &lhs,
const Example &rhs)
{
return lhs.i > rhs.i;
}
};
};
bool comparetor(std::pair<int, int> &lhs, std::pair<int, int> &rhs)
{
return lhs.first > rhs.first;
}
int main()
{
try
{
std::vector<int> vec;
//to see the effec bad_alloc, command out for(;;)
//on the same terminal
//ulimit -s -V 20000
//run the binary
//after do not forget to run ulimit -S -v unlimited
//for(;;)
vec.push_back(1);
}
catch(std::bad_alloc e)
{
std::cout<<e.what()<<std::endl;
}
//std::vector<int> secondVec (10,10);
//std::cout<<secondVec[10]<<std::endl; //this will violate memory
//std::cout<<secondVec.at(10)<<std::endl; // this will return out_of_rangee error
std::vector<int> vec = {1,2,3,4,5,6,7,8,9};
std::sort(vec.begin(), vec.end(), std::greater<int>());
//iterate through c++11 range based loop
for(auto s : vec)
std::cout<<s;
vec.erase(std::remove_if(vec.begin(), vec.end(),[]
(int i)
{
return i > 5;
}),
vec.end());
std::cout<<"after removing"<<std::endl;
for(auto s : vec)
std::cout<<s;
std::vector<std::pair<int,int>>vec2 =
{
{1,9},{2,8},{3,7},{4,6},{5,5},{6,4},{7,3},{8,2},{9,1}
};
std::cout<<std::endl;
//iterate through stl style
std::vector<std::pair<int, int>>::iterator vecIt;
for(vecIt = vec2.begin(); vecIt != vec2.end(); vecIt++)
std::cout<<vecIt->first<<" "<<vecIt->second<<std::endl;
std::sort(vec2.begin(), vec2.end(), comparetor);
//std::sort(vec2.begin(), vec2.end(), std::greater<std::pair<int, int>>());
/*
std::sort(vec2.begin(), vec2.end(),[](std::pair<int, int> &lhs,
std::pair<int, int> &rhs)
{
return lhs.first > rhs.first;
}
);
*/
std::cout<<"after sorting"<<std::endl;
for(auto s : vec2)
std::cout<<s.first<<" "<<s.second<<std::endl;
typedef std::pair<int, int> ii;
std::vector<ii> vii;
std::vector<Example> vecExample =
{
{1,9},
{2,8},
{3,7},
{4,6},
{5,5},
{6,4},
{7,3},
{8,2},
{9,1}
};
for(auto s : vecExample)
std::cout<<s;
//std::sort(vec.begin(), vec.end());
//std::sort(vec.begin(), vec.end(), Example::comp); // function object
//std::sort(vec.begin(), vec.end(), Example::Comp{}); //function object
std::sort(vecExample.begin(), vecExample.end(), std::greater<Example>());
auto sortEx = [](const Example &lhs, const Example &rhs)
{
return lhs.getI() > rhs.getI();
};
std::sort(vecExample.begin(), vecExample.end(), sortEx);
std::sort(vecExample.begin(), vecExample.end(),[](const Example &lhs,
const Example &rhs)
{
return lhs.getI() > rhs.getI();
});
std::cout<<"after sorting"<<std::endl;
for(auto s : vecExample)
std::cout<<s;
return 0;
}