您的位置:首页 > 编程语言 > C语言/C++

CPP 复习 记录

2011-12-09 00:02 134 查看
1. bool

bool temp = 0;

cout << temp ;//temp = 0;

`````````````````````

bool temp = -1;

cout << temp; // output is 1; (if temp != 0, temp temp =1)

2. static_cast<TYPE>(value)

static_cast<char>(98) ;// is 'b';

3. VC++ (MicroSoft VC)

NEW PROJECT -> Win32 -> Win32 Console Application,; Empty Project

4. 输出格式控制

#include <iomanip>

cout << setw(8) << "C++" << setw(6) <<101 <<endl; //setw(width) , to control the width of output

OUTPUT:

C++ 101// 5 个空格, C++, 3个空格, 101

setprecision(n); 设置一个浮点数的精度,返回新精度的浮点数。

cout << setprecision(3)<<12.34567<<endl; //output: 12.3

//与setw不同,setprecision会保持到下一个setprecision之前。

cout << left; // or , cout << right 实现左对齐或者是右对齐。

cout << setw(8) << 1.23 << endl;

5. 枚举类型使用

enum Day {MONDAY, TUESDAY, WEDNESDAY};//默认时,各个值:MONDAY = 0, TUESDAY = 1, WEDNESDAY = 3

Day day;

day = MONDAY;

enum Day {MONDAY =10, TUESDAY = 30, WEDNESDAY = 50};//指定各值。

6. 数组作为函数参数

一维数组:void p(int list[], int size){...}

二维数组:void p(int list[][column_size], int size){...}

//可以向函数传递数组,但是函数不可以返回数组。

7.

count = 9;

int &refcount = count;

refcount 与 count是指向同一数。relcount为别名。

8. 一个指针可以用来引用一个数组,一个字符串,一个整数或其他变量。

声明:int * pCount;

pCount = &count;// &count returns to the address

or : int *pCount = &count;

and: *pCount 可以返回变量值

9. 利用指针的按引用方式 实现 向函数传递参数

void swap( int *pValue1, int *pValue2)

{

int temp =* pValue1;

*pValue1 = *pValue2;

*pValue2 = temp;

}

swap(&num1, &num2);

10. 数组和指针

int list[6] = {11, 12, 13, 14}

*list, *(list + 1), *(list + 2), *(list + 3) 则可以分别得到对应的数组成员的值

11. 常量指针 VS 数据常量

double * const pValue = &radius; //

const double * pValue = &radius; //

12. 通过指针访问对象成员

Circle circle1;

Circle *pCircle = & circle1;

cout<< (*pCircle).radius<<endl;

cout<< pCircle->radius<<endl;// -> 是针对于指针的特别方式。

// this->radius, (*this).radius,

13. 在函数中,可以使用new,开辟新内存,在函数返回时,其仍可以继续使用。

使用delete可以显式销毁

14. circle.h & circle.cpp // circle.h 用作声明,基本的。而circle.cpp则是来实现其方法等

15. 避免重复声明,

如:可能会多次地include 某个头文件,于是:

可以在该头文件中,加入(Date1.h)

#ifndef DATE_H

#define DATE_H

...//原Date1.h中的内容

#endif

......................................

16. Circle *pCircle = new Circle();

delete pCircle;

//此时,会调用到析构函数,~Circle();

17.IO 操作

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

ofstream output;

output.open("E:\\test\\out.txt"); //你懂的。

output<<"hehe"<<endl;

output.close();

system("pause");

return 0;

}

ifstream input;

input >> temp1 >> temp2 ;

input.fail() ; // if fail to open the file, then return true

input.eof() ;// to test whether it comes to the end of file

getline( char array[], int size, char delimitChar );

EG.

char state[40];

input.getline(state, 40, '#'); // 会从文件中读取字符,直至遇到字符#,或者达到文件末尾,或者已经读入了39个字符(第40字符为'\0');

*********************************************

fstream 和文件打开模式

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

fstream inout;

inout.open("E:\\test\\out.txt",ios::out);

inout<<"1"<<" "<<"2"<<" "<<"3";

inout.close();

inout.open("E:\\test\\out.txt",ios::out|ios::app);

inout<<" "<<"4"<<" "<<"5";

inout.close();

inout.open("E:\\test\\out.txt",ios::in);

char temp[5];

while(!inout.eof())

{

inout>>temp;

cout<<temp<<endl;

}

inout.close();

system("pause");

return 0;

}

*********************************************

文件模式:ios::in,输入,ios::out,输出,ios::app, 追加,ios::binary, 打开文件用于二进制输入输出

*********************************************

随即文件访问

seekp & seekg (seek put & seek get)

seekg(100L, ios::beg) ;//从文件开始处第100个字节

seekg(-100L, ios::end); //从文件末尾向后100个字节

seekp(42L, ios::cur); //当前位置向前移动42个字节

seekp(42L, ios::cur); //当前位置向后移动42个字节

seekp(100L); //移到文件第100个字节处

18 vector & iterator

vector<int> intVector;

intVector.push_back(10);

intVector.push_back(20);

intVector.push_back(30);

vector<int>::iterator p1;

for(p1=intVector.begin(); p1!=intVector.end(); p1++) //intVector.end(); return to the address of next element after the last elemnt;

{

cout << *p1<<" "; //traverse the vector, then output;

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