1、#pragma once用法总结
为了避免同一个头文件被包含(include)多次,C/C++中有两种宏实现方式:一种是#ifndef方式;另一种是#pragma once方式。
#ifndef的方式依赖于宏名字不能冲突,这不光可以保证同一个文件不会被包含多次,也能保证内容完全相同的两个文件不会被不小心同时包含。
#pragma once则由编译器提供保证:同一个文件不会被包含多次。注意这里所说的“同一个文件”是指物理上的一个文件,而不是指内容相同的两个文件。
方式一由语言支持所以移植性好,方式二 可以避免名字冲突
2、c++四舍五入函数
3、“const char*”类型的值不能用于初始化“char”类型的实体
(1)在char*前加const关键字 const char *str1 = "ABC";
(2)对变量进行强转 char *str1 = (char *)"ABC";
(3)对变量先进行字符数组声明 char str[]="ABC"; char *str1=str;
(4)设置属性 -> C/C++ ->语言 -> 符合模式项->否
4、atoi函数源代码
isspace(int x) { if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r') return 1; else return 0; } isdigit(int x) { if(x<='9'&&x>='0') return 1; else return 0; } int atoi(const char *nptr) { int c; /* current char */ int total; /* current total */ int sign; /* if '-', then negative, otherwise positive */ /* skip whitespace */ while ( isspace((int)(unsigned char)*nptr) ) ++nptr; c = (int)(unsigned char)*nptr++; sign = c; /* save sign indication */ if (c == '-' || c == '+') c = (int)(unsigned char)*nptr++; /* skip sign */ total = 0; while (isdigit(c)) { total = 10 * total + (c - '0'); /* accumulate digit */ c = (int)(unsigned char)*nptr++; /* get next char */ } if (sign == '-') return -total; else return total; /* return result, negated if necessary */ }
5、使用ofstream打开文件
ofstream outfile; outfile.open("C:\\123.txt", ios::out); if (outfile.is_open()) { cout << "文件打开成功" << endl; } outfile.close();
6、寄存器变量
形同虚设,程序中不需要用。
7、枚举的使用
// EnumParam.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <iostream.h> enum RecordsetState {RS_OPEN = 3, RS_EDIT, RS_CLOSE = 6}; //定义枚举类型 void OperateRecordset(RecordsetState nState) { if (nState == RS_OPEN) { cout << "打开记录集" << endl; } else if (nState == RS_EDIT) { cout << "编辑记录集" << endl; } else if (nState == RS_CLOSE) { cout << "关闭记录集" << endl; } } int main(int argc, char* argv[]) { OperateRecordset(RS_OPEN); //调用函数OperateRecordset //OperateRecordset(3); //错误的函数调用 return 0; }
8、结构体的使用
// AssignStructure.cpp : Defines the entry point for the console application. #include "stdafx.h" #include "iostream.h" const int CHAR_LEN = 128; struct Student //定义一个结构体 { char szName[CHAR_LEN]; //姓名 int nAge; //年龄 char szSex[CHAR_LEN]; //性别 char szAddress[CHAR_LEN]; //地址 }; int main(int argc, char* argv[]) { Student Jeff; Jeff.nAge = 15; Student Annie; Annie = Jeff; cout << Annie.nAge << endl; return 0; }
9、使用指针变量访问和修改对象
// PtrAccess.cpp : Defines the entry point for the console application. #include "stdafx.h" #include "iostream.h" int main(int argc, char* argv[]) { int nLen = 10; //定义整型变量 int *pLen = &nLen; //定义指针,并进行初始化 cout << "pLen指向的数据" << *pLen << endl; //输出指针指向的数据 *pLen = 100; //设置指针指向的数据 cout << "nLen的值:" << nLen << endl; //输出变量nLen cout << "pLen指向的数据:" << *pLen << endl; //输出指针指向的数据 return 0; }
10、使用指针遍历数组
// IterateArray.cpp : Defines the entry point for the console application. #include "stdafx.h" #include "iostream.h" int main(int argc, char* argv[]) { int nArray[5] = {1, 2, 3, 4, 5}; //定义一个数组,包含5个元素 int *pIndex = nArray; //定义一个指针,指向数组 for(int i=0; i<5; i++) //使用指针遍历数组 { cout << *pIndex << endl; //读取数组元素值 pIndex++; //使指针指向下一个数组元素 } return 0; }