大家可以下载后用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) 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; }
时间: 2024-10-29 10:09:19