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;
}
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;
}
相关文章推荐
- 项目的复习和回忆记录
- Cpp环境【POJ3320】Jessica's Reading Problem 洁西卡的复习计划
- 终于……我的qt可以build了,内牛满面,记录qlocale.cpp:6528error心路历程
- 《算法设计与分析》考试说明(复习总结 与 试题提交方式、个人考勤记录——2014.05.10更新)
- 开始单片机的C51重新复习了!(单片机复习全记录)
- 关于CPP内存泄露要记录的步骤
- 2017.7.17 学习记录 Server复习
- Java基础复习之StringBuffer和StringBuilder简要记录
- ios学习记录 day24 UI 3 复习
- 【复习记录】树上倍增
- 【记录】仅仅是复习下这个效果
- C++复习记录2 二义性 malloc free 以及 new delete
- include .h 以及.cpp的记录
- C语言易忘知识记录复习
- CPP复习笔记 1
- C语言基础数据类型与运算符号复习记录
- usaco 以后复习题目记录
- (常复习)poj 1015 dp+记录dp路径+转变最优子结构+区间映射
- CPP复习笔记 3
- ######03.经典实体Bean设计:购物项-购物车;订单项-订单。---记录。便于复习。借鉴设计思想