-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudy.cpp
More file actions
561 lines (452 loc) · 12.9 KB
/
study.cpp
File metadata and controls
561 lines (452 loc) · 12.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#include "study.h"
#include "pluto.h"
void Study::print() {
cout<<"Hello, welcome to Redhat Linux os!"<<endl;
}
//deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素
void Study::queue() {
deque<int> d;
//d.assign(2,7);
deque<int>::iterator pos;
d.push_back(10);
//cout<<d[0];
d.push_back(20);
d.push_back(30);
for (pos = d.begin(); pos != d.end(); pos++)
printf("%d ", *pos);
putchar('\n');
cout<<"Start Queue:"<<endl;
for(int i = 0; i < d.size(); i++)
{
cout<<d.at(i)<<"\t";
}
cout<<endl;
d.push_front(5);
d.push_front(3);
d.push_front(1);
cout<<"after push_front(5.3.1):"<<endl;
for(int i = 0;i < d.size();i++)
{
cout<<d.at(i)<<"\t";
}
cout<<endl;
d.pop_front();
d.pop_front();
cout<<"after pop_front() two times:"<<endl;
for(int i = 0;i < d.size();i++)
{
cout<<d.at(i)<<"\t";
}
cout<<endl;
}
void Study::compare() {
vector<int>v(2);
v[0]=10;
int *p = &v[0];
// cout<<"vector第一个元素迭代指针*p="<<*p<<endl;
cout<<"vector`s no.1 itor *p="<<*p<<endl;
v.push_back(20);
// cout<<"vector容量变化后原vector第1个元素迭代指针*p="<<*p<<endl;
cout<<"vector after change no.1 itor *p="<<*p<<endl;
deque<int> d(2);
d[0]=10;
int *q = &d[0];
//cout<<"deque第一个元素迭代指针*q="<<*q<<endl;
cout<<"deque`s no.1 itor *q="<<*q<<endl;
d.push_back(20);
//cout<<"deque容量变化后第一个元素迭代器指针*q="<<*q<<endl;
cout<<"deque after change no.1 itor *q="<<*q<<endl;
}
void Study::vect() {
// 初始化示例
// //int型vector
// vector<int> vecInt;
// //float型vector
// vector<float> vecFloat;
// //自定义类型,保存类A的vector
// vector<A> vecA;
// //自定义类型,保存指向类A的指针的vector
// vector<A*> vecPointA;
//代码用了4种方法建立vector并对其初始化
//int型vector,包含3个元素
// vector<int> vecIntA(3);
// //int型vector,包含3个元素且每个元素都是9
// vector<int> vecIntB(3,9);
// //复制vecIntB到vecIntC
// vector<int> vecIntC(vecIntB);
// int iArray[]={2,4,6};
// //创建vecIntD
// vector<int> vecIntD(iArray,iArray+3);
//打印vectorA,此处也可以用下面注释内的代码来输出vector中的数据
/*for(int i=0;i<vecIntA.size();i++)
{
cout<<vecIntA[i]<<" ";
}*/
// cout<<"vecIntA:"<<endl;
// for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
// {
// cout<<*it<<" ";
// }
// cout<<endl;
// //打印vecIntB
// cout<<"VecIntB:"<<endl;
// for(vector<int>::iterator it = vecIntB.begin() ;it!=vecIntB.end();it++)
// {
// cout<<*it<<" ";
// }
// cout<<endl;
// //打印vecIntC
// cout<<"VecIntB:"<<endl;
// for(vector<int>::iterator it = vecIntC.begin() ;it!=vecIntC.end();it++)
// {
// cout<<*it<<" ";
// }
// cout<<endl;
// //打印vecIntD
// cout<<"vecIntD:"<<endl;
// for(vector<int>::iterator it = vecIntD.begin() ;it!=vecIntD.end();it++)
// {
// cout<<*it<<" ";
// }
// cout<<endl;
//增加及获得元素示例
// //int型vector,包含3个元素
// vector<A> vecClassA;
// A a1(1);
// A a2(2);
// A a3(3);
// //插入1 2 3
// vecClassA.push_back(a1);
// vecClassA.push_back(a2);
// vecClassA.push_back(a3);
// int nSize = vecClassA.size();
// cout<<"vecClassA:"<<endl;
// //打印vecClassA,方法一:
// for(int i=0;i<nSize;i++)
// {
// cout<<vecClassA[i].n<<" ";
// }
// cout<<endl;
// //打印vecClassA,方法二:
// for(int i=0;i<nSize;i++)
// {
// cout<<vecClassA.at(i).n<<" ";
// }
// cout<<endl;
// //打印vecClassA,方法三:
// for(vector<A>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)
// {
// cout<<(*it).n<<" ";
// }
// cout<<endl;
// //int型vector,包含3个元素
// vector<A*> vecClassA;
// A *a1 = new A(1);
// A *a2 = new A(2);
// A *a3 = new A(3);
// //插入1 2 3
// vecClassA.push_back(a1);
// vecClassA.push_back(a2);
// vecClassA.push_back(a3);
// int nSize = vecClassA.size();
// cout<<"vecClassA:"<<endl;
// //打印vecClassA,方法一:
// for(int i=0;i<nSize;i++)
// {
// cout<<vecClassA[i]->n<<"\t";
// }
// cout<<endl;
// //打印vecClassA,方法二:
// for(int i=0;i<nSize;i++)
// {
// cout<<vecClassA.at(i)->n<<"\t";
// }
// cout<<endl;
// //打印vecClassA,方法三:
// for(vector<A*>::iterator it = vecClassA.begin();it!=vecClassA.end();it++)
// {
// cout<<(**it).n<<"\t";
// }
// cout<<endl;
// delete a1; delete a2;delete a3;
// //修改元素示例
// //int型vector,包含3个元素
// vector<int> vecIntA;
// //插入1 2 3
// vecIntA.push_back(1);
// vecIntA.push_back(2);
// vecIntA.push_back(3);
// int nSize = vecIntA.size();
// //通过引用修改vector
// cout<<"通过数组修改,第二个元素为8:"<<endl;
// vecIntA[1]=8;
// cout<<"vecIntA:"<<endl;
// //打印vectorA
// for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
// {
// cout<<*it<<" ";
// }
// cout<<endl;
// //通过引用修改vector
// cout<<"通过引用修改,第二个元素为18:"<<endl;
// int &m = vecIntA.at(1);
// m=18;
// cout<<"vecIntA:"<<endl;
// //打印vectorA
// for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
// {
// cout<<*it<<" ";
// }
// cout<<endl;
// //通过迭代器修改vector
// cout<<"通过迭代器修改,第二个元素为28"<<endl;
// vector<int>::iterator itr = vecIntA.begin()+1;
// *itr = 28;
// cout<<"vecIntA:"<<endl;
// //打印vectorA
// for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
// {
// cout<<*it<<" ";
// }
// cout<<endl;
// 删除向量示例
//int型vector,包含3个元素
vector<int> vecIntA;
//循环插入1 到10
for(int i=1;i<=10;i++)
{
vecIntA.push_back(i);
}
vecIntA.erase(vecIntA.begin()+4);
cout<<"del no.5 vecIntA:"<<endl;
//打印vectorA
for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
{
cout<<*it<<"\t";
}
cout<<endl;
//删除第2-5个元素
vecIntA.erase(vecIntA.begin()+1,vecIntA.begin()+5);
cout<<"del no.2-no.5 vecIntA:"<<endl;
//打印vectorA
for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
{
cout<<*it<<"\t";
}
cout<<endl;
//删除最后一个元素
vecIntA.pop_back();
cout<<"del last vecIntA:"<<endl;
//打印vectorA
for(vector<int>::iterator it = vecIntA.begin();it!=vecIntA.end();it++)
{
cout<<*it<<"\t";
}
cout<<endl;
}
void Study::vects() {
Student s1("1001","zhangsan","boy","1988-10-10");
Student s2("1002","lisi","boy","1988-8-25");
Student s3("1003","wangwu","boy","1989-2-14");
// map<int,Student*> stumap;
// stumap[1001]=&s1;
// stumap[1002]=&s2;
// stumap[1003]=&s3;
// map<int, Student*>::iterator iterstu = stumap.find(1003);
// if (iterstu != stumap.end())
// {
// iterstu->second->Display();
// }
RoleTemp r1;
r1.rid=1001;
r1.age=12;
r1.name="toy";
RoleTemp r2;
r2.rid=1002;
r2.age=13;
r2.name="tom";
RoleTemp r3;
r3.rid=1003;
r3.age=14;
r3.name="joy";
map<int,RoleTemp> rolemap;
rolemap[1001]=r1;
rolemap[1002]=r2;
rolemap[1003]=r3;
map<int, RoleTemp>::iterator iterrole = rolemap.find(1003);
if (iterrole != rolemap.end())
{
cout<<"role:"<<iterrole->second.name<<endl;
}
StudCollect s;
s.Add(s1);
s.Add(s2);
s.Add(s3);
Student* ps = s.Find("1002");
if(ps)
ps->Display();
}
void Student::Display() {
cout<<m_strNO<<"\t";
cout<<m_strName<<"\t";
cout<<m_strSex<<"\t";
cout<<m_strDate<<"\t\n";
}
//注意传值 传址
void StudCollect::Add(Student &s) {
//cout<<&s<<endl;//输出地址
//cout<<s.m_strNO<<endl;
m_vStud.push_back(s);
}
Student* StudCollect::Find(string strNO) {
bool bFind = false;
int i;
for(i = 0;i < m_vStud.size();i++)
{
Student& s = m_vStud.at(i);
if(s.m_strNO == strNO)
{
bFind = true;
break;
}
}
Student* s = NULL;
if(bFind)
s = &m_vStud.at(i);
return s;
}
void Study::lists() {
LISTSTR test;
test.push_back("back");
test.push_back("middle");
test.push_back("front");
cout<<test.front()<<endl;
cout<<*test.begin()<<endl;
cout<<test.back()<<endl;
cout<<*(test.rbegin())<<endl;
test.pop_front();
test.pop_back();
cout<<test.front()<<endl;
}
void Study::maps(){
map<char,int> mymap;
mymap.insert(pair<char,int>('a',10));
mymap.insert(pair<char,int>('z',200));
pair<map<char,int>::iterator,bool> ret;
ret = mymap.insert(pair<char,int>('z',500));
if (ret.second == false)
{
cout<<"element 'z' already existed";
cout<<"with a value of "<<ret.first->second<<'\n';
}
map<char,int>::iterator it = mymap.begin();
mymap.insert(it,pair<char,int>('b',300));
mymap.insert(it,pair<char,int>('c',400));
map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));
cout<<"mymap contains :\n";
for (it = mymap.begin();it!= mymap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
}
cout<<"anothermap contains :\n";
for (it = anothermap.begin();it!= anothermap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
}
}
void Study::maps1() {
map<char,int> mymap;
map<char,int>::iterator it;
mymap['a'] = 10;
mymap['b'] = 20;
mymap['c'] = 30;
mymap['d'] = 40;
mymap['e'] = 50;
mymap.insert(pair<char,int>('f',60));
cout<<"initial mymap contains :\n";
for (it = mymap.begin();it!= mymap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
}
it = mymap.find('b');
mymap.erase(it);
mymap.erase('c');
it = mymap.find('e');
mymap.erase(it,mymap.end());
cout<<"now mymap contains :\n";
for (it = mymap.begin();it!= mymap.end();it++)
{
cout<<it->first<<"=>"<<it->second<<'\n';
}
}
void Study::swap1(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
void Study::swap2(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void Study::duplicate (int& a, int& b, int& c) {
a*=2;
b*=2;
c*=2;
}
void Study::pluto_alltype_test()
{
CPluto c1;
c1.Encode(120);
c1 << (uint8_t) 120;
c1 << (uint16_t) 120;
c1 << (uint32_t) 120;
c1 << (uint64_t) 120;
c1 << (int8_t) 120;
c1 << (int16_t) 120;
c1 << (int32_t) 120;
c1 << (int64_t) 120;
// c1 << (float32_t) 120.1;
// c1 << (float64_t) 120.1;
// c1 << "abcddef";
// {
// charArrayDummy cc;
// cc.m_l = 4;
// strcpy(cc.m_s, "12\03");
// c1 << cc;
// cc.m_l = 0;
// }
c1 << EndPluto;
PrintHexPluto(c1);
//CPluto c2(c1.GetBuff(), c1.GetLen());
//c2.Decode();
//uint8_t u8;
// uint16_t u16;
// uint32_t u32;
// uint64_t u64;
// int8_t i8;
// int16_t i16;
// int32_t i32;
// int64_t i64;
// float32_t f32;
// float64_t f64;
// string s;
//charArrayDummy cc;
//c2 >> u8 >> u16 >> u32 >> u64 >> i8 >> i16 >> i32 >> i64 >> f32 >> f64 >> s;// >> cc;
// cout<<u8<<endl;
// cout<<u16<<endl;
// cout<<u32<<endl;
// cout<<u64<<endl;
// cout<<i8<<endl;
// cout<<i16<<endl;
// cout<<i32<<endl;
// cout<<i64<<endl;
// cout<<f32<<endl;
// cout<<f64<<endl;
// cout<<s<<endl;
// cout <<"err_code:"<<c2.GetDecodeErrIdx() << endl;
cout<<"Mod:"<<__FILE__<<"\tLine:"<<__LINE__<<endl;
}