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

Object-C与标准C/C++混合编程例子

2011-11-05 17:42 281 查看
转自张运涛

objective-C/C++开发语言了。除了Cocoa相关框架和库之外,像Boost、STL以及标准ANSI C运行时库均可使用

1.引入头文件:

#include <vector>

#include <algorithm>

using namespace std;


2.将实现文件改名为.mm 告诉XCode启用gcc...

示例:

#include <iostream>

#include <vector>

#include <list>

#include <set>

#include <map>

#include <string>

#include <iterator>

#include <algorithm>

#include <functional>

using namespace std;

//函数重载

void test(int first ,int second)

{

NSLog(@"test_int_int called!");

}

void test(int first,float second)

{

NSLog(@"test_int_float called!");

}

//模板

template <class T>

void test(T parameter)

{

NSLog(@"template fun called!");

}

//结构与运算符重载

struct myNum

{

int num;

};

int operator+(myNum first , myNum second)

{

return first.num+second.num;

}

//类的使用

class CPoint {

private:

int x,y;

public:

//构造与析构函数

CPoint(int x,int y);

CPoint(const CPoint& pt);

~CPoint(){NSLog(@"destruction fun called");}

//属性与析构

void setX(int x){this->x=x;}

void setY(int y){this->y=y;}

int getX(){return x;}

int getY(){return y;}

//输出

void outPut();

friend ostream& operator<<( ostream & out,  CPoint & target);

//算符重载

CPoint operator+(CPoint &target);

CPoint operator-(CPoint &target);

void  operator+=(CPoint &target);

void  operator-=(CPoint &target);

};

//类方法实现部分

CPoint::CPoint(int x,int y)

{

this->x=x;

this->y=y;

}

CPoint::CPoint(const CPoint& pt)

{

x=pt.x;

y=pt.y;

}

CPoint CPoint::operator+(CPoint &target)

{

return CPoint(x+target.getX(),y+target.getY());

}

CPoint CPoint::operator-(CPoint &target)

{

return CPoint(x+target.getX(),y+target.getY());

}

void  CPoint::operator+=(CPoint &target)

{

x+=target.getX();

y+=target.getY();

}

void  CPoint::operator-=(CPoint &target)

{

x-=target.getX();

y-=target.getY();

}

void CPoint::outPut()

{

cout<<"("<<x<<","<<y<<")"<<endl;

}

//友无函数实现

ostream& operator<<( ostream & out,  CPoint & target)

{

out<<"("<<target.x<<","<<target.y<<")"<<endl;

return out;

}

//控制台练习 for 标准C/C++

-(void)consoleStudyStandC_CPP

{

//1.

//函数重载练习

test(1,1);

test(1,1.5f);

//2.

//模板函数练习

test(1);

//3.

{

//结构及运算符重载示例

cout<<"结构与算符重载的使用:\n";

myNum num1,num2;

num1.num=3;

num2.num=2;

cout<<num1+num2<<endl;

}

//4.

{

//自定义类的使用:

cout<<"自定义类CPoint的使用...\n";

CPoint pt1(10,10);

pt1.outPut();

pt1.setX(20);

pt1.setY(20);

cout<<pt1;

CPoint pt2(CPoint(30,30));

CPoint temp=pt1+pt2;

cout<<temp<<endl;

}

//5.

{

//标准string使用

cout<<"string 的使用...\n";

string str="zhangyuntao in 2010.8.12 ";

str+="hehe         -----张运涛\n";

cout<<str;

cout<<"使用c_str...\n";

cout<<str.c_str();

//stringWithUTF8String:

//Returns a string created by copying the data from a given C array of UTF8-encoded bytes.

cout<<"从string转成NSString:";

NSLog( [NSString stringWithUTF8String:str.c_str()] );

//cocoa Foundational NSString使用:

NSString *istr=[NSString stringWithString:@"zhangyuntao 张运涛."];

str=[istr cStringUsingEncoding: NSUTF8StringEncoding];

cout<<"从NSString转成string: "<<str<<endl;

cout<<endl;

}

//6.

{

cout<<"vector 一般用法:"<<endl;

//vector容器

vector<int> col;

for(int i=0;i<10;i++)

col.push_back(rand()%100);

//输出容器元素

for(int j=0;j<10;j++)

NSLog(@"%d",col[j]);

//使用C++标准输出流来输出容器元素

for(int k=0;k<10;k++)

cout<<col[k]<<"";

cout<<endl;

//标准sort算法的使用

NSLog(@"After sorting ...:\n");

sort(col.begin(),col.end());

//迭代器的使用

vector<int>::iterator p;

for(p=col.begin();p!=col.end();p++)

NSLog(@"%d",*p);

//标准find算法的使用

p=find(col.begin(), col.end(), 20);

p!=col.end() ? NSLog(@"The num 20 is in the vector") :NSLog(@"The num 20 is not in the vector");

cout<<endl;

}

{

//用vector构建二维数组

vector< vector<int> > col2;

for(int i=0;i<10;i++)

{

vector<int> temp;

for(int j=0;j<10;j++)

temp.push_back(rand()%90+10);

//用函数对象来进行排序

sort(temp.begin(),temp.end(),greater<int>());

col2.push_back(temp);

}

//输出

cout<<"vector 二维数组的使用:\n";

for(int k=0;k<10;k++)

copy(col2[k].begin(), col2[k].end(),ostream_iterator<int>(cout,"")),cout<<"\n";

cout<<endl;

}

//7.

//list使用

{

list<int> col;

for(int i=0;i<20;i++)

col.push_back(rand()%30);

col.sort();

col.erase(unique(col.begin(),col.end()) , col.end());

cout<<"list 容器使用:\n";

cout<<"after sort and unique, there is"<<col.size()<<" elements in the list\n";

copy(col.begin(), col.end(),ostream_iterator<int>(cout,""));

cout<<endl<<endl;

}

//8.

//set使用

{

set<int> col;

for(int i=0;i<10;i++) col.insert(rand()%90+10);

cout<<"Set 容器使用:\n";

copy(col.begin(), col.end(),ostream_iterator<int>(cout,""));

cout<<endl<<endl;

}

//9.

//map使用:

{

map<int,string> col;

col[1]="iPod";

col[2]="iPhone";

col[3]="iTouch";

cout<<"map 容器使用:\n";

for(map<int,string>::iterator p=col.begin();p!=col.end();p++)

cout<<"("<<p->first<<","<<p->second<<")"<<endl;

cout<<endl;

}

//10.

//文件读写操作

{

ofstream fout;

fout.open("1.txt");

//默认目录为根目录,非当前程序目录,切记!

for(int i=0;i<10;i++)

fout<<i<<"";

fout.close();

cout<<"Write to file succeed!"<<endl;

ifstream fin;

fin.open("1.txt");

int num;

while(fin>>num)

cout<<num<<"";

cout<<endl<<"Read finished!"<<endl;

fin.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: