iphone开发之C++和Objective-C混编 如何在xcode中用C++的STL

简介:

 

http://download.csdn.net/source/2803162

已经将完整的工程上传到csdn的资源中了,手动贴出链接。

 

如有其它的交流,欢迎相互交流。

 

看下本blog后续贴出的转载的那篇翻译的官方文档,跑一下我给的这个例子,混编应该没什么大碍了吧,自吹一下,哈哈哈。

 

iphone开发里面xcode使用stl其实也不是有那么困难的,我例子只是基本的使用方式说明,高级的还要自己努力。

废话不多,代码放出。

不知道怎么上传附件,放到资源里面自己去下吧。

//单独的一个c++类,和普通的写法没什么两样

新建一个工程OCplusplus,将头文件和m文件改成如下内容,记得要改名成mm文件

 

#import <UIKit/UIKit.h>

 

@interface OCplusplusAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

}

 

@property (nonatomicretainIBOutlet UIWindow *window;

NSString* test();

@end

 

#import "OCplusplusAppDelegate.h"

#import "cplusplus.h"

#import "hellocpp.h"

#import "useSTL.h"

//这个文件就是要用到c++的东西,就要改后缀名

@implementation OCplusplusAppDelegate

 

@synthesize window;

 

//试试,自己用自己知道。

 

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

 

    // Override point for customization after application launch

    [window makeKeyAndVisible];

 

cplusplus *cpp = new cplusplus();

NSLog(@"%d",cpp-&gt;testadd(46));

delete cpp;

 

hello *h = new hello();

NSLog(@"%d",h-&gt;testadd(1122));

h-&gt;sayhello();

NSLog(@"%@",h-&gt;OCsayello());

NSLog(@"%@",test());

delete h;

 

useSTL *us = new useSTL();

us-&gt;testSTL();

delete us;//干什么都要干净利落,不要拉下东西,记得释放内存

}

 

NSString* test()

{//这就是oc类的c++写法

NSLog(@"jhdglajdklfja");

return @"ttttttttt";

}

 

- (void)dealloc {

    [window release];

    [super dealloc];

}

 

 

@end

//////////////////////////////////////////////////

新建一个文件cplusplus,也就是一个类,也是改名成mm文件

class cplusplus

{

public:

int testadd(int a, int b);

};

 

#import "cplusplus.h"

 

int cplusplus::testadd(int a,int b)

{

return a + b;

}

 

//////////////////////////////////////////////////

再新建一个文件hellocpp ,也是一个内嵌类,也需要改成mm文件

#import <Foundation/Foundation.h>

 

#import "cplusplus.h"

 

@interface hellocpp : NSObject {

//这是内嵌类,自己试试自己知道

class hello : public cplusplus {

 

public:

void sayhello();

NSString *OCsayello();

};

 

}

 

@end

 

 

 

#import "hellocpp.h"

#import "cplusplus.h"

 

@implementation hellocpp

 

void hello::sayhello()

{

printf("say hello in hello");

}

 

NSStringhello::OCsayello()

{//这种写法也可以,就会在oc类里也可以的。

NSLog(@"OC say hello");

return @"test";

}

@end

 

////////////////////////////////////////////

再新建一个文件useSTL,也是一个类,也需要改成mm文件

这回要使用的是c++中的stl的vector功能

//内嵌的类也是可以的

class useSTL {

public:

void testSTL();

};

//////////////////////////////////////////

 

 

#import "useSTL.h"

 

using namespace std;//这个也要写上才行

 

#import "vector"//记得头文件啊

#include <iostream>

 

void useSTL::testSTL()

{//这段代码是网上随意copy的,有点修改。

int n = 9;

vector<int>* vectors = new vector<int>[n];

int i;

for(i=0; i<n; i++)

{

for(int j=0; j&lt;i; j++)

{

int data;

cin>&gt;data;

vectors[i].push_back(j);

}

}

cout&lt;&lt;"共有"&lt;&lt;n&lt;&lt;"vector,各vector元素如下:"&lt;&lt;endl;

for(i=0; i&lt;n; i++)

{

cout&lt;&lt;""&lt;&lt;i+1&lt;&lt;"vector的元素:";

int j;

for(j=0; j&lt;vectors[i].size(); j++)

{

cout&lt;&lt;vectors[i][j]&lt;&lt;"\t";

}

cout&lt;&lt;endl;

}

delete [] vectors;

}

 

确保工程可以编译通过,看一下运行的结果。

 

运行结果:

[Session started at 2010-11-02 15:56:56 +0800.]

2010-11-02 15:56:58.345 OCplusplus[10303:207] 10

2010-11-02 15:56:58.350 OCplusplus[10303:207] 33

say hello in hello2010-11-02 15:56:58.353 OCplusplus[10303:207] OC say hello

2010-11-02 15:56:58.354 OCplusplus[10303:207] test

2010-11-02 15:56:58.354 OCplusplus[10303:207] jhdglajdklfja

2010-11-02 15:56:58.355 OCplusplus[10303:207] ttttttttt

共有9vector,各vector元素如下:

1vector的元素:

2vector的元素:0

3vector的元素:0 1

4vector的元素:0 1 2

5vector的元素:0 1 2 3

6vector的元素:0 1 2 3 4

7vector的元素:0 1 2 3 4 5

8vector的元素:0 1 2 3 4 5 6

9vector的元素:0 1 2 3 4 5 6 7










本文转自 arthurchen 51CTO博客,原文链接:http://blog.51cto.com/arthurchen/577910,如需转载请自行联系原作者
目录
相关文章
|
18天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
19 2
|
24天前
|
开发框架 Linux C语言
C、C++、boost、Qt在嵌入式系统开发中的使用
C、C++、boost、Qt在嵌入式系统开发中的使用
31 1
|
3天前
|
存储 搜索推荐 C++
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
【C++高阶(二)】熟悉STL中的map和set --了解KV模型和pair结构
|
3天前
|
设计模式 C语言 C++
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
【C++进阶(六)】STL大法--栈和队列深度剖析&优先级队列&适配器原理
|
3天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
3天前
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
3天前
|
编译器 C++
【C++进阶(三)】STL大法--vector迭代器失效&深浅拷贝问题剖析
【C++进阶(三)】STL大法--vector迭代器失效&深浅拷贝问题剖析
|
4天前
|
存储 算法 C语言
c++的学习之路:9、STL简介与string(1)
c++的学习之路:9、STL简介与string(1)
20 0
|
14天前
|
存储 算法 C++
【C++初阶】STL详解(九) priority_queue的使用与模拟实现
【C++初阶】STL详解(九) priority_queue的使用与模拟实现
20 0
|
14天前
|
存储 算法 编译器
【C++初阶】STL详解(三)vector的介绍与使用
【C++初阶】STL详解(三)vector的介绍与使用
34 0