create c++ note directory
This commit is contained in:
144
c++ note/chp10/chapter 10.cpp
Normal file
144
c++ note/chp10/chapter 10.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
//Exercise 10.2
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#define NUM 10
|
||||
using namespace std;
|
||||
int main(){
|
||||
list<string> 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 <vector>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
using namespace std;
|
||||
int main(){
|
||||
vector<int> intVec{ 5,4,3,1,2 };
|
||||
cout << accumulate(intVec.cbegin(), intVec.cend(), 0) << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Exercise 10.6
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#define NUM 10
|
||||
using namespace std;
|
||||
int main(){
|
||||
vector<int> 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 <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void print(vector<string> strVec);
|
||||
int main(){
|
||||
vector<string> 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<string> strVec){
|
||||
if(!strVec.empty()){
|
||||
for (auto It = strVec.cbegin(); It != strVec.cend(); It++)
|
||||
cout << *It << ' ';
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
//Exercise 10.13
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
void print(vector<string> strVec);
|
||||
bool have_five_char(string word){
|
||||
return word.size() >= 5;
|
||||
}
|
||||
int main(){
|
||||
vector<string> 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<string> 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 <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#define LENGTH 6
|
||||
using namespace std;
|
||||
int main(){
|
||||
vector<string> 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;
|
||||
}
|
||||
0
c++ note/chp7/exercise7_1.cpp
Normal file
0
c++ note/chp7/exercise7_1.cpp
Normal file
500
c++ note/chp9/chapter 9.cpp
Normal file
500
c++ note/chp9/chapter 9.cpp
Normal file
@@ -0,0 +1,500 @@
|
||||
#include <list>
|
||||
#include <deque>
|
||||
|
||||
//Exercise 9.2
|
||||
std::list<std::deque<int> > List;
|
||||
|
||||
//Exercise 9.4
|
||||
using std::vector;
|
||||
bool find(vector<int>::iterator front, vector<int>::iterator behind, int target){
|
||||
for(;front<behind;front++){
|
||||
if(*front == target) return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
//Exercise 9.5
|
||||
using std::vector;
|
||||
vector<int>::iterator find(vector<int>::iterator front, vector<int>::iterator behind, int target){
|
||||
for(;front<behind;front++){
|
||||
if(*front == target) return front;
|
||||
}
|
||||
return behind;
|
||||
}
|
||||
|
||||
//Exercise 9.6
|
||||
while(iter1 < iter2)/* ... */ --> while(iter1 != iter2)/* ... */
|
||||
_list DO NOT support `<` operator_
|
||||
|
||||
//Exercise 9.18
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <deque>
|
||||
using namespace std;
|
||||
int main(){
|
||||
deque<string> strdq;
|
||||
string currStr;
|
||||
while(cin >> currStr){
|
||||
strdq.push_back(currStr);
|
||||
}
|
||||
deque<string>::iterator dqIt;
|
||||
for(dqIt = strdq.begin(); dqIt != strdq.end(); dqIt++){
|
||||
cout << *dqIt;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Exercise 9.19
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
int main() {
|
||||
list<string> strdq;
|
||||
string currStr;
|
||||
while (cin >> currStr) {
|
||||
strdq.push_back(currStr);
|
||||
}
|
||||
list<string>::const_iterator dqIt;
|
||||
for (dqIt = strdq.cbegin(); dqIt != strdq.cend(); dqIt++) {
|
||||
cout << *dqIt << endl;
|
||||
}
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Exercise 9.20
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
using namespace std;
|
||||
int main(){
|
||||
list<int> iList = {8,9,5,3,1,2,6,4,7,0};
|
||||
deque<int> 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 <stdexcept>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
vector<int> 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 <vector>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
|
||||
vector<int> ivec;
|
||||
list<int> 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 <vector>
|
||||
#include <forward_list>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
forward_list<int> 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 <forward_list>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
forward_list<string> insertStr(forward_list<string> src, string pos, string destStr);
|
||||
int main(){
|
||||
forward_list<string> 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<string> insertStr(forward_list<string> 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 <string>
|
||||
#include <iostream>
|
||||
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 <string>
|
||||
#include <iostream>
|
||||
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 <iostream>
|
||||
#include <string>
|
||||
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 <iostream>
|
||||
#include <string>
|
||||
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 <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
int main(){
|
||||
string ascender("bdfhjklt"), descender("gjpqy");
|
||||
string invalid(ascender.append(descender));
|
||||
vector<string> text;
|
||||
string word;
|
||||
while(cin >> word){
|
||||
text.push_back(word);
|
||||
}
|
||||
string::size_type maxlen = 0;
|
||||
vector<string>::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 <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main() {
|
||||
int N;
|
||||
cin >> N;
|
||||
vector<string> 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 <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main() {
|
||||
int N;
|
||||
cin >> N;
|
||||
vector<string> 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 <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#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 <iostream>
|
||||
#include <string>
|
||||
#include <stack>
|
||||
using namespace std;
|
||||
int main(){
|
||||
string expr{ "This is (Mooophy(awesome)((((wooooooooo))))) and (ocxs) over" };
|
||||
const char repl = '#';
|
||||
stack<char> 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;
|
||||
}
|
||||
8
c++ note/common_bugs.md
Normal file
8
c++ note/common_bugs.md
Normal file
@@ -0,0 +1,8 @@
|
||||
Some Common Bugs
|
||||
================
|
||||
|
||||
为了更好的debug,我觉得还是有必要把平时出现的有点难度的bug记录下来,以让自己知道你自己写的究竟是些啥???
|
||||
|
||||
> 在调用了构造函数,进行了必要的初始化,之后的操作却出现了空指针?
|
||||
|
||||
因为我嵌套调用了构造函数。在构造函数中调用其他构造函数,只会产生一个匿名的对象,并不会帮助你初始化当前的对象。
|
||||
38
c++ note/exception.md
Normal file
38
c++ note/exception.md
Normal file
@@ -0,0 +1,38 @@
|
||||
## exception
|
||||
|
||||
+ 基本结构`try-catch`
|
||||
|
||||
try{
|
||||
expression
|
||||
}catch(errorType err){
|
||||
expression
|
||||
}catch(errorType err){
|
||||
expression
|
||||
}...
|
||||
+ 文件支持
|
||||
|
||||
#include <stdexcept>
|
||||
//#include <exception>
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user