diff --git a/c++ note/chp10/chapter 10.cpp b/c++ note/chp10/chapter 10.cpp new file mode 100644 index 0000000..8b627b8 --- /dev/null +++ b/c++ note/chp10/chapter 10.cpp @@ -0,0 +1,144 @@ +//Exercise 10.2 +#include +#include +#include +#include +#define NUM 10 +using namespace std; +int main(){ + list strList; + string tmp; + for (int ix = 0; ix != NUM; ix++) { + cin >> tmp; + strList.push_back(tmp); + } + string val; + cin >> val; + cout << count(strList.begin(), strList.end(), val) << endl; + system("pause"); + return 0; +} + +//Exercise 10.3 +#include +#include +#include +using namespace std; +int main(){ + vector intVec{ 5,4,3,1,2 }; + cout << accumulate(intVec.cbegin(), intVec.cend(), 0) << endl; + system("pause"); + return 0; +} + +//Exercise 10.6 +#include +#include +#include +#include +#define NUM 10 +using namespace std; +int main(){ + vector intVec; + fill_n(back_inserter(intVec), 10, 0); + for (auto It = intVec.cbegin(); It != intVec.cend(); It++) + cout << *It << ' '; + cout << endl; + system("pause"); + return 0; +} + +//Exercise 10.9 +#include +#include +#include +#include +using namespace std; +void print(vector strVec); +int main(){ + vector strVec; + string word; + while (cin >> word) + strVec.push_back(word); + print(strVec); + sort(strVec.begin(), strVec.end()); + print(strVec); + auto unique_end = unique(strVec.begin(), strVec.end()); + print(strVec); + strVec.erase(unique_end, strVec.end()); + print(strVec); + + system("pause"); + return 0; +} + +void print(vector strVec){ + if(!strVec.empty()){ + for (auto It = strVec.cbegin(); It != strVec.cend(); It++) + cout << *It << ' '; + } + cout << endl; +} + +//Exercise 10.13 +#include +#include +#include +#include +using namespace std; +void print(vector strVec); +bool have_five_char(string word){ + return word.size() >= 5; +} +int main(){ + vector words; + string word; + while (cin >> word) + words.push_back(word); + auto end = partition(words.begin(), words.end(), have_five_char); + print(words); + words.erase(end, words.end()); + print(words); + system("pause"); + return 0; +} +void print(vector strVec) { + if (!strVec.empty()) { + for (auto It = strVec.cbegin(); It != strVec.cend(); It++) + cout << *It << ' '; + } + cout << endl; +} + +//Exercise 10.14 +auto func = [](int a, int b){return a+b;}; + +//Exercise 10.15 +int a = 42; +auto func = [a](int b){return a+b;}; + +//Exercise 10.20 +#include +#include +#include +#include +#define LENGTH 6 +using namespace std; +int main(){ + vector strVec; + string word; + while (cin >> word) + strVec.push_back(word); + auto count = count_if(strVec.cbegin(), strVec.cend(), [](string str) {return str.size() > LENGTH; }); + cout << count << endl; + system("pause"); + return 0; +} + +//Exercise 10.21 +auto func = [arg]() mutable ->bool{ + if(arg == 0) return True; + //else + while(arg-- != 0); + return False; +} \ No newline at end of file diff --git a/c++ note/chp7/exercise7_1.cpp b/c++ note/chp7/exercise7_1.cpp new file mode 100644 index 0000000..e69de29 diff --git a/c++ note/chp9/chapter 9.cpp b/c++ note/chp9/chapter 9.cpp new file mode 100644 index 0000000..533537b --- /dev/null +++ b/c++ note/chp9/chapter 9.cpp @@ -0,0 +1,500 @@ +#include +#include + +//Exercise 9.2 +std::list > List; + +//Exercise 9.4 +using std::vector; +bool find(vector::iterator front, vector::iterator behind, int target){ + for(;front::iterator find(vector::iterator front, vector::iterator behind, int target){ + for(;front while(iter1 != iter2)/* ... */ +_list DO NOT support `<` operator_ + +//Exercise 9.18 +#include +#include +#include +using namespace std; +int main(){ + deque strdq; + string currStr; + while(cin >> currStr){ + strdq.push_back(currStr); + } + deque::iterator dqIt; + for(dqIt = strdq.begin(); dqIt != strdq.end(); dqIt++){ + cout << *dqIt; + } + return 0; +} + +//Exercise 9.19 +#include +#include +#include +using namespace std; +int main() { + list strdq; + string currStr; + while (cin >> currStr) { + strdq.push_back(currStr); + } + list::const_iterator dqIt; + for (dqIt = strdq.cbegin(); dqIt != strdq.cend(); dqIt++) { + cout << *dqIt << endl; + } + + system("pause"); + return 0; +} + +//Exercise 9.20 +#include +#include +#include +#include +using namespace std; +int main(){ + list iList = {8,9,5,3,1,2,6,4,7,0}; + deque evendq, odddq; + for(auto listIt = iList.cbegin(); listIt != iList.cend(); listIt++){ + //if odd + if(*listIt % 2) odddq.push_back(*listIt); + else evendq.push_back(*listIt); + } + + for(auto dqIt = odddq.cbegin(); dqIt != odddq.cend(); cout << *dqIt << ' ', dqIt++); + cout << endl; + for(auto dqIt = evendq.cbegin(); dqIt != evnedq.cend(); cout << *dqIt << ' ', dqIt++); + cout << endl; + + return 0; +} + +//Exercise 9.24 +#include +#include +#include +using namespace std; +int main(){ + vector emptyVector; + try { + //cout << *emptyVector.begin() << endl; + //cout << emptyVector.front() << endl; + //cout << emptyVector[0] << endl; + cout << emptyVector.at(0) << endl; + }catch(out_of_range err){ + cout << err.what() << endl; + cout << "Press to exit" << endl; + system("pause"); + } + return 0; +} + +//Exercise 9.26 +#include +#include +#include +using namespace std; +int main(){ + int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; + vector ivec; + list ilst; + for(int i = 0; i != 11; i++){ + ivec.push_back(ia[i]); + ilst.push_back(ia[i]); + } + for(auto vIt = ivec.begin(); vIt != ivec.end();){ + if (*vIt % 2 == 0) vIt = ivec.erase(vIt); //even + else vIt += 1; + } + for(auto lIt = ilst.begin(); lIt != ilst.end();){ + if (*lIt % 2) lIt = ilst.erase(lIt); // odd + else lIt++; + } + cout << "result vector:" << endl; + for (auto It = ivec.begin(); It != ivec.end(); It++) + cout << *It << ' '; + cout << "\nresult list" << endl; + for (auto It = ilst.begin(); It != ilst.end(); It++) + cout << *It << ' '; + cout << endl; + system("pause"); + return 0; +} + +//Exercise 9.27 +#include +#include +#include +using namespace std; +int main(){ + forward_list ilst = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; + auto lIt = ilst.begin(); + auto prevlIt = ilst.before_begin(); + for(; lIt != ilst.end();){ + if (*lIt % 2) lIt = ilst.erase_after(prevlIt); // odd + else{ + lIt++, prevlIt++; + } + } + cout << "\nresult list" << endl; + for (auto It = ilst.begin(); It != ilst.end(); It++) + cout << *It << ' '; + cout << endl; + system("pause"); + return 0; +} + + +//Exercise 9.28 +#include +#include +#include +using namespace std; +forward_list insertStr(forward_list src, string pos, string destStr); +int main(){ + forward_list strLst = { "This", "is", "not", "TRUE", "happiness", "!" }; + auto resultLst = insertStr(strLst, string("is"), string("true")); + for (auto It = resultLst.cbegin(); It != resultLst.cend(); It++) + cout << *It << ' '; + cout << endl; + system("pause"); + return 0; +} + +forward_list insertStr(forward_list src, string pos, string destStr){ + auto It = src.begin(); + auto prev = src.before_begin(); + while(It != src.end()){ + if (*It == pos) { + src.insert_after(It, destStr); + break; + } + ++prev; + ++It; + } + if (It == src.end()) src.insert_after(prev, destStr); + return src; +} + +//Exercise 9.43 +#include +#include +using namespace std; +string strReplace(string s, string oldVal, string newVal); +string::iterator find(string &s, string dest, string::iterator start); +int main(){ + string src("thothothohelloworld"), oldVal("hell"), newVal("world"); + src = strReplace(src, oldVal, newVal); + cout << src << endl; + system("pause"); + return 0; +} +string strReplace(string s, string oldVal, string newVal){ + auto pos = s.begin(); + while (pos != s.end()) { + pos = find(s, oldVal, pos); + if (pos == s.end()) break; + pos = s.erase(pos, oldVal.size() + pos); + pos = s.insert(pos, newVal.begin(), newVal.end()); + pos += newVal.size(); + } + return s; +} +string::iterator find(string &s, string dest, string::iterator start){ + string::iterator pos = start, local, tmp; + for(; pos <= s.end() - dest.size(); pos++){ + tmp = dest.begin(); + local = pos; + while(*tmp == *local){ + tmp++, local++; + if (tmp == dest.end()) return pos; + } + } + return s.end(); +} + + +// Exercise 9.44 +#include +#include +using namespace std; +string strReplace(string s, string oldVal, string newVal); +int main(){ + string src("thothotho"), oldVal("tho"), newVal("though"); + src = strReplace(src, oldVal, newVal); + cout << src << endl; + system("pause"); + return 0; +} +string strReplace(string s, string oldVal, string newVal){ + string::size_type pos = 0; + while (pos != s.size()) { + pos = s.find(oldVal, pos); + if (pos == string::npos) break; + s.replace(pos, oldVal.size(), newVal); + pos += newVal.size(); + } + return s; +} + +//Exercise 9.45 +#include +#include +using namespace std; +string completeName(string name, string prefix, string suffix); +int main(){ + string name, prefix, suffix; + cin >> name >> prefix >> suffix; + string fullname = completeNam(name, prefix, suffix); + cout << fullname << endl; + system("pause"); + return 0; +} +string completeName(string name, string prefix, string suffix){ + name.insert(name.begin(), prefix.begin(), prefix.end()); + name.append(suffix); + return name; +} + +//Exercise 9.46: using index and length, and insert function only +string completeName(string name, string prefix, string suffix){ + name.insert(0, prefix); + name.insert(name.size(), suffix); + return name; +} + +//Exercise 9.47 +#include +#include +using namespace std; +int main(){ + string s("ab2c3d7R4E6"); + string numbers("0123456789"); + string::size_type pos = 0; + while((pos = s.find_first_of(numbers, pos)) != s.npos){ + cout << s[pos] << ' '; + pos++; + } + cout << endl; + pos = 0; + while ((pos = s.find_first_not_of(numbers, pos)) != s.npos) { + cout << s[pos] << ' '; + pos++; + } + cout << endl; + system("pause"); + return 0; +} + +//Exercise 9.49 +#include +#include +#include +using namespace std; +int main(){ + string ascender("bdfhjklt"), descender("gjpqy"); + string invalid(ascender.append(descender)); + vector text; + string word; + while(cin >> word){ + text.push_back(word); + } + string::size_type maxlen = 0; + vector::const_iterator pos; + for(auto It = text.cbegin();It != text.cend(); It++){ + if((*It).find_first_of(invalid) == (*It).npos){ + if ((*It).size() > maxlen) { + maxlen = (*It).size(); + pos = It; + } + } + } + cout << *pos << endl; + system("pause"); + return 0; +} + +//Exercise 9.50.a +#include +#include +#include +using namespace std; +int main() { + int N; + cin >> N; + vector s(N); + for (int ix = 0; ix != N; ix++) + cin >> s[ix]; + int sum = 0; + for (auto It = s.cbegin(); It != s.cend(); It++) { + sum += stoi(*It); + } + cout << sum << endl; + system("pause"); + return 0; +} + +//Exercise 9.50.b +#include +#include +#include +using namespace std; +int main() { + int N; + cin >> N; + vector s(N); + for (int ix = 0; ix != N; ix++) + cin >> s[ix]; + double sum = 0; + for (auto It = s.cbegin(); It != s.cend(); It++) { + sum += stod(*It); + } + cout << sum << endl; + system("pause"); + return 0; +} + +//Exercise 9.51 +#include +#include +#include +#include +#define NUM_OF_MONTH 12 +using namespace std; +class date{ +private: + unsigned year; + unsigned month; + unsigned day; + const char* months[12]{"January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December" }; + const char* alphabet{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" }; +public: + date(string strDate); + date(); + unsigned get_year(){ + return year; + } + //get_month();get_day(); + void print_date(int format = 0); +}; + +//default constructor +date::date(){ + year = 1998; + month = 1; + day = 1; +} + +//constructor taking a string +date::date(string strDate){ + string strMonth, strYear, strDay; + if (strDate.find(' ') != strDate.npos) { + strMonth = strDate.substr(0, strDate.find(' ')); + int ix; + try { + for (ix = 0; ix != NUM_OF_MONTH; ix++) { + if (!strMonth.compare(months[ix]) || !strMonth.compare(string(months[ix], 3))) { + month = ix + 1; + break; + } + } + if (ix == NUM_OF_MONTH) throw runtime_error("input error: invalid month"); + strYear = strDate.substr(strDate.size() - 4); + if (strYear.find_first_of(alphabet) != strYear.npos) throw runtime_error("input error: invalid year"); + else year = stoi(strYear); + strDay = strDate.substr(strDate.find(' ') + 1, strDate.size() - 6); + if (strDay.find_first_of(alphabet) != strDay.npos) throw runtime_error("input error: invalid day"); + else day = stoi(strDay); + } + catch(runtime_error err){ + cout << err.what() << endl; + cout << "continue or not(y/n): "; + char c; + cin >> c; + if (c == 'n' || c == 'N') exit(0); + } + }else{//format 1/1/1998 + try { + strMonth = strDate.substr(0, strDate.find('/')); + if (strMonth.find_first_of(alphabet) != strMonth.npos) throw runtime_error("input error: invalid month"); + else month = stoi(strMonth); + strYear = strDate.substr(strDate.size() - 4); + if (strYear.find_first_of(alphabet) != strYear.npos) throw runtime_error("input error: invalid year"); + else year = stoi(strYear); + strDay = strDate.substr(strDate.find('/') + 1, strDate.size() - 5); + if (strDay.find_first_of(alphabet) != strDay.npos) throw runtime_error("input error: invalid day"); + else day = stoi(strDay); + } + catch(runtime_error err){ + cout << err.what() << endl; + cout << "continue or not(y/n): "; + char c; + cin >> c; + if (c == 'n' || c == 'N') exit(0); + } + } +} + +//print current date with various formats +void date::print_date(int format){ + switch(format){ + default: + case 0: + cout << months[month-1] << ' ' << day << ", " << year << endl;; + break; + case 1: + cout << string(months[month-1], 3) << ' ' << day << ", " << year << endl; + break; + case 2: + cout << month << '/' << day << '/' << year << endl; + break; + } +} + +//Exercise 9.52 +#include +#include +#include +using namespace std; +int main(){ + string expr{ "This is (Mooophy(awesome)((((wooooooooo))))) and (ocxs) over" }; + const char repl = '#'; + stack charStack; + int seen = 0; + for(auto It = expr.cbegin(); It != expr.cend(); It++){ + charStack.push(*It); + if (*It == '(') seen += 1; + if(*It == ')' && seen > 0){ + seen -= 1; + while (charStack.top() != '(') charStack.pop(); + charStack.pop(); + charStack.push(repl); + } + } + //output the result + string output; + for (; !charStack.empty(); charStack.pop()) + output.insert(output.begin(), charStack.top()); + cout << output << endl; + system("pause"); + return 0; +} diff --git a/c++ note/common_bugs.md b/c++ note/common_bugs.md new file mode 100644 index 0000000..120daa7 --- /dev/null +++ b/c++ note/common_bugs.md @@ -0,0 +1,8 @@ +Some Common Bugs +================ + +为了更好的debug,我觉得还是有必要把平时出现的有点难度的bug记录下来,以让自己知道你自己写的究竟是些啥??? + +> 在调用了构造函数,进行了必要的初始化,之后的操作却出现了空指针? + +因为我嵌套调用了构造函数。在构造函数中调用其他构造函数,只会产生一个匿名的对象,并不会帮助你初始化当前的对象。 \ No newline at end of file diff --git a/c++ note/exception.md b/c++ note/exception.md new file mode 100644 index 0000000..a2761be --- /dev/null +++ b/c++ note/exception.md @@ -0,0 +1,38 @@ +## exception + ++ 基本结构`try-catch` + + try{ + expression + }catch(errorType err){ + expression + }catch(errorType err){ + expression + }... ++ 文件支持 + + #include + //#include + using std::runtime_error ++ 具体使用方法 + - try block内放可能会产生异常的语句,并抛出(throw)相应的异常 + + try{ + if(...) throw runtime_error("fatal error: dividing zero"); + if(...) throw ... + //else + normal expression + } + - 在catch中检查可能会出现的异常,若异常与catch的异常类型对应,则进入相应的异常处理程序 + + try{ + ... + }catch(runtime_error err){ + cout << err.what() << endl;//err.what()返回抛出异常时初始化的字符串 + //other handling + cout << "continue or not(y/n): "; + char c; + cin >> c; + if(c == 'n' || c == 'N') break; + //else continue + } \ No newline at end of file