张运涛

c++

   :: 首页 :: 联系 :: 聚合  :: 管理

常用链接

留言簿(4)

搜索

  •  

最新评论

说明:
objective-C/C++开发语言了。除了Cocoa相关框架和库之外,像Boost、STL以及标准ANSI C运行时库均可使用。不过大家在使用这些库时还得注意自己程序的运行环境。 --------------------zhangyuntao 2010.8.12
方式:
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();

}

}

//程序运行结果:

2010-08-12 11:55:09.102 test[2550:207] test_int_int called!

2010-08-12 11:55:09.106 test[2550:207] test_int_float called!

2010-08-12 11:55:09.108 test[2550:207] template fun called!

结构与算符重载的使用:

5

自定义类CPoint的使用...

(10,10)

(20,20)

(50,50)


2010-08-12 11:55:09.113 test[2550:207] destruction fun called

2010-08-12 11:55:09.115 test[2550:207] destruction fun called

2010-08-12 11:55:09.117 test[2550:207] destruction fun called

string 的使用...

zhangyuntao in 2010.8.12 hehe         -----张运涛

使用c_str...

zhangyuntao in 2010.8.12 hehe         -----张运涛

string转成NSString:2010-08-12 11:55:09.119 test[2550:207] zhangyuntao in 2010.8.12 hehe         -----张运涛

NSString转成string: zhangyuntao 张运涛.


vector 一般用法:

2010-08-12 11:55:09.122 test[2550:207] 53

2010-08-12 11:55:09.124 test[2550:207] 29

2010-08-12 11:55:09.126 test[2550:207] 97

2010-08-12 11:55:09.128 test[2550:207] 85

2010-08-12 11:55:09.130 test[2550:207] 62

2010-08-12 11:55:09.132 test[2550:207] 76

2010-08-12 11:55:09.134 test[2550:207] 13

2010-08-12 11:55:09.136 test[2550:207] 93

2010-08-12 11:55:09.138 test[2550:207] 89

2010-08-12 11:55:09.140 test[2550:207] 80

53 29 97 85 62 76 13 93 89 80 

2010-08-12 11:55:09.143 test[2550:207] After sorting ...:

2010-08-12 11:55:09.145 test[2550:207] 13

2010-08-12 11:55:09.147 test[2550:207] 29

2010-08-12 11:55:09.149 test[2550:207] 53

2010-08-12 11:55:09.151 test[2550:207] 62

2010-08-12 11:55:09.153 test[2550:207] 76

2010-08-12 11:55:09.155 test[2550:207] 80

2010-08-12 11:55:09.157 test[2550:207] 85

2010-08-12 11:55:09.159 test[2550:207] 89

2010-08-12 11:55:09.161 test[2550:207] 93

2010-08-12 11:55:09.163 test[2550:207] 97

2010-08-12 11:55:09.165 test[2550:207] The num 20 is not in the vector


vector 二维数组的使用:

87 84 75 75 73 45 42 41 22 18 

97 87 80 57 57 49 46 35 17 15 

88 87 78 73 59 39 34 32 20 13 

87 81 66 61 54 54 53 27 22 14 

95 90 77 63 57 41 36 29 15 13 

99 83 80 76 74 66 47 43 27 11 

97 94 80 68 61 47 36 19 18 15 

67 57 51 47 45 40 36 35 28 16 

97 82 79 76 61 59 54 17 11 10 

82 78 77 46 38 31 24 23 17 15 


list 容器使用:

after sort and unique, there is14 elements in the list

3 6 9 10 11 14 15 16 17 18 21 22 24 28 


Set 容器使用:

21 29 31 35 45 61 64 80 86 97 


map 容器使用:

(1,iPod)

(2,iPhone)

(3,iTouch)


Write to file succeed!

0 1 2 3 4 5 6 7 8 9 

Read finished!

2010-08-12 11:55:09.183 test[2550:207] DrawRect 

Terminating in response to SpringBoard's termination.


[Session started at 2010-08-12 11:59:01 +0800.]

2010-08-12 11:59:09.099 test[2564:207] test_int_int called!

2010-08-12 11:59:09.103 test[2564:207] test_int_float called!

2010-08-12 11:59:09.105 test[2564:207] template fun called!

结构与算符重载的使用:

5

自定义类CPoint的使用...

(10,10)

(20,20)

(50,50)


2010-08-12 11:59:09.108 test[2564:207] destruction fun called

2010-08-12 11:59:09.110 test[2564:207] destruction fun called

2010-08-12 11:59:09.112 test[2564:207] destruction fun called

string 的使用...

zhangyuntao in 2010.8.12 hehe         -----张运涛

使用c_str...

zhangyuntao in 2010.8.12 hehe         -----张运涛

string转成NSString:2010-08-12 11:59:09.114 test[2564:207] zhangyuntao in 2010.8.12 hehe         -----张运涛

NSString转成string: zhangyuntao 张运涛.


vector 一般用法:

2010-08-12 11:59:09.116 test[2564:207] 33

2010-08-12 11:59:09.118 test[2564:207] 85

2010-08-12 11:59:09.120 test[2564:207] 10

2010-08-12 11:59:09.122 test[2564:207] 35

2010-08-12 11:59:09.123 test[2564:207] 46

2010-08-12 11:59:09.125 test[2564:207] 65

2010-08-12 11:59:09.127 test[2564:207] 56

2010-08-12 11:59:09.129 test[2564:207] 52

2010-08-12 11:59:09.131 test[2564:207] 48

2010-08-12 11:59:09.133 test[2564:207] 65

33 85 10 35 46 65 56 52 48 65 

2010-08-12 11:59:09.135 test[2564:207] After sorting ...:

2010-08-12 11:59:09.137 test[2564:207] 10

2010-08-12 11:59:09.139 test[2564:207] 33

2010-08-12 11:59:09.141 test[2564:207] 35

2010-08-12 11:59:09.143 test[2564:207] 46

2010-08-12 11:59:09.145 test[2564:207] 48

2010-08-12 11:59:09.147 test[2564:207] 52

2010-08-12 11:59:09.149 test[2564:207] 56

2010-08-12 11:59:09.151 test[2564:207] 65

2010-08-12 11:59:09.152 test[2564:207] 65

2010-08-12 11:59:09.154 test[2564:207] 85

2010-08-12 11:59:09.156 test[2564:207] The num 20 is not in the vector


vector 二维数组的使用:

83 79 73 55 47 31 26 16 13 10 

98 90 75 70 66 66 61 54 53 18 

67 62 48 45 43 34 29 27 26 19 

98 79 73 62 52 35 25 20 17 16 

95 95 57 53 36 35 29 27 26 12 

75 70 68 60 60 57 47 25 15 14 

97 94 81 57 24 21 20 19 18 17 

99 96 84 81 75 63 54 52 49 18 

94 90 85 82 80 75 73 53 44 24 

79 77 75 74 46 42 35 26 24 21 


list 容器使用:

after sort and unique, there is12 elements in the list

0 2 9 10 11 12 13 17 20 21 28 29 


Set 容器使用:

24 32 33 40 47 50 51 85 89 96 


map 容器使用:

(1,iPod)

(2,iPhone)

(3,iTouch)


Write to file succeed!

0 1 2 3 4 5 6 7 8 9 

Read finished!


 

 


posted on 2010-08-12 09:13 张运涛 阅读(15713) 评论(0)  编辑 收藏 引用 所属分类: iphone

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理