服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - NDK 数据结构之队列与栈等的实现

NDK 数据结构之队列与栈等的实现

2021-06-07 15:31chuyouyinghe C/C++

这篇文章主要介绍了NDK 数据结构之队列与栈等的实现的相关资料,希望通过本文大家能理解掌握这部分内容,需要的朋友可以参考下

NDK 数据结构之队列与栈等的实现

com_tz_ndk_cpp_NDKCpp.h

?
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
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_tz_ndk_cpp_NDKCpp */
 
#ifndef _Included_com_tz_ndk_cpp_NDKCpp
#define _Included_com_tz_ndk_cpp_NDKCpp
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:   com_tz_ndk_cpp_NDKCpp
 * Method:  callCppTest
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueue
 (JNIEnv *, jobject);
 
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueuePriority
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStack
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppList
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListDelete
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListInsert
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSet
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetReverse
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetSort
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetFind
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiSet
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMap
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapDelete
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapFind
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiMap
    (JNIEnv *, jobject);
 
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorCopy
    (JNIEnv *, jobject);
 
#ifdef __cplusplus
}
#endif
#endif

com_tz_ndk_cpp_NDKCpp.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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include <iostream>
#include <string>
#include <android/log.h>
#include "com_tz_ndk_cpp_NDKCpp.h"
 
using namespace std;
 
//1.C++语言:queue队列-基本使用
#include <queue>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueue
    (JNIEnv *, jobject){
  //初始化
  queue<char> q;
  //添加元素
  q.push('A');
  q.push('B');
  q.push('C');
  //添加头部
//  q.front() = 'z';
  //添加尾部
//  q.back() = 'D';
 
  //删除操作
  while (!q.empty()){
    __android_log_print(ANDROID_LOG_INFO,"main","值: %c",q.front());
    //删除
    q.pop();
  }
}
 
 
//2.C++语言:queue队列-优先级
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppQueuePriority
    (JNIEnv *, jobject){
  //2.1 添加元素(默认是按照添加的顺序排列)
//  queue<int> q;
//  q.push(10);
//  q.push(50);
//  q.push(20);
//  q.push(5);
//  //打印
//  while (!q.empty()){
//    __android_log_print(ANDROID_LOG_INFO,"main","值: %d",q.front());
//    q.pop();
//  }
 
  //2.2 最大值优先级队列(从大到小排列)
//  priority_queue<int> pq1;
//  pq1.push(10);
//  pq1.push(50);
//  pq1.push(20);
//  pq1.push(5);
//  while (!pq1.empty()){
//    __android_log_print(ANDROID_LOG_INFO,"main","值: %d",pq1.top());
//    pq1.pop();
//  }
 
  //2.3 最小值优先级队列
  //注意:不同额编译器对语法检查有差别
  //在AS中进行NDK开发>>符号认为运算符,所以为了避免出现这样的情况,请用空格分离'> >'
  priority_queue<int,vector<int>,greater<int> > pq1;
  pq1.push(10);
  pq1.push(50);
  pq1.push(20);
  pq1.push(5);
  while (!pq1.empty()){
    __android_log_print(ANDROID_LOG_INFO,"main","值: %d",pq1.top());
    pq1.pop();
  }
}
 
 
 
//3.C++语言:stack栈-基本使用
#include <stack>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppStack
    (JNIEnv *, jobject){
  stack<int> st;
  st.push(10);
  st.push(20);
  st.push(30);
 
  while (!st.empty()){
    __android_log_print(ANDROID_LOG_INFO,"main","值: %d",st.top());
    st.pop();
  }
}
 
 
//4.C++语言:list-基本使用
#include <list>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppList
    (JNIEnv *, jobject){
  list<int> lt;
  //从头部添加
  lt.push_front(10);
  lt.push_front(20);
  lt.push_front(30);
  //从尾部添加
  lt.push_back(40);
  lt.push_back(50);
  lt.push_back(60);
 
  //循环遍历
  for (list<int>::iterator it = lt.begin() ; it != lt.end() ; it++){
    __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it);
  }
 
  list<int>::iterator it = lt.begin();
  //连续相加允许(++)
  //支持'++'、'--'运算符
  it++;
  it--;
  //注意:不支持间断
  //不支持'+'、'-'运算度
//  it = it - 1;
 
}
 
 
//5.C++语言:list-删除
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListDelete
    (JNIEnv *, jobject){
  list<int> lt;
  //从头部添加
  lt.push_front(10);
  lt.push_front(20);
  lt.push_front(30);
  //从尾部添加
  lt.push_back(40);
  lt.push_back(50);
  lt.push_back(60);
 
  //方式一
//  list<int>::iterator it = lt.begin();
//  it++;
//  //删除:删除第二个元素
//  lt.erase(it);
 
  //方式二
  //删除第二个元素(直接根据内容删除)
//  lt.remove(20);
 
  //方式三:区间删除
  //开始位置
  list<int>::iterator it_begin = lt.begin();
  //结束位置
  list<int>::iterator it_end = lt.begin();
  it_end++;
  it_end++;
  //删除元素(如果已经被删除的元素不能够在删除)
  lt.erase(it_begin,it_end);
 
  //循环遍历
  for (list<int>::iterator it = lt.begin() ; it != lt.end() ; it++){
    __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it);
  }
}
 
 
 
//6.C++语言:list-插入
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppListInsert
    (JNIEnv *, jobject){
  list<int> lt;
  //从尾部添加
  lt.push_back(40);
  lt.push_back(50);
  lt.push_back(60);
 
  //插入
  lt.insert(lt.begin(),30);
  //循环遍历
  for (list<int>::iterator it = lt.begin() ; it != lt.end() ; it++){
    __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it);
  }
}
 
 
//7.C++语言:set-基本使用(元素唯一)-默认从小到大排列
//特点一:元素唯一
//特点二:默认从小到大排列
#include <set>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSet
    (JNIEnv *, jobject){
  set<int> st;
  st.insert(40);
  st.insert(10);
  st.insert(30);
  st.insert(20);
 
  //删除
  set<int>::iterator it = st.begin();
  st.erase(it);
 
  for (set<int>::iterator it = st.begin() ; it != st.end() ; it++){
    __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it);
  }
 
}
 
 
//8.C++语言:set-基本使用(元素唯一)-从大到小排列
//set<int,greater<int>>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetReverse
    (JNIEnv *, jobject){
  set<int,greater<int> > st;
  st.insert(40);
  st.insert(10);
  st.insert(30);
  st.insert(20);
 
 
  for (set<int>::iterator it = st.begin() ; it != st.end() ; it++){
    __android_log_print(ANDROID_LOG_INFO,"main","值:%d",*it);
  }
}
 
 
 
//9.C++语言:set-自定义排序规则
//需求:根据学生的成绩进行排序
class Student{
private:
  char* name;
  int score;
public:
  Student(char* name,int score){
    this->name = name;
    this->score = score;
  }
  int getScore(){
    return this->score;
  }
  void printStudent(){
    __android_log_print(ANDROID_LOG_INFO,"main","姓名: %s, 成绩: %d",this->name,this->score);
  }
};
//仿函数
struct Soft{
  //方式一:不写常量
//  bool operator()(Student &left,Student &right){
//    return left.getScore() < right.getScore();
//  }
  //方式二:const修饰
  bool operator()(const Student &left,const Student &right){
    //类型转换
    Student stu_left = const_cast<Student&>(left);
    Student stu_right = const_cast<Student&>(right);
    return stu_left.getScore() < stu_right.getScore();
  }
};
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetSort
    (JNIEnv *, jobject){
  set<Student,Soft> st;
  st.insert(Student("小宇",50));
  st.insert(Student("梦想",59));
  st.insert(Student("song",55));
  st.insert(Student("远方",58));
  st.insert(Student("石桥中化妖",40));
 
  for (set<Student>::iterator it = st.begin() ; it != st.end() ; it++){
    Student stu = const_cast<Student&>(*it);
    stu.printStudent();
  }
 
}
 
//10.C++语言:set-查找
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppSetFind
    (JNIEnv *, jobject){
  set<int> st;
  st.insert(10);
  st.insert(20);
  st.insert(30);
  st.insert(40);
  st.insert(50);
  st.insert(60);
  st.insert(70);
 
  //方式一
  //查找等于30元素
  //st.find(2);
 
  //方式二
  //查找等于或者小余35元素
  //如果存在你输入的值30,那么就返回当前值30(例如:30)
  //如果不存在你查找的值31,那么返回大于31的最近的一个元素指针
  set<int>::iterator it_lower = st.lower_bound(31);
  __android_log_print(ANDROID_LOG_INFO,"main","查找结果: %d",*it_lower);
 
  //如果存在你查找的值30,那么就返回大于30最近的一个元素指针40
  //如果不存在你查找的值31,那么就返回大于31最近的一个元素的指针40
  set<int>::iterator it_upper = st.upper_bound(31);
  __android_log_print(ANDROID_LOG_INFO,"main","查找结果: %d",*it_upper);
 
  //方式三:既要返回最小也要最大
  pair<set<int>::iterator,set<int>::iterator> p = st.equal_range(30);
  //获取返回的元素
  __android_log_print(ANDROID_LOG_INFO,"main","小: %d",*p.first);
  __android_log_print(ANDROID_LOG_INFO,"main","大: %d",*p.second);
 
}
 
 
//11.C++语言:multiset-基本使用
//允许存储重复元素
//默认升序排列
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiSet
    (JNIEnv *, jobject){
  //升序
//  multiset<int> mst;
//  mst.insert(10);
//  mst.insert(20);
//  mst.insert(30);
//  mst.insert(10);
//
//  for (multiset<int>::iterator it = mst.begin() ; it != mst.end() ; it++){
//    __android_log_print(ANDROID_LOG_INFO,"main","值: %d",*it);
//  }
 
  //降序
//  multiset<int,greater<int> > mst;
//  mst.insert(10);
//  mst.insert(20);
//  mst.insert(30);
//  mst.insert(10);
//
//  for (multiset<int>::iterator it = mst.begin() ; it != mst.end() ; it++){
//    __android_log_print(ANDROID_LOG_INFO,"main","值: %d",*it);
//  }
 
  //自定义排序方式
  multiset<Student,Soft> mst;
  mst.insert(Student("小宇",50));
  mst.insert(Student("梦想",59));
  mst.insert(Student("song",55));
  mst.insert(Student("远方",58));
  mst.insert(Student("石桥中化妖",40));
  mst.insert(Student("Dream",40));
  for (multiset<Student>::iterator it = mst.begin() ; it != mst.end() ; it++){
    Student stu = const_cast<Student&>(*it);
    stu.printStudent();
  }
}
 
 
 
//12.C++语言:map-基本使用
#include <map>
#include <string>
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMap
    (JNIEnv *, jobject){
  map<int,string> mp;
  //方式一:插入数据pair
  mp.insert(pair<int,string>(01,"陈国军"));
  mp.insert(pair<int,string>(02,"Mr.Sunday"));
  mp.insert(pair<int,string>(03,"Studio"));
  mp.insert(pair<int,string>(04,"余祚宁"));
 
  //方式二:如果key存在,那么就不添加同时不覆盖,如果不存在,就添加
  pair<map<int,string>::iterator, bool> result = mp.insert(map<int,string>::value_type(04,"相约98"));
  if(result.second){
    __android_log_print(ANDROID_LOG_INFO,"main","添加成功!");
  }else{
    __android_log_print(ANDROID_LOG_INFO,"main","已存在,添加失败!");
  }
 
  //方式三:
  mp.insert(make_pair(05,"定定"));
 
  //方式四:如果key存在,重复添加会覆盖,如果不存在,那就直接添加
  mp[5] = "石桥中化妖";
  mp[6] = "定定";
 
  for (map<int,string>::iterator it = mp.begin() ; it != mp.end() ; it++){
    //获取key:it->first
    __android_log_print(ANDROID_LOG_INFO,"main","key: %d",it->first);
    //获取value:it->second
    __android_log_print(ANDROID_LOG_INFO,"main","value: %s",it->second.c_str());
  }
 
}
 
 
 
//13.C++语言:map-删除
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapDelete
    (JNIEnv *, jobject){
  map<int,string> mp;
  mp.insert(pair<int,string>(01,"陈国军"));
  mp.insert(pair<int,string>(02,"Mr.Sunday"));
  mp.insert(pair<int,string>(03,"Studio"));
  mp.insert(pair<int,string>(04,"余祚宁"));
 
  //删除
  map<int,string>::iterator it = mp.begin();
  mp.erase(it);
 
  //打印
  for (map<int,string>::iterator it = mp.begin() ; it != mp.end() ; it++){
    //获取key:it->first
    __android_log_print(ANDROID_LOG_INFO,"main","key: %d",it->first);
    //获取value:it->second
    __android_log_print(ANDROID_LOG_INFO,"main","value: %s",it->second.c_str());
  }
}
 
 
 
//14.C++语言:map-查找(equal_range)
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMapFind
    (JNIEnv *, jobject){
  map<int,string> mp;
  mp.insert(pair<int,string>(01,"陈国军"));
  mp.insert(pair<int,string>(02,"Mr.Sunday"));
  mp.insert(pair<int,string>(03,"Studio"));
  mp.insert(pair<int,string>(04,"余祚宁"));
 
  //获取大于或者等于2的元素
  pair<map<int,string>::iterator,map<int,string>::iterator> p = mp.equal_range(2);
  //判断是否存在元素
  if(p.first != mp.end()){
    //等于2
    //获取key:it->first
    __android_log_print(ANDROID_LOG_INFO,"main","key: %d",p.first->first);
    //获取value:it->second
    __android_log_print(ANDROID_LOG_INFO,"main","value: %s",p.first->second.c_str());
 
    //大于2元素
    //获取key:it->first
    __android_log_print(ANDROID_LOG_INFO,"main","key: %d",p.second->first);
    //获取value:it->second
    __android_log_print(ANDROID_LOG_INFO,"main","value: %s",p.second->second.c_str());
  }
}
 
 
 
//15.C++语言:multimap-一对多
//需求:一个用户对应多个订单
class Order{
private:
  char* name;
  int num;
public:
  Order(char* name,int num){
    this->name = name;
    this->num = num;
  }
  void printOrder(){
    __android_log_print(ANDROID_LOG_INFO,"main","订单名称:%s, 订单号:%d",this->name,this->num);
  }
};
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppMultiMap
    (JNIEnv *, jobject){
 
  multimap<string,Order> mst;
  mst.insert(make_pair("陈国军",Order("男士外套",01)));
  mst.insert(make_pair("陈国军",Order("户外跑鞋",02)));
 
  mst.insert(make_pair("梦想",Order("女士外套",03)));
  mst.insert(make_pair("梦想",Order("女士高跟鞋",02)));
 
  mst.insert(make_pair("Dream",Order("女士纱衣",03)));
  mst.insert(make_pair("Dream",Order("女士布鞋",02)));
  mst.insert(make_pair("Dream",Order("女士外套",02)));
  mst.insert(make_pair("Dream",Order("女士裤子",02)));
 
  //遍历
//  for (multimap<string,Order>::iterator it = mst.begin() ; it != mst.end() ; it++){
//    //获取key:it->first
//    __android_log_print(ANDROID_LOG_INFO,"main","key: %s",it->first.c_str());
//    //获取value:it->second
//    Order order = const_cast<Order&>(it->second);
//    order.printOrder();
//  }
 
 
  //需求:只获取"梦想"订单
  //获取订单的数量
  int count = mst.count("梦想");
  //打印"梦想"订单:找到
  multimap<string,Order>::iterator it = mst.find("梦想");
  //循环遍历打印
  //计数
  int i = 0;
  while (it != mst.end() && i < count){
    __android_log_print(ANDROID_LOG_INFO,"main","key: %s",it->first.c_str());
    Order order = const_cast<Order&>(it->second);
    order.printOrder();
    i++;
    it++;
  }
}
 
 
 
//16.C++语言:vector-浅拷贝和深拷贝
class User{
private:
  char* name;
  int age;
public:
  //浅拷贝(默认就是浅拷贝)
  User(char* name,int age){
    //动态分配内存
    this->name = new char[strlen(name)+1];
    strcpy(this->name,name);
 
    this->age = age;
  }
  ~User(){
    if(this->name != NULL){
      delete[] this->name;
      this->name = NULL;
      this->age = 0;
    }
  }
  void printUser(){
    __android_log_print(ANDROID_LOG_INFO,"main","名称:%s, 年龄: %d",this->name,this->age);
  }
 
  //深拷贝
  User(const User &user){
    //先释放内存
    if(this->name != NULL){
      delete[] this->name;
      this->name = NULL;
      this->age = 0;
    }
    //动态分配内存
    this->name = new char[strlen(user.name)+1];
    strcpy(this->name,user.name);
    this->age = user.age;
  }
 
  User& operator=(User &user){
    if(this->name != NULL){
      delete[] this->name;
      this->name = NULL;
      this->age = 0;
    }
    //动态分配内存
    this->name = new char[strlen(user.name)+1];
    strcpy(this->name,user.name);
    this->age = user.age;
    return *this;
  }
 
};
//class User{
//private:
//  string* name;
//  int age;
//public:
//  //浅拷贝(默认就是浅拷贝)
//  User(string name,int age){
//    //动态分配内存
//    this->name = new string(const_cast<string&>(name));
//    this->age = age;
//  }
//  ~User(){
//    if(this->name != NULL){
//      delete this->name;
//      this->name = NULL;
//      this->age = 0;
//    }
//  }
//  void printUser(){
//    __android_log_print(ANDROID_LOG_INFO,"main","名称:%s, 年龄: %d",this->name,this->age);
//  }
//
//  //深拷贝
//  User(const User &user){
//    //先释放内存
//    if(this->name != NULL){
//      delete this->name;
//      this->name = NULL;
//      this->age = 0;
//    }
//    //动态分配内存
//    this->name = new string(const_cast<string&>(user.name));
//    this->age = age;
//  }
//
//  User& operator=(User &user){
//    if(this->name != NULL){
//      delete this->name;
//      this->name = NULL;
//      this->age = 0;
//    }
//    //动态分配内存
//    this->name = new string(const_cast<string&>(user.name));
//    this->age = age;
//    return *this;
//  }
//
////};
JNIEXPORT void JNICALL Java_com_tz_ndk_cpp_NDKCpp_callCppVectorCopy
    (JNIEnv *, jobject){
  vector<User> vt;
  User user("石桥中化妖",18);
  //实参作为形参,需要调用拷贝函数,然后默认是浅拷贝
  vt.push_back(user);
 
  //解决方案:深拷贝
  for (vector<User>::iterator it = vt.begin() ; it != vt.end() ; it++){
    User order = const_cast<User&>(*it);
    order.printUser();
  }
 
  //作业:string如何深拷贝(稍微麻烦一点)
}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/chuyouyinghe/article/details/53287015

延伸 · 阅读

精彩推荐