面向对象程序设计-C++ Steam & Vector 【第三次上课笔记】
大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看
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++ Steam & Vector 【第三次上课笔记】的更多相关文章
- {key}面向对象程序设计-C++ polymorphism 【第十三次上课笔记】
Peronal Link: http://segmentfault.com/a/1190000002464822 这节课讲了本门课程 面向对象程序设计中最为重要的一个部分 - 多态 /******** ...
- [.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类
[.net 面向对象程序设计进阶] (11) 序列化(Serialization)(三) 通过接口 IXmlSerializable 实现XML序列化 及 通用XML类 本节导读:本节主要介绍通过序列 ...
- [.net 面向对象程序设计进阶] (7) Lamda表达式(三) 表达式树高级应用
[.net 面向对象程序设计进阶] (7) Lamda表达式(三) 表达式树高级应用 本节导读:讨论了表达式树的定义和解析之后,我们知道了表达式树就是并非可执行代码,而是将表达式对象化后的数据结构.是 ...
- 对于“2017面向对象程序设计(Java)第三周学习总结”存在问题的反馈
对于“2017面向对象程序设计(Java)第三周学习总结”存在问题的反馈 一:教学中存在的学习问题 “1.由于同学们平时练习不足,上课总是出现跟不上老师的节奏的现象. 2.个别同学上课不认真听讲,打开 ...
- «面向对象程序设计(java)»第三周学习总结 周强 201771010141
实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3)掌握Java语言构造基本程序语法知识(ch1-ch3): (4)利用已 ...
- c++面向对象程序设计 谭浩强 第三章答案
2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,in ...
- [.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(三) 利用多线程提高程序性能(下)
[.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(二) 利用多线程提高程序性能(下) 本节导读: 上节说了线程同步中使用线程锁和线程通知的方式来处理资源共享问题,这 ...
- 2017面向对象程序设计(Java)第三周学习总结
白驹过隙,日月如梭,一转眼,我们已经度过了第三周的学习时光,随着时间的一天天流逝,我么对知识的积累也逐渐增多.当然,我们还有许许多多需要改进的地方.下面,我将对第三周的助教工作进行总结,望老师及同学们 ...
- 达拉草201771010105《面向对象程序设计(java)》第三周学习总结
达拉草201771010105«面向对象程序设计(java)»第三周学习总结 第一部分:实验部分 1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟 ...
随机推荐
- cdoj 1134 男神的约会 状压dp
题目链接 给一个10*10的方格, 每个格子里面有0-9,走到一个格子, 就要在这个格子待一段时间, 时间长度为这个格子的数字. 从左上角走到右下角, 要求0-9必须每种格子都要走到, 输出最短时间. ...
- python自学笔记(十一)关于函数及书写格式
1.函数是抽象的第一步 1.1 有关高压锅 1.2 函数是抽象出来的结构,是总结,是方法 1.3 多用函数 2.如何定义函数 2.1 def是关键词, ...
- MySQL DBA成长之路
http://blog.51cto.com/zt/579 :GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'm ...
- Android studio如何使用SVN进行版本控制?
通过这两天对Android Studio的研究,终于搞通了Android Studio的基本操作及与SVN的相关关联操作(这样才能在公司的开发工作中使用):Google年底将会停止ADT插件的更新和支 ...
- Ants(思维)
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12893 Accepted: 5637 Description ...
- 用javap命令反编译来分析字符串问题
编写Test.java.编译完后生成Test.class文件,然后对该文件运行javap -c Test命令,生成字节码指令.阅读并得出结论 一.s1和s2指向常量池的不同常量 ①java代码 pub ...
- HDU4712+随机算法
随机算法 求n个20位的2进制串的MinDist. Dist:两个串的异或结果中1的个数 /* 随机算法 */ #include<algorithm> #include<iostre ...
- HttpWebRequest和WebClient的区别
HttpWebRequest和WebClient的区别(From Linzheng): 1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Creat ...
- Backbone入门教程
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Android学习之Drawable(一)
Drawable有很多种,它们表示一种图像概念,但它们不全是图片.Drawable是什么呢?下面是Google Android API中的定义: A Drawable is a general abs ...