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

【C/C++相关】论程序员写技术博客的重要性

2015-07-04 14:21 471 查看

C++数组的动态分配空间、初始化和释放空间

参考这里

针对于一维数组

动态分配空间(new)

const int n = 10;
int *array = new int
;


初始化(memset)

#include <string.h>
memset(array, 0, n*sizeof(int));


释放空间(delete)

delete []array;


针对于二维数组

动态分配空间(new)

const int n = 10;
int **array = new int *
;
for(int i=0;i<n;i++)
array[i] = new int
;


初始化(memset)

初始化和动态分配一块进行:

#include <string.h>
const int n = 10;
int **array = new int *
;
for(int i=0;i<n;i++)
{
array[i] = new int
;
memset(array[i], 0, n*sizeof(int));
}


释放空间(delete)

有多少个new就有多少个delete。

for(int i=0;i<n;i++)
delete[] array[i];
delete []array;


C/C++读写文件

C++读写

参考文章《C++中cin.getline()、getline()、cin.get()区别》

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
ifstream ifs("in.txt");
ofstream ofs("out.txt");
string temp;
while(getline(ifs, temp))
{
cout<<temp<<endl;
}
ifs.close();
ofs.close();
return 0;
}


C读写

#include <stdio.h>

freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdin);

fclose(stdin);
fclose(stdout);


C/C++中assert断言的使用

#include <assert.h>

assert(int expression);


C++的头文件和实现文件分别写什么

C++的头文件和实现文件分别写什么

……

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