您的位置:首页 > 移动开发 > Objective-C

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

2010-11-02 16:16 441 查看
 

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 (nonatomic, retain) IBOutlet 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->testadd(4, 6));
delete cpp;
 
hello *h = new hello();
NSLog(@"%d",h->testadd(11, 22));
h->sayhello();
NSLog(@"%@",h->OCsayello());
NSLog(@"%@",test());
delete h;
 
useSTL *us = new useSTL();
us->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");
}
 
NSString* hello::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>
;
int i;
for(i=0; i<n; i++)
{
for(int j=0; j<i; j++)
{
int data;
cin>>data;
vectors[i].push_back(j);
}
}
cout<<"共有"<<n<<"个vector,各vector元素如下:"<<endl;
for(i=0; i<n; i++)
{
cout<<"第"<<i+1<<"个vector的元素:";
int j;
for(j=0; j<vectors[i].size(); j++)
{
cout<<vectors[i][j]<<"/t";
}
cout<<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
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xcode iphone c++ c vector delete
相关文章推荐