面向对象程序设计-C++ Steam & Vector 【第三次上课笔记】
2015-03-16 11:51
726 查看
大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看
Conference: http://blog.csdn.net/candy1232009/article/details/7032526
Conference: http://blog.csdn.net/candy1232009/article/details/7032526
//ofstream fout; //C style //fout.open("fout.txt"); ofstream fout ("fout.txt"); //C++ out stream recommend ifstream fin ("fio.cpp"); //fout.close() //Not necessary in C++, because C++ do it automatically //Copy the whole file int main(){ ofstream fout ("SourceCodeCopy.cpp"); ifstream fin ("SourceCode.cpp"); string str; while(getline(fin, str)){ //Discards newline char fout << str << endl; //... must add it back } return 0; } //calculate the average number int main(){ int i, num, cur; cin >> num; //double* array = (double*) malloc(num * sizeof(double)); //C style double* array = new double[num]; //C++ style double ave = 0.0; for(i = 0; i < num; ++i){ cin >> array[i]; ave += array[i]; } cout << "Ave is the " << ave / num << endl; //free(array); //C style delete[] array; //[] means delete all the Array, if "delete array" means delete only the array[0] return 0; } //one double number array Example int main(){ double* pd = new double; cin >> *pd; cout << *pd; delete pd; return 0; } //Introduce Vector int main(){ vector <double> vc; //init a vector vc.push_back(27.8); //insert element to its tail vc.push_back(54.2); //vc[2] = 89.3 //Don't do in this way, no such spacez for(i = 0; i < vc.size(); ++i){ cout << vc[i] << endl; } return 0; } //Answer is 0 0 89.3 27.8 54.2 (5 elements) int main(){ int i; vector <double> vc(3); //init a space long for 3 vc.push_back(27.8); vc.push_back(54.2); vc[2] = 89.3;// for(i = 0; i < vc.size(); ++i){ cout << vc[i] << endl; } return 0; } //Copy an entire file into a vector of string int main(){ vector <string> v; ofstream out ("SourceCodeCopy.cpp"); ifstream in ("SourceCode.cpp"); string line; while(getline(in, line)){ v.push_back(line); } for(int i = 0; i < v.size(); ++i){ out << 1 + i << ": " << v[i] << endl; } return 0; } //Class work //give a number N, and make n random numbers into a file int main(){ srand((int)time(NULL)); int i, n; vector <int> v; ofstream out ("rand_num.txt"); cin >> n; while(n--){ v.push_back(rand() % 65536); } for(i = 0; i < v.size(); ++i){ out << v[i] << endl; } return 0; } //make n numbers in the range [0, 1) #include <iostream> #include <cstdlib> #include <ctime> #include <fstream> #include <string> #include <vector> #include <cmath> #include <algorithm> using namespace std; int main(){ srand((int)time(NULL)); int i, n; vector <double> v; ofstream out ("rand_num.txt"); cin >> n; while(n--){ v.push_back((double)rand() / (double)RAND_MAX); } for(i = 0; i < v.size(); ++i){ out << v[i] << endl; } return 0; }
相关文章推荐
- 面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】
- 面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】
- 面向对象程序设计-C++ Type conversion (Static) & Inheritance & Composition【第十二次上课笔记】
- 面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】
- 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】
- 面向对象程序设计-C++ Inheritance & Multiple inheritance & RTTI【第十三次上课笔记】
- 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】
- {key}面向对象程序设计-C++ polymorphism 【第十三次上课笔记】
- 面向对象与C++程序设计-类的继承与派生学习笔记
- C++学习笔记【第三部分第十五章:面向对象程序设计】
- 面向对象与C++程序设计-向量和迭代器学习笔记
- 笔记:C++学习之旅---面向对象程序设计2
- 设计模式C++学习笔记之二十(完结篇 & 面向对象原则)设计模式C++实例下载
- C++ 学习笔记(15)面向对象程序设计(类、继承、虚函数、抽象类、using命令、容器与继承)
- C++学习笔记 面向对象程序设计
- php学习笔记 PHP面向对象的程序设计
- 浪潮优派培训java笔记:第6章 面向对象程序设计
- php学习笔记 PHP面向对象的程序设计
- Javascript高级程序设计第二版第六章--面向对象程序设计(Object-Oriented Programming)简称OOP编程--笔记
- java笔记:第6章 面向对象程序设计