Essential C++ 笔记

一. C++编程基础

1.数组

数组初始化

const int a=10;
int b[a];

vector 初始化

int a[8] = {1,2,3,4,5,6,7,8};
vector<int> v(a, a+8);

2.指针

指针初始化为,防止对null指针进行提领操作

int *p =0;
if(p && *p==1024){}

判断是否为空指针

if(!p){}

指针数组

vector<int> a, b, c;
vector<int> *p[] = {&a, &b, &c};

伪随机数

#include <cstdlib>
srand(123);
int n = rand()%5;

3.文件操作

写文件

//定义文件输出对象
#include <fstream>
ofstream outfile("test.txt"); //覆盖方式
ofstream outfile("test.txt", ios_base::app); //追加方式
//判断文件打开是否成功
if(!outfile)
	cerr<<"文件开启失败"<<endl;
else
	outfile<<"test01"<<endl;

读文件

#include <fstream>
//定义文件读取对象
ifstream infile("test.txt");
//判断文件是否打开成功
if(!infile)
	cerr<<"文件开启失败"<<endl;
else{
	string s;
	while(infile>>s){ cout<<s<<endl; }
}

读写文件

#include <fstream>
//定义文件读写对象
fstream iofile("test.txt", ios_base::in|ios_base::app);
//判断文件开始是否成功
if(!iofile){
	cerr<<"文件开启失败"<<endl;
}else{
	iofile.seekg(0);//将光标重定位到文件开始
}

二. 面向过程的编程风格

函数

值传递、引用传递

void test(int a){} //值传递: 1.不修改传入对象 2.效率低
void test(int &a){} //引用传递:1.直接修改传入的对象 2.效率高
void test(const int &a){} //引用传递 1.不修改传入对象 2.效率高
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值