sort簡單使用ios struct Record { string name; // ... }; struct name_compare { // compare Records using "name" as the key bool operator()(const Record& a, const Record& b) const { return a.name<b.name; } }; void f(vector<Record>& vs) { sort(vs.begin(), vs.end(), name_compare()); // ... }
|
delete後自動賦值爲0的模板函數ide template<class T> inline void destroy(T*& p) { delete p; p = 0; }
|
整數轉字符串函數 #include<iostream> #include<string> #include<sstream> using namespace std; string itos(int i) // convert int to string { stringstream s; s << i; return s.str(); } int main() { int i = 127; string ss = itos(i); const char* p = ss.c_str(); cout << ss << " " << p << "\n"; }
|
以上代碼來自網上以及本身編寫 |