sort简单使用

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的模板函数

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";
}

 

                                                                                                                   以上代码来自网上以及自己编写