参考文献《大道至简:C++ STL》
- 一个demo
#include#include #include using namespace std; struct per { int _no; double _sc; void clear() { _no = 0; _sc = 0; } }; int main() { per temp; temp.clear(); list
p; p.clear(); cout << "Press any to continue..." << endl; cin.get(); int counter = 3; while(counter) { int no; double sc; cout << ">>" << endl; cin >> no >> sc; temp._no = no; temp._sc = sc; p.push_back(temp); memset(&temp, 0, sizeof(per)); counter--; } for(list ::iterator pit = p.begin(); pit != p.end(); pit++) { temp.clear(); temp = * pit; cout << temp._no << "," << temp._sc << endl; } return 0; }
- 文件读写demo
#include#include using namespace std; int main() { ifstream _ifs; ofstream _ofs; char file1[256]; char file2[256]; char context[256]; printf("2 file name(in and out) >>"); cin >> file1 >> file2; _ifs.open(file1, ios::in); _ofs.open(file2, ios::out); while (!_ifs.eof()) { _ifs.getline(context, 128); _ofs << context << endl; } _ifs.close(); _ofs.close(); return 0; }
- 类模板的静态成员demo
#includeusing namespace std; template class BasicClass { public: static int _a; static float _b; public: static int getA() { return _a; } static int getB() { return _b; } }; template int BasicClass ::_a = 1; template float BasicClass ::_b = 2.0; int main() { BasicClass bc; bc._a = 2; bc._b = 3.2; cout << bc.getA() << "," << bc.getB() << endl; return 0; }
- for_each的用法demo
#include#include #include #include using namespace std; void print(string& s) { cout << s << endl; } int main() { vector v_str(5); for (int i = 0; i < 5; i++) { getline(cin, v_str[i]); } for_each(v_str.begin(), v_str.end(), print); return 0; }



