std:sort和字符串hash函数
2014-06-26 14:07
162 查看
class CStudent
{
public:
CStudent() {}
~CStudent() {}
bool operator<(const CStudent &stu) const
{
return num > stu.num;
}
int num;
};
struct cmp
{
bool operator() ( CStudent &a, CStudent &b )
{
return a.num < b.num;
}
};
bool rankStudent(const CStudent &stu1, const CStudent &stu2)
{
return stu1.num > stu2.num;
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<CStudent> vec;
for (int i = 0; i < 10; i++)
{
CStudent stu;
stu.num = rand();
vec.push_back(stu);
}
//sort(vec.begin(), vec.end());
sort(vec.begin(), vec.end(), rankStudent);
for (int i = 0; i < 10; i++)
{
cout<<vec[i].num<<"\t";
}
priority_queue<CStudent/*,vector<CStudent>,cmp*/> tmpQueue;
for (int i = 0; i < 10; i++)
{
CStudent stu;
stu.num = rand();
tmpQueue.push(stu);
}
while(!tmpQueue.empty())
{
CStudent &stu = tmpQueue.top();
cout<<stu.num<<"\t";
tmpQueue.pop();
}
getchar();
return 0;
}
unsigned long long hashCode(std::string str)
{
unsigned long long ulRet = 0;
for (unsigned int i = 0; i < str.size(); i++)
{
ulRet = 31 * ulRet + str[i];
}
return ulRet;
}
{
public:
CStudent() {}
~CStudent() {}
bool operator<(const CStudent &stu) const
{
return num > stu.num;
}
int num;
};
struct cmp
{
bool operator() ( CStudent &a, CStudent &b )
{
return a.num < b.num;
}
};
bool rankStudent(const CStudent &stu1, const CStudent &stu2)
{
return stu1.num > stu2.num;
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<CStudent> vec;
for (int i = 0; i < 10; i++)
{
CStudent stu;
stu.num = rand();
vec.push_back(stu);
}
//sort(vec.begin(), vec.end());
sort(vec.begin(), vec.end(), rankStudent);
for (int i = 0; i < 10; i++)
{
cout<<vec[i].num<<"\t";
}
priority_queue<CStudent/*,vector<CStudent>,cmp*/> tmpQueue;
for (int i = 0; i < 10; i++)
{
CStudent stu;
stu.num = rand();
tmpQueue.push(stu);
}
while(!tmpQueue.empty())
{
CStudent &stu = tmpQueue.top();
cout<<stu.num<<"\t";
tmpQueue.pop();
}
getchar();
return 0;
}
unsigned long long hashCode(std::string str)
{
unsigned long long ulRet = 0;
for (unsigned int i = 0; i < str.size(); i++)
{
ulRet = 31 * ulRet + str[i];
}
return ulRet;
}
相关文章推荐
- js将数组中的字符串按照第二个字符的大小排序(sort排序)
- 【转载】字符串Hash函数比较
- std::string字符串分割
- 各种字符串hash函数
- C/C++字符串处理:std::vector与std::StringBuilder
- std::sort
- 36:字符串排序SortString
- 关于 std::sort 自定义 比较函数 是 debug模式下报 invalid operator< 警告
- UVA 10905- Children's Game(sort处理string字符串)
- std::sort与qsort
- 字符串的hash函数
- 字符串Hash函数
- 字符串排序(sort)
- 各种字符串Hash函数
- C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique
- 使用std::sort()排序导致程序core问题分析
- 字符串hash函数
- 标准字符串std::string和System::String, CString之间的转换
- 各种字符串Hash函数比较
- 各种字符串Hash函数