-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice_vector.cpp
More file actions
186 lines (160 loc) · 3.98 KB
/
Copy pathpractice_vector.cpp
File metadata and controls
186 lines (160 loc) · 3.98 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
181
182
183
184
185
186
#include <iostream>
#include <cstring>
#include <array>
#include <algorithm>
#include <vector>
using namespace std;
void ex_string(){
char test[]="hello world!";
// Method 1
for(int i =0; i< sizeof(test); i++){
cout << test[i] ;
}
cout << endl;
//Method 2
for(auto ch : test){
if(ch =='\0'){
cout << "null" << endl;
}
else{
cout << ch;
}
}
//Method 3
char ch[]=u8"測";
cout << strlen(ch) << endl << ch << endl;
}
void ex_array(){
array<int, 5> number={66,12,30,50,90};
// Method 1
cout << "Size of array<int, 5>:" << sizeof(number)<< endl;
for(auto ch: number){
cout << ch << endl;
}
// Method 2
array<int ,5> number2(number); // copy
cout << "Method2" << endl;
for(auto ch: number2){
cout << ch << endl;
}
// Method 3
number[1]= 666;
cout << "Method3" << endl;
for(auto ch: number2){
cout << ch << endl;
}
// Method 4
cout << "Array begin(Addr): " << number.begin() << endl
<< "Array end(Addr): " << number.end() << endl
<< "Array begin: " << *number.begin() << endl
<< "Array end: " << *number.end() << endl;
// Sorting 1
cout << "Sorting" << endl;
array<int, 5> num_sort={9,20,10,3,6};
cout << "Before :" ;
for(auto n : num_sort){
cout << n << ' ';
}
cout << endl;
cout << "After :" ;
sort(num_sort.begin(),num_sort.end());
for(auto n : num_sort){
cout << n << ' ';
}
cout << endl;
//Find 1
array<int, 5> num_find={9,920,310,3,6};
int target;
cout << "The table is [";
for(auto n:num_find){
cout << n << ' ';
}
cout << "] Please input your target number:";
cin >> target;
array<int,5>::iterator result = find(num_find.begin(), num_find.end(),target);
cout << (result != num_find.end()? "Got it":"Didn't find it.") << endl;
//Reverse
array<int,5> num_reverse = {9,5,1,3,2};
cout << "Reverse" << endl;
for(auto n : num_reverse){
cout << n << ' ';
}
cout << endl;
reverse(num_reverse.begin(), num_reverse.end());
for(auto n : num_reverse){
cout << n << ' ';
}
cout << endl;
}
void test_array(){
array<float, 5>num={311.14,36,1.9,320.1,112};
//sorting
cout << "==Sorting==" << endl;
array<float,5>num_sort(num);
cout << "Before:";
for(auto n : num){
cout << ' ' << n;
}
cout << endl;
cout << "After:";
sort(num_sort.begin(), num_sort.end());
for(auto n : num_sort){
cout << ' ' << n;
}
cout << endl;
//reverse
cout << "==Reverse==" << endl;
array<float,5>num_reverse(num);
cout << "Before:";
for(auto n : num){
cout << ' ' << n;
}
cout << endl;
cout << "After:";
reverse(num_reverse.begin(), num_reverse.end());
for(auto n : num_reverse){
cout << ' ' << n;
}
cout << endl;
//find
cout << "==Find==" << endl;
array<float,5>num_find(num);
float target;
cout << "Input a number you want:";
cin >> target;
array<float,5>::iterator result = find(num_find.begin(), num_find.end(),target);
cout << (result!=num_find.end()?"find it":"didn't match");
}
void ex_vector(){
vector<int> num={10,30,96,11};
vector<int> num1={10,11,661,4};
cout << "==Vector==" << endl;
cout << "for in range" << endl;
for(auto n:num){
cout << ' ' << n;
}
cout << endl;
num.swap(num1);
cout << "for in index" << endl;
for(int i=0; i< num.size(); i++){
cout << ' ' << num[i];
}
cout << endl;
}
void var_input(vector<int> args){
for(auto arg : args){
cout << arg << endl;
}
}
int main(){
// ex_string();
// ex_array();
// test_array();
// ex_vector();
vector<int> myarg;
myarg.push_back(5);
myarg.push_back(9);
myarg.push_back(1);
var_input(myarg);
return 0;
}