(´>∀)人(´・ω・)ノヽ(・ε・*)人(-д-`)

Ruby @Sprout 2022


(´>∀)人(´・ω・)ノ日主ヽ(・ε・*)人(-д-`)

Ruby @Sprout 2022


(´>∀)人(´・ω・)ノ今日主題ヽ(・ε・*)人(-д-`)

Ruby @Sprout 2022


人(´・ω・)ノ 今日主題 ヽ(・ε・*)人

Ruby @Sprout 2022


ω・)ノ 今日主題 ヽ(・ε

Ruby @Sprout 2022


)ノ 今日主題 ヽ(

Ruby @Sprout 2022


今日主題

Ruby @Sprout 2022


C++ String & FileIO

Ruby @Sprout 2022


Outline


C-string Review

std::string

Applications of std::string

std::stringstream

FileIO

Applications of FileIO


C-string Review

std::string

Applications of std::string

std::stringstream

FileIO

Applications of FileIO


C-string 的本質


C-string 本質上是 char 陣列

C-string Review 1


C-string 的操基本操作


利用 <cstring> (<string.h>) 中的函式

C-string Review 2


字面常數字串存在哪裡?


是 Stack 嗎?還是 Heap 呢?答案是:都不是!


C-string Review 3-1

C-string Review 3-2


多數 Bug 的根源未定義行為


因為 C-string 本質是陣列,所以如同其他型態的陣列一樣,超出範圍不一定會報錯,是未定義行為!

C-string Review 4


C-string Review

std::string

Applications of std::string

std::stringstream

FileIO

Applications of FileIO


A peek into std::string


C++ String 1
C++ String 2


各種「附帶功能」(Methods)


C++ String 3


各種「附帶功能」(Methods)


更多、更詳細的說明文件 (Documentation) 請參考

C++ reference


Any questions?


C-string Review

std::string

Applications of std::string

std::stringstream

FileIO

Applications of FileIO


課堂練習 1


Sprout OJ No. 441


Solution


哈哈不給你看,上課講解!


Any questions?


C-string Review

std::string

Applications of std::string

std::stringstream

FileIO

Applications of FileIO


What is "stream"


Stream ,即「流」。

  • 輸入流:字節從設備流向內存
  • 輸出流:字節從內存流向設備

Reference


ostream


  • cout
  • cerr
  • clog

istream


  • cin


Sample code


w/o stringstream
w/ stringstram


課堂練習 2


Sprout OJ No. 442


C-string Review

std::string

Applications of std::string

std::stringstream

FileIO

Applications of FileIO


Create (Overwrite) a file


#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream outFile;
    outFile.open("out.txt");
    outFile << "Some text";
    outFile.close();
    return 0;
}

Load an existing file


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream inFile;
    inFile.open("in.txt");
    string s; 
    while (inFile >> s) cout << s << endl;
    inFile.close();
    return 0;
}

Load and overwrite an existing file


#include <bits/stdc++.h>
using namespace std;
const string FILENAME = "last_login_time.txt";

int main() {
    ifstream inFile; ofstream outFile;
    inFile.open(FILENAME);
    time_t curr = time(nullptr), last; inFile >> last;
    inFile.close();
    cout << "It has been " << curr-last << " seconds since your last login." << endl;
    outFile.open(FILENAME);
    outFile << curr;
    outFile.close();
    return 0;
}

C-string Review

std::string

Applications of std::string

std::stringstream

FileIO

Applications of FileIO


Loading testcases


#include <bits/stdc++.h>
using namespace std;

int main() {
    ifstream inFile("testcase.txt");
    int n, _max = -1;
    while (inFile >> n)
        _max = max(n, _max);
    cout << "The max numbers out of all numbers in the testcase: " << _max << endl;
    inFile.close();
    return 0;
}

Quick Review


  • C-string vs. C++ string
  • stream
  • stringstream
  • FileIO

感謝參與!

Select a repo