-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl first class.cpp
115 lines (78 loc) · 2.78 KB
/
stl first class.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
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
youtube clas link
https://www.youtube.com/watch?v=W1McvE4a910&list=PLoa_roVVsxA0D1Kv_T7rbGHtSdYIUo4f5
#include<bits/stdc++.h>
using namespace std;
int main ()
{
vector<int> v;
v.push_back( 1 );
v.push_back( 2 );
v.push_back( 3 );
cout << v[0] << " " << v[1] << " " << v[2] << endl; /// 1 2 3
v[1] = 3;
cout << v[0] << " " << v[1] << " " << v[2] << endl; /// 1 3 3
cout << v.size() << endl; /// 3
for ( int i = 0; i < v.size(); i++ ) cout << v[i] << " "; /// 1 3 3
cout << endl;
vector <int> v1 = { 2, 3, 4 };
cout << v1.size() << endl; /// 3
for ( int i = 0; i < v1.size(); i++ ) cout << v1[i] << " "; /// 2 3 4
cout << endl;
v.clear();
cout << v.size() << endl; /// 0
cout << v.empty() << endl; /// 1
cout << v1.empty() << endl; /// 0
v1.resize(5);
cout << v1.size() << endl; /// 5
for ( int i = 0; i < v1.size(); i++ ) cout << v1[i] << " "; /// 2 3 4 0 0
cout << endl;
vector<int> a(5);
cout << a.size() << endl; /// 5
for ( int i = 0; i < a.size(); i++ ) cout << a[i] << " "; /// 0 0 0 0 0
cout << endl;
a = v1;
for ( auto u : a ) cout << u << " "; /// 2 3 4 0 0
cout << endl;
vector<int>::iterator it;
for ( it = a.begin(); it != a.end(); it++ ) cout << *it << " "; /// 2 3 4 0 0
cout << endl;
a = { 3, 4, 5, 1, 2 };
sort ( a.begin(), a.end() ); ///O(n*log2(n))
for ( auto u : a ) cout << u << " "; /// 1 2 3 4 5
cout << endl;
sort ( a.rbegin(), a.rend() );
for ( auto u : a ) cout << u << " "; /// 5 4 3 2 1
cout << endl;
a = { 3, 4, 5, 1, 2 };
sort ( a.begin(), a.end(), greater<int>() );
for ( auto u : a ) cout << u << " "; /// 5 4 3 2 1
cout << endl;
a = { 3, 4, 5, 1, 2 };
reverse( a.begin(), a.end() );
for ( auto u : a ) cout << u << " "; /// 2 1 5 4 3
cout << endl;
cout << a.back() << endl; /// 3
a.pop_back(); /// O(1) complexity.
cout << a.back() << endl; /// 4
a = { 3, 4, 5, 1, 2 };
cout << *a.begin() << endl; /// 3
a.erase( a.begin() ); /// O(n) complexity.
for ( auto u : a ) cout << u << " "; /// 4 5 1 2
cout << endl;
a.erase( a.begin()+2 );
for ( auto u : a ) cout << u << " "; /// 4 5 2
cout << endl;
a = { 1, 1, 2, 2, 2, 3, 3 };
unique( a.begin(), a.end() );
for ( auto u : a ) cout << u << " "; /// 1 2 3 2 2 3 3
cout << endl;
a = { 1, 1, 2, 2, 2, 3, 3 };
int n = unique( a.begin(), a.end() ) - a.begin();
cout << n << endl; /// 3
for ( int i = 0; i < n; i++ ) cout << a[i] << " "; /// 1 2 3
cout << endl;
a = { 2, 3, 1, 5 };
cout << max_element( a.begin(), a.end() ) - a.begin() << endl; /// 3
cout << *max_element( a.begin(), a.end() ) << endl; /// 5
return 0;
}