c++学習点滴3
7552 ワード
一、異常処理
二、汎用アルゴリズム
0. 1
class MyException { //
public :
MyException(){errorCode = -1;}
MyException(int code) {errorCode = code;}
int get(){return errorCode;}
private :
int errorCode;
};
void fun () {
try {
throw MyException(1);
} catch (MyException& e) { // ,
cout << "exception: " << e.get() << endl;
}
}
int main() {
fun();
return 0;
}
2:
class MyException1 {};
class MyException2 {};
void fun () {
try {
int flag = 3; // flag ,
if (flag == 1) {
throw MyException1();
} else if (flag == 2) {
throw MyException2();
} else {
throw string();
}
} catch (MyException1& e) {
cout << "exception1 handled." << endl;
} catch (MyException2& e) {
cout << "exception2 handled." << endl;
} catch (...) { // catch-all
cout << "other exception handled." << endl;
}
cout << "after exception handled." << endl;
}
int main() {
fun();
return 0;
}
1. throw
// , throw
void sample() throw(string); // JAVA :void sampe() throws StringException;
//void sample(){} // error. throw
//void sample() throw (int) {} // error. throw
//void sample() throw (string, int){} // error. throw
void sample() throw (string) {} // ok. throw
2. c++ throw ( int, string, MyException)
3. throw 1 -- throw
// throw
void sample() throw (string) { // string
throw string("xxxx"); // OK.
}
int main() {
try {
sample();
} catch (string& str) { //
cout << "exception: " << str << endl;
}
return 0;
}
4. throw 2 -- throw
void sample() throw (string) { // string
throw int(-1); // string 。
// c++ unexpected(), terminate()
}
int main() {
try {
sample(); // int
} catch (string& str) {
cout << "exception: " << str << endl;
} catch (...) { // catch-all, 。 c++ unexpected()
cout << "catch all exceptioin" << endl;
}
cout << "after exception handler." << endl;
return 0;
}
5. throw 3 -- throw
// throw , throw
void sample() throw (string) { }
void (*pf)() throw (string) = sample; // ok。 。
void (*pf2)() throw (string,int) = sample; // ok.
void (*pf3)() throw() = sample; // ok。
void (*pf4)() = sample; // ok.
void (*pf5)() throw (int) = sample; // ok.
6.
二、汎用アルゴリズム
void printVec(vector<string>& svec);
bool comparestr(const string& str1, const string& str2);
//
class LessThan {
public:
// ()
bool operator()(const string& str1, const string& str2) {
return str1.size() < str2.size();
}
};
class GreaterThan{
public:
GreaterThan(){_size = 3;}
GreaterThan(int size) {_size = size;}
int size(){return _size;}
bool operator() (const string &str) {return str.size()>_size;}
private :
int _size;
};
class PrintElem {
public :
void operator() (const string& elem) {
cout << elem << " ";
}
};
void fun () {
string arr[] = {"aaa", "bbb", "ccc", "ddd"};
vector<string> svec(arr, arr+4);
printVec(svec);
vector<string> svec2;
svec2.push_back("xxx"); // xxx
// copy
copy(svec.begin(), svec.end(), back_inserter(svec2));
printVec(svec2); // xxx,aaa,bbb,ccc,ddd
// sort
sort(svec2.begin(), svec2.end());
printVec(svec2); // aaa,bbb,ccc,ddd,xxx
svec2.push_back("xxx");
svec2.push_back("aaa");
printVec(svec2); // aaa,bbb,ccc,ddd,xxx,xxx,aaa
// unique ,
vector<string>::iterator removedIter = unique(svec2.begin(), svec2.end(), comparestr);
printVec(svec2); // aaa, bbb, ccc, ddd, xxx, aaa, aaa,
svec2.erase(removedIter);
printVec(svec2); // aaa, bbb, ccc, ddd, xxx, aaa,
sort(svec2.begin(), svec2.end(), comparestr); //
printVec(svec2); // aaa, aaa, bbb, ccc, ddd, xxx,
//
string arr2[] = {"abc", "ab", "a"};
svec2 = vector<string>(arr2, arr2+3);
stable_sort(svec2.begin(), svec2.end(), LessThan()); //
printVec(svec2); // a, ab, abc,
//
string s1("abc");
string s2("ab");
LessThan lt;
bool b = lt(s1, s2); // size
cout << "lt(s1,s2): " << (b?"true":"flase") << endl; // false。 s1 size s2 size
// count_if
string arr3[] = {"aaa", "bbbb", "ddddd", "cc"};
svec2 = vector<string>(arr3, arr3+4);
int cnt = count_if(svec2.begin(), svec2.end(), GreaterThan(3)); // size 3
cout << "cnt=" << cnt << endl; // 2
cnt = count_if(svec2.begin(), svec2.end(), GreaterThan(2)); // size 2
cout << "cnt=" << cnt << endl; // 3
// remove
vector<string>::iterator iter = remove(svec2.begin(), svec2.end(), "bbbb"); // bbbb
printVec(svec2); // aaa, ddddd, cc, cc,
svec2.erase(iter, svec2.end());
printVec(svec2); // aaa, ddddd, cc,
// for-each
for_each(svec2.begin(), svec2.end(), PrintElem());
}
int main() {
fun();
return 0;
}
bool comparestr(const string& str1, const string& str2) {
return str1.compare(str2) == 0;
}
void printVec(const vector<string>& svec) {
vector<string>::const_iterator iter = svec.begin();
for (; iter != svec.end(); iter++) {
cout << *iter << ", ";
}
cout << endl;
}
、
class CompareInt{
public :
bool operator() (const int& i1, const int& i2) {
return i1 < i2;
}
};
template<typename Type, class Comp>
const Type& min3(const Type *p, int size, Comp comp) {
int minIndex = 0;
for (int i = 0; i < size; i++) {
if (comp(p[i], p[minIndex])) {
minIndex = i;
}
}
return p[minIndex];
}
の を います.int arr[] = {4,3,1,2};
cout << "min number: " << min3(arr, 4, CompareInt()) << endl; // 1
、 を る つの //
template <typename T>
const T& min1(const T *p, int size) {
int minIdx = 0;
for (int i = 0; i < size; i++) {
if (p[i] < p[minIdx]) {
minIdx = i;
}
}
return p[minIdx];
}
//
bool intComp(const int& i1, const int& i2) {
return i1 < i2;
}
template <typename T>
const T& min2(const T *p, int size, bool (*comp)(const T&, const T&) ){
int minIdx = 0;
for (int i = 0; i < size; i++) {
if (comp(p[i], p[minIdx])) {
minIdx = i;
}
}
return p[minIdx];
}
//
class Less{
public:
bool operator()(const int &v1, const int &v2) {
return v1 < v2;
}
};
template <typename T>
const T& min3(const T *p, int size, Less less){
int minIdx = 0;
for (int i = 0; i < size; i++) {
if (less(p[i], p[minIdx])) {
minIdx = i;
}
}
return p[minIdx];
}
void test () {
int arr[] = {4,1,3,2};
cout << "min number: " << min1(arr, 4) << endl;
cout << "min number: " << min2(arr, 4, intComp);
cout << "min number: " << min3(arr, 4, Less());
}