Object-C与标准C/C++混合编程例子
转自张运涛
objective-C/C++开发语言了。除了Cocoa相关框架和库之外,像Boost、STL以及标准ANSI C运行时库均可使用
1.引入头文件:
1 #include <vector>
2
3 #include <algorithm>
4
5 using namespace std;
2.将实现文件改名为.mm 告诉XCode启用gcc...
示例:
1 #include <iostream>
2
3 #include <vector>
4
5 #include <list>
6
7 #include <set>
8
9 #include <map>
10
11 #include <string>
12
13 #include <iterator>
14
15 #include <algorithm>
16
17 #include <functional>
18
19 using namespace std;
20
21
22
23 //函数重载
24
25 void test(int first ,int second)
26
27 {
28
29 NSLog(@"test_int_int called!");
30
31 }
32
33
34 void test(int first,float second)
35
36 {
37
38 NSLog(@"test_int_float called!");
39
40 }
41
42
43 //模板
44
45 template <class T>
46
47 void test(T parameter)
48
49 {
50
51 NSLog(@"template fun called!");
52
53 }
54
55
56 //结构与运算符重载
57
58 struct myNum
59
60 {
61
62 int num;
63
64 };
65
66
67 int operator+(myNum first , myNum second)
68
69 {
70
71 return first.num+second.num;
72
73 }
74
75
76 //类的使用
77
78 class CPoint {
79
80
81 private:
82
83 int x,y;
84
85 public:
86
87 //构造与析构函数
88
89 CPoint(int x,int y);
90
91 CPoint(const CPoint& pt);
92
93 ~CPoint(){NSLog(@"destruction fun called");}
94
95 //属性与析构
96
97 void setX(int x){this->x=x;}
98
99 void setY(int y){this->y=y;}
100
101 int getX(){return x;}
102
103 int getY(){return y;}
104
105 //输出
106
107 void outPut();
108
109 friend ostream& operator<<( ostream & out, CPoint & target);
110
111 //算符重载
112
113 CPoint operator+(CPoint &target);
114
115 CPoint operator-(CPoint &target);
116
117 void operator+=(CPoint &target);
118
119 void operator-=(CPoint &target);
120
121 };
122
123 //类方法实现部分
124
125 CPoint::CPoint(int x,int y)
126
127 {
128
129 this->x=x;
130
131 this->y=y;
132
133 }
134
135 CPoint::CPoint(const CPoint& pt)
136
137 {
138
139 x=pt.x;
140
141 y=pt.y;
142
143 }
144
145 CPoint CPoint::operator+(CPoint &target)
146
147 {
148
149 return CPoint(x+target.getX(),y+target.getY());
150
151 }
152
153 CPoint CPoint::operator-(CPoint &target)
154
155 {
156
157 return CPoint(x+target.getX(),y+target.getY());
158
159 }
160
161 void CPoint::operator+=(CPoint &target)
162
163 {
164
165 x+=target.getX();
166
167 y+=target.getY();
168
169 }
170
171 void CPoint::operator-=(CPoint &target)
172
173 {
174
175 x-=target.getX();
176
177 y-=target.getY();
178
179 }
180
181 void CPoint::outPut()
182
183 {
184
185 cout<<"("<<x<<","<<y<<")"<<endl;
186
187 }
188
189 //友无函数实现
190
191 ostream& operator<<( ostream & out, CPoint & target)
192
193 {
194
195 out<<"("<<target.x<<","<<target.y<<")"<<endl;
196
197 return out;
198
199 }
200
201 //控制台练习 for 标准C/C++
202
203 -(void)consoleStudyStandC_CPP
204
205 {
206
207 //1.
208
209 //函数重载练习
210
211 test(1,1);
212
213 test(1,1.5f);
214
215 //2.
216
217 //模板函数练习
218
219 test(1);
220
221 //3.
222
223 {
224
225 //结构及运算符重载示例
226
227 cout<<"结构与算符重载的使用:\n";
228
229 myNum num1,num2;
230
231 num1.num=3;
232
233 num2.num=2;
234
235 cout<<num1+num2<<endl;
236
237 }
238
239
240 //4.
241
242 {
243
244 //自定义类的使用:
245
246 cout<<"自定义类CPoint的使用...\n";
247
248 CPoint pt1(10,10);
249
250 pt1.outPut();
251
252 pt1.setX(20);
253
254 pt1.setY(20);
255
256 cout<<pt1;
257
258 CPoint pt2(CPoint(30,30));
259
260 CPoint temp=pt1+pt2;
261
262 cout<<temp<<endl;
263
264 }
265
266
267 //5.
268
269 {
270
271 //标准string使用
272
273 cout<<"string 的使用...\n";
274
275 string str="zhangyuntao in 2010.8.12 ";
276
277 str+="hehe -----张运涛\n";
278
279 cout<<str;
280
281 cout<<"使用c_str...\n";
282
283 cout<<str.c_str();
284
285
286
287 //stringWithUTF8String:
288
289 //Returns a string created by copying the data from a given C array of UTF8-encoded bytes.
290
291 cout<<"从string转成NSString:";
292
293 NSLog( [NSString stringWithUTF8String:str.c_str()] );
294
295
296
297 //cocoa Foundational NSString使用:
298
299 NSString *istr=[NSString stringWithString:@"zhangyuntao 张运涛."];
300
301 str=[istr cStringUsingEncoding: NSUTF8StringEncoding];
302
303 cout<<"从NSString转成string: "<<str<<endl;
304
305 cout<<endl;
306
307 }
308
309
310 //6.
311
312 {
313
314 cout<<"vector 一般用法:"<<endl;
315
316 //vector容器
317
318 vector<int> col;
319
320 for(int i=0;i<10;i++)
321
322 col.push_back(rand()%100);
323
324 //输出容器元素
325
326 for(int j=0;j<10;j++)
327
328 NSLog(@"%d",col[j]);
329
330 //使用C++标准输出流来输出容器元素
331
332 for(int k=0;k<10;k++)
333
334 cout<<col[k]<<"";
335
336 cout<<endl;
337
338 //标准sort算法的使用
339
340 NSLog(@"After sorting ...:\n");
341
342 sort(col.begin(),col.end());
343
344 //迭代器的使用
345
346 vector<int>::iterator p;
347
348 for(p=col.begin();p!=col.end();p++)
349
350 NSLog(@"%d",*p);
351
352 //标准find算法的使用
353
354 p=find(col.begin(), col.end(), 20);
355
356 p!=col.end() ? NSLog(@"The num 20 is in the vector") :NSLog(@"The num 20 is not in the vector");
357
358 cout<<endl;
359
360 }
361
362 {
363
364 //用vector构建二维数组
365
366 vector< vector<int> > col2;
367
368 for(int i=0;i<10;i++)
369
370 {
371
372 vector<int> temp;
373
374 for(int j=0;j<10;j++)
375
376 temp.push_back(rand()%90+10);
377
378 //用函数对象来进行排序
379
380 sort(temp.begin(),temp.end(),greater<int>());
381
382 col2.push_back(temp);
383
384 }
385
386 //输出
387
388 cout<<"vector 二维数组的使用:\n";
389
390 for(int k=0;k<10;k++)
391
392 copy(col2[k].begin(), col2[k].end(),ostream_iterator<int>(cout,"")),cout<<"\n";
393
394 cout<<endl;
395
396 }
397
398 //7.
399
400 //list使用
401
402 {
403
404 list<int> col;
405
406 for(int i=0;i<20;i++)
407
408 col.push_back(rand()%30);
409
410 col.sort();
411
412 col.erase(unique(col.begin(),col.end()) , col.end());
413
414 cout<<"list 容器使用:\n";
415
416 cout<<"after sort and unique, there is"<<col.size()<<" elements in the list\n";
417
418 copy(col.begin(), col.end(),ostream_iterator<int>(cout,""));
419
420 cout<<endl<<endl;
421
422 }
423
424 //8.
425
426 //set使用
427
428 {
429
430 set<int> col;
431
432 for(int i=0;i<10;i++) col.insert(rand()%90+10);
433
434 cout<<"Set 容器使用:\n";
435
436 copy(col.begin(), col.end(),ostream_iterator<int>(cout,""));
437
438 cout<<endl<<endl;
439
440 }
441
442 //9.
443
444 //map使用:
445
446 {
447
448 map<int,string> col;
449
450 col[1]="iPod";
451
452 col[2]="iPhone";
453
454 col[3]="iTouch";
455
456 cout<<"map 容器使用:\n";
457
458 for(map<int,string>::iterator p=col.begin();p!=col.end();p++)
459
460 cout<<"("<<p->first<<","<<p->second<<")"<<endl;
461
462 cout<<endl;
463
464 }
465
466 //10.
467
468 //文件读写操作
469
470 {
471
472 ofstream fout;
473
474 fout.open("1.txt");
475
476 //默认目录为根目录,非当前程序目录,切记!
477
478 for(int i=0;i<10;i++)
479
480 fout<<i<<"";
481
482 fout.close();
483
484 cout<<"Write to file succeed!"<<endl;
485
486 ifstream fin;
487
488 fin.open("1.txt");
489
490 int num;
491
492 while(fin>>num)
493
494 cout<<num<<"";
495
496 cout<<endl<<"Read finished!"<<endl;
497
498 fin.close();
499
500 }
501
502 }