摘要:用途一:定义一种类型的别名,而不只是简单的宏替换。可以用作同时声明指针型的多个对象。比如:char* pa, pb;//这多数不符合我们的意图,它只声明了一个指向字符变量的指针,和一个字符变量;以下则可行:typedef char* PCHAR;PCHAR pa, pb; 用途二:用在旧的C代码中,帮助struct。以前的代码中,声明struct新对象时,必须要带上struct,即形式为:struct结构名对象名,如:struct tagPOINT1{int x;int y;};struct tagPOINT1 p1;而在C++中,则可以直接写:结构名对象名,即:tagPOINT1 p1;.. 阅读全文
转:c++ typedef关键字
2012-04-16 20:48 by youxin, 509 阅读, 0 推荐, 收藏,
摘要:typedef是一种特殊的声明方式,不过它与普通声明的含义大不相同。普通声明的主角是“变量”,它或是创建一个新变量或是对外文件变量使用前的声明;而typedef声明的主角则是“类型”,通过这个声明对一种数据类型引入新的名字。从引入新名字这个角度来说,typedef声明又和宏定义有些相似:用新名字代替已有的名字。接下来的叙述会看到这两者之间的区别。typedef是特殊的 正如一开始所说的那样,typedef是特殊的声明。最常见以及常用的方式如下:1 /* 代码段1 */2 struct stuinfo3 {4 char id[20];5 char name[20];6 ... 阅读全文
c++函数作为参数传递
2012-04-16 20:33 by youxin, 18046 阅读, 3 推荐, 收藏,
摘要:把函数作为参数传递,常用的方法之一是用typedef定义一个函数指针。#include<iostream>using namespace std;typedef int (*pf)(int,int); //此种方式最容易理解,定义了一个函数指针类型;函数名就是指针。int f(pf p,int a,int b){ return p(a,b);}int add(int a,int b){ return a+b;}int main(){ cout<<f(add,1,2)<<endl;//3}还可以这样:#include<iostream>using 阅读全文
c++ vector.clear()
2012-04-16 18:51 by youxin, 702 阅读, 0 推荐, 收藏,
摘要:vector::clearvoid clear ( );Clear content 清除全部的内容All the elements of the vector are dropped: their destructors are called, and then they are removed from thevectorcontainer, leaving the container with asizeof0.// clearing vectors#include <iostream>#include <vector>using namespace std;int 阅读全文
c++ 流的clear 与sync
2012-04-16 18:26 by youxin, 1504 阅读, 0 推荐, 收藏,
摘要:先看函数原型:ios::clearvoid clear ( iostate state = goodbit );Set error state flagsSets a new value for theerror control state.All the bits in thecontrol stateare replaced by the new ones; The value existing before the call has no effect.If the function is called withgoodbitas argument (which is the defau 阅读全文
C++ copy 函数
2012-04-16 17:18 by youxin, 10131 阅读, 0 推荐, 收藏,
摘要:先看下面的一段代码: vector<int> u(10,100); vector<int> v; copy(u.begin(),u.end(),v.begin()); for(vector<int>::iterator it=v.begin();it!=v.end();it++) { cout<<*it<<ends; }运行错误! 功能很简单,把vector u复制给v,但运行异常。为什么? vector<int> v;定义时定义时没有分配空间,copy不成功。应改为vector<int> v(u.size() 阅读全文
C++ 各种排序算法的区别
2012-04-15 21:55 by youxin, 528 阅读, 0 推荐, 收藏,
摘要:一、partition 算法可以把满足特定条件的元素放在区间的前部,并且返回一个迭代器,指向第一个不满足条件的元素。例如:我们要找出一个班级中成绩大于60分的学生,那么我们应该这样做。bool IsPass(int a){ return a>=60;}vector<int>::iterator Pass = partition(record.begin(),record.end(),IsPass);在此调用后,所有分数大于60分的学生都被放在record.begin()到Pass(不包括pass)之间,而分数小于60分的学生都被放在Pass到record.end()之间。2. 阅读全文
c++计算程序运行时间
2012-04-15 15:29 by youxin, 2516 阅读, 0 推荐, 收藏,
摘要:C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下: clock_t clock( void ); 这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对 它的定义: #ifndef _CLOCK_T_DEFINED typedef long clock_t; #define _CLOCK_... 阅读全文
c++ rbegin() 和rend()
2012-04-15 14:15 by youxin, 1928 阅读, 0 推荐, 收藏,
摘要:看下面的一段代码: for(vector<string>::iterator it=vec.rbegin();it!=vec.rend();it--) { cout<<*it<<ends; }编译有错,为什么?rbegin() rend()的返回类型不是vector<string>::iterator ,而是reverse_iterator; reverse_iterator rbegin();const_reverse_iterator rbegin() const;Return reverse iterator to reverse begi 阅读全文
fibonacci数的矩阵乘法
2012-04-15 02:01 by youxin, 378 阅读, 0 推荐, 收藏,
摘要:斐波那契数列1,1,2,3,5,8,13…….有如下定义F(n)=f(n-1)+f(n-2)F(1)=1F(2)=1对于以下矩阵乘法[F(n+1)]=[1 0][F(n) ][ F(n)]=[1 0][F(n-1)] 结果 F(n+1)=F(n)+F(n-1) F(n)=F(n) 设[ 1 1] [ 1 0]为Af(n)=Af(n-1)=A^n-1f(1)f(1)可以表示成[ 1 1] [ 1 0] 所以F[n] = A^n 还有另外一种形式:( F(n+1) F(n) F(n) F(n-1) ) = A^n;先看自己写的简单易懂的代码:#i... 阅读全文
浙公网安备 33010602011771号