c++ primer plus(二)std

来源:互联网 发布:初学java看什么书好 编辑:程序博客网 时间:2024/05/01 19:28

   

1. using namespace std;

名字空间既是引用哪个名字空间的函数

 

2. 输入 cin 

cin得到int变量:

int nTemp;

cin>> nTemp;

 

cin获取字符串:

char name[20]

cin >> name;

cin 获取字符串有问题:

a. 只能得到一个单词,若输入lei fist则 得到lei,

b. 会跳过空格。若输人"   ddd",则得到"ddd".  

c.  应该使用cin.getline:   cin.getline(name,20);

 

3. cout输出

a. 输出字符串

char flower[10] = "rose";

cout << flower<<"s arered\n";

flower是个指向一个字符的地址,cout会从该字符开始打印,直到遇到空字符为止。


b. 输出整数

通过dec hex oct改变输出整数的类型。

int chest = 42;

int waist = 0x42;

int inseam = 042; //8进制

int main()

{

    usingnamespacestd;

    intchest = 42;

    intwaist = 42;

    intinseam = 42;

 

    cout<< dec << "Monsieur cuts a striking figure!"<< endl;

    cout<< "chest = "<< chest <<" (decimal for 42)"<< endl;

    cout<< hex;        //manipulator for changing number base;

    cout<< "waist = "<< waist <<" (hexadecimal for 42)"<< endl;

    cout<< oct;        //manipulator for changing number base;

    cout<< "inseam = "<< inseam <<" (octal for 42)"<< endl;

    return0;

}

结果:

Monsieur cuts a striking figure!

chest = 42 (decimal for 42)

waist = 2a (hexadecimal for 42)

inseam = 52 (octal for 42)

 

c.  这三个换行是一样的:

cout <<  endl;

cout<<  '\n'

cout << "\n"

0 0
原创粉丝点击