2012-12-1

学习笔记

 

1.类的定义                    

#include <iostream>

classPeople

{

    friend std::ostream & operator<<(std::ostream&,const People &);

public:

    People():age(0),height(0),weight(0){}

    People(int Age,doubleHeight,doubleWeight):age(Age),height(Height),weight(Weight){}

    People(const People &obj):age(obj.age),height(obj.height),weight(obj.weight){}

public:

    void display(){std::cout<<"年龄:"<<age<<身高:"<<height<<" 体重:"<<weight<<std::endl;}

    void showNum(){std::cout<<"人数:"<<num<<std::endl;}

private:

    int age;

    double height;

    double weight;

public:

    static int num;

      

};

intPeople::num;

std::ostream & operator<<(std::ostream& os, People &obj)

{

    obj.display();

    return os;

}

classStudent:public People

{

public:

    Student(int Age,doubleHeight,double Weight,constchar * ptr,intScore):People(Age,Height,Weight),score(Score)

    {::strcpy(name,ptr);}

public:

    void display(){std::cout<<"姓名:"<<name<<" 年级:"<<score<<std::endl;display();}

private:

    char name[18];

    int score;

};

intmain(int argc, char* argv[])

{

    Peoplearr[4];

    People::num=4;

    for(int i=1;i!=5;++i)

    {

       int a;

       double b,c;

       std::cout<<"请输入第"<<i<<"个人的信息,依次输入年龄身高体重:\n";

       std::cin>>a>>b>>c;

       arr[i-1]=People(a,b,c);

    }

    for(int i=0;i!=4;++i)

    {

       std::cout<<arr[i];

       arr[i].showNum();   

    }

    system("pause");

    return 0;

}

 

2.笔记本i5处理器 每秒计算次数

#include <iostream>

#include <ctime>

intmain(int argc,char* argv[])

{

    ::clock_tnow=::clock();

    long double i=-99;

    while(::clock()-now<(CLOCKS_PER_SEC))

    {++i;}

    std::cout<<i;

    ::system("pause");

    return 0;

}

大约为100万次。

 

 

 


3、c++中数组的赋值。

#include <iostream>

#include <ctime>

#include <algorithm>

voiddisplay(char a){std::cout<<a;}

intmain(int argc,char* argv[])

{

    char * ptr="asdf";//字符串字面值是一个常量数组,但返回的是一个char指针,非const char 指针

    //std::cout<<ptr;

    //int arr[2][2][2]={{{1,}},{2},4}; //错误

    //int arr[2][2][2]={{{1,}},2,{4}}; //正确

    //int arr[2][2][2]={{{1,}},{{2}},4}//错误

    //int arr[2][2][2]={{{1,}},{{2}}}; //正确

    //int arr[3][3][3]={{{1},5,{6}},2,3};

    //std::for_each(&arr[0][0][0],&arr[0][0][0]+27,display);

 

    char arr1[3][3][3]={{"bb"},{"c"},{"ee"}};

    std::for_each(&arr1[0][0][0],&arr1[0][0][0]+27,display);

    ::system("pause");

    return 0;

}

C++中没有定义多维度数组,而是通过数组的数组来实现多维数组的目的。多维数组可以像普通数组一样直接赋值。

多维数组在赋值的时候有三种形式,example1:

Type array[size][size]={a, b , c,…} 可以不等于数组的大小,为赋值的自动初始化为零。

Example2: type array[size1][size2]={{ a, b, c…},{e ,f , g…}…}

Example3: type array[size1][size2]={{ a , b ,c…},e ,f, g…}

最后一种用法,在第一个未加{}的元素开始,后面全都是最基本元素。

当数组维数更多的时候,采用递归的原则,跟上面的赋值方法相同。

有一个特殊的用法,就是底层是char数组的时候,可以用c风格字符串来赋值,这个时候赋值的不再以基本元素为单位,而是以底层的基本char数组为赋值单位,例子:

Char array[size1][size2][size3]={“about”,“back ”,” cock ” ….}

这时候以char  [size2][size3]为单位赋值。

4、

#include <iostream>

#include <ctime>

#include <algorithm>

voiddisplay(char a){std::cout<<a;}

intmain(int argc,char* argv[])

{

   /*const char arr[]={97,98,99,100};

    char * ptr=const_cast<char*>( arr);

    *ptr=110;//成功

    std::cout<<arr[0];*/

   

    //char * ptr="zxl";

    //*ptr='A';  //报错

    //std::cout<<ptr;

 

    //char * ptr=const_cast<char*>("zxl");

    //*ptr='A';//报错

    //std::cout<<ptr;

    ::system("pause");

    return 0;

}

上面例子说明,字符串字面值(c风格字符串)在保存的时候,是内存写保护的。

C++的实现,会自动把c风格字符串转换为const char 指针,如果左值是char指针,实现会调用const_static<char *>来自动转换,但是转换后仍不能改变c风格字符换的值。

当然在其他的情况下,通过const_static<>转换后,是可以改变原始是const的对象的值的。

 

5.

#include <iostream>

#include <ctime>

#include <algorithm>

intb(0),cnt(0);

voiddisplay(char a)

{

    ++cnt;

    std::cout<<a;

    if(cnt%b==0){std::cout<<std::endl;}

}

intmain(int argc,char* argv[])

{

lable: 

    cnt=0;

    int size(0);

    std::cin>>size;

    b=2*size-1;

    char * ptr=new char[size*(2*size-1)];

    std::fill(ptr,ptr+(size*(2*size-1)-1),' ');

    for(inti=size-1;i>=0;--i)

    {

       int temp=size-1-i;

       std::fill(&ptr[i*(2*size-1)+temp],&ptr[i*(2*size-1)+2*size-1-temp],'*');

    }

    std::for_each(ptr,ptr+(size*(2*size-1)-1)+1,display);

gotolable;

    ::system("pause");

    return 0;

}

上面是个连续输出圣诞树图形的小程序,需要首先输入树的高度。

有两个知识点,一个是在动态创建数组的时候,最好不创建多维数组,因为动态创建多维数组的时候通常每个维的大小是不确定的,如果是多维的则返回的指针类型将不能再编译时候确定,编译器就会很迷惑。

另外,我发现函数内部的静态对象很不好用,只能在函数内部初始化或者改变,在外部改变不了,还不如用全局变量呢,当然如果能完成任务的前提下,它有它封装的好处。

注意标准库算法,最后一个迭代器指向的对象是不做操作的。

 

5、

#include <iostream>

#include <ctime>

#include <string>

#include <algorithm>

intb(0),cnt(0);

voiddisplay(char a)

{

    ++cnt;

    std::cout<<a;

    if(cnt%b==0){std::cout<<std::endl;}

}

intmain(int argc,char* argv[])

{

    char arr[]="i amconfused about \ ";

    std::stringstr="testing with string type";

    std::cout<<sizeof(arr);

    ::system("pause");

    return 0;

}

特殊字符(转义字符)

编程

ADCII码编号

换行

\n

10

水平制表

\t

9

垂直制表

\v

11

退格

\b

8

回车

\r

13

响铃

\a

7

斜杠

\\

92

问号

\?

63

单引

\’

39

双引

\”

34

字符分为三类,第一类是用转义符’\’和不用转义符’\’没区别的 比如 \h 与h,他们都是表示一个字符h,第二类是用转义符跟不用有很大区别的,如上面列出的\n \t \v \b \r \a 与 n t v b a ,是完全不同的字符,第三类是必须用转义符表示的字符,他们包括\跟”,\在c++里面是链接字符,”在c++里面是字符串结束标志,这俩要用在字符串里面必须转义。(有点疑问就是,c++里面’跟?转义不转义感觉没区别,不知道为什么上表加了它们俩)

另外所有的字符都可以用ASCII码的编号来表示 如 \97(dec) \001(oct) \x024(hex)

通过十进制跟八进制表示字符的时候,编译器只取三位,如果取的三位数转义后无字符对应,则取消转义。例子:

Sizeof(“abc\1234”)=6 字节,因为\123有意义,表示一个字符。

Sizeof(“abc\4321”)=8字节,因为\432转义无意义,所以\432等效于432。

对于八进制,因为只读三位,以0开始,所以根本不会超过字符的最大值,例:

Sizeof(“abc\0993”)=6 字节  。

对于十六进制,不限制\x后面的位数,也就是除非重新遇到0-f之外的字符(这个原则也可以截断上面十进制跟八进制的转义过程),它一直计算在内,不会自己截断,如果转义的数过大,编译器就会报错。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值