首页 > 编程语言 > 详细

C++代码片段

时间:2014-04-08 02:56:50      阅读:512      评论:0      收藏:0      [点我收藏+]

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

 

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

 

本文出自 “Never Stop Sharing” 博客,请务必保留此出处http://loma1990.blog.51cto.com/6082839/1391831

C++代码片段,布布扣,bubuko.com

C++代码片段

原文:http://loma1990.blog.51cto.com/6082839/1391831

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!