物件導向程式設計-C++ Steam & Vector 【第三次上課筆記】

來源:互聯網
上載者:User

標籤:

 

大家可以下載後用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 recommendifstream fin ("fio.cpp");    //fout.close()    //Not necessary in C++, because C++ do it automatically//Copy the whole fileint 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 numberint 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 Exampleint main(){    double* pd = new double;    cin >> *pd;    cout << *pd;    delete pd;    return 0;}//Introduce Vectorint 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 stringint 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 fileint 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;}

 

物件導向程式設計-C++ Steam & Vector 【第三次上課筆記】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.