佛山网站建设公司-印象互动,公司网站开发怎么做,seo索引擎优化,企业网站建设的必要性及维护文章目录目录1.文件和流2.异常处理3.动态内存4.命名空间目录
1.文件和流 注意 文件打开方式中的in和out都是相对于内存#xff08;计算机#xff09;而言的#xff0c;计算机读取文件#xff0c;是将数据从磁盘中的文件读入到内存中#xff0c;所以用的是in #include 计算机而言的计算机读取文件是将数据从磁盘中的文件读入到内存中所以用的是in #include fstream
#include iostream
using namespace std;int main ()
{char data[100];// 以写模式打开文件ofstream outfile;outfile.open(afile.dat);cout Writing to the file endl;cout Enter your name: ; cin.getline(data, 100);// 向文件写入用户输入的数据outfile data endl;cout Enter your age: ; cin data;cin.ignore();// 再次向文件写入用户输入的数据outfile data endl;// 关闭打开的文件outfile.close();// 以读模式打开文件ifstream infile; infile.open(afile.dat); cout Reading from the file endl; infile data; // 在屏幕上写入数据cout data endl;// 再次从文件读取数据并显示它infile data; cout data endl; // 关闭打开的文件infile.close();return 0;
}当上面的代码被编译和执行时它会产生下列输入和输出 $./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9 注意 流操作符也是相对于计算机而言的计算机是可以赋值和改变的左值在左边所以从外部读取数据到计算机内存中使用的流操作符是 同理从计算机内存中输出到其他设备屏幕使用的流操作符是 ,数据的方向是从内存到外部设备所以箭头朝右边非常形象。 #include iostream
#include fstream
using namespace std;//向文件内部写入数据并将数据读出
void file_wr(void)
{char data[100];//向文件写入数据ofstream outfile;outfile.open(test.txt);cout Write to the file endl;cout Enter your name: endl;cin.getline(data, 100);outfile data endl;cout Enter your age: endl;cin data;cin.ignore();outfile data endl;outfile.close();//从文件读取数据ifstream infile;infile.open(test.txt);cout Read from the file endl;infile data;cout data endl;infile data;cout data endl;infile.close();
}//将数据从一文件复制到另一文件中
void file_copy(void)
{char data[100];ifstream infile;ofstream outfile;infile.open(test.txt);outfile.open(test_1.txt);cout copy from test.txt to test_1.txt endl;while (!infile.eof()){infile data;cout data endl;outfile data endl;}infile.close();outfile.close();
}//测试上述读写文件与文件数据复制
int main()
{file_wr();file_copy();return 0;
}当上面的代码被编译和执行时它会产生下列输入和输出 $./a.out Writing to the file Enter your name: John Enter your age: 20 Reading from the file John 20 copy from test.txt to test_1.txt John 20
2.异常处理 #include iostream
using namespace std;double division(int a, int b)
{if( b 0 ){throw Division by zero condition!;}return (a/b);
}int main ()
{int x 50;int y 0;double z 0;try {z division(x, y);cout z endl;}catch (const char* msg) {cerr msg endl;}return 0;
}由于我们抛出了一个类型为 const char* 的异常因此当捕获该异常时我们必须在 catch 块中使用 const char*。当上面的代码被编译和执行时它会产生下列结果 Division by zero condition! 3.动态内存 #include iostream
using namespace std;int main ()
{double* pvalue NULL; // 初始化为 null 的指针pvalue new double; // 为变量请求内存*pvalue 29494.99; // 在分配的地址存储值cout Value of pvalue : *pvalue endl;delete pvalue; // 释放内存return 0;
}#include iostream
using namespace std;int main()
{int **p; int i,j; //p[4][8] //开始分配4行8列的二维数据 p new int *[4];for(i0;i4;i){p[i]new int [8];}for(i0; i4; i){for(j0; j8; j){p[i][j] j*i;}} //打印数据 for(i0; i4; i){for(j0; j8; j) { if(j0) coutendl; coutp[i][j]\t; }} //开始释放申请的堆 for(i0; i4; i){delete [] p[i]; }delete [] p; return 0;
}0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 0 2 4 6 8 10 12 14 0 3 6 9 12 15 18 21 #include iostream
using namespace std;class Box
{public:Box() { cout 调用构造函数 endl; }~Box() { cout 调用析构函数 endl; }
};int main( )
{Box* myBoxArray new Box[4];delete [] myBoxArray; // 删除数组return 0;
}如果要为一个包含四个 Box 对象的数组分配内存构造函数将被调用 4 次同样地当删除这些对象时析构函数也将被调用相同的次数4次。 当上面的代码被编译和执行时它会产生下列结果
调用构造函数 调用构造函数 调用构造函数 调用构造函数 调用析构函数 调用析构函数 调用析构函数 调用析构函数
class A
{private:char *m_cBuffer;int m_nLen;public:A(){ m_cBuffer new char[m_nLen]; }~A() { delete [] m_cBuffer; }
};
A *a new A[10];// 仅释放了a指针指向的全部内存空间 但是只调用了a[0]对象的析构函数 剩下的从a[1]到a[9]这9个用户自行分配的m_cBuffer对应内存空间将不能释放 从而造成内存泄漏
delete a;// 调用使用类对象的析构函数释放用户自己分配内存空间并且 释放了a指针指向的全部内存空间
delete [] a;4.命名空间
本质上命名空间就是定义了一个范围。 #include iostream
using namespace std;// 第一个命名空间
namespace first_space{void func(){cout Inside first_space endl;}
}
// 第二个命名空间
namespace second_space{void func(){cout Inside second_space endl;}
}
int main ()
{// 调用第一个命名空间中的函数first_space::func();// 调用第二个命名空间中的函数second_space::func(); return 0;
}当上面的代码被编译和执行时它会产生下列结果 Inside first_space Inside second_space
#include iostream
using namespace std;// 第一个命名空间
namespace first_space{void func(){cout Inside first_space endl;}
}
// 第二个命名空间
namespace second_space{void func(){cout Inside second_space endl;}
}
using namespace first_space;
int main ()
{// 调用第一个命名空间中的函数func();return 0;
}当上面的代码被编译和执行时它会产生下列结果 Inside first_space
using 指令也可以用来指定命名空间中的特定项目。例如如果您只打算使用 std 命名空间中的 cout 部分您可以使用如下的语句
using std::cout;
#include iostream
using namespace std;// 第一个命名空间
namespace first_space{void func(){cout Inside first_space endl;}// 第二个命名空间namespace second_space{void func(){cout Inside second_space endl;}}
}
using namespace first_space::second_space;
int main ()
{// 调用第二个命名空间中的函数func();return 0;
}当上面的代码被编译和执行时它会产生下列结果 Inside second_space