#include "HashSet.h"
#include "Student.h"
#include<string>
using namespace std;
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::HashSeach(const key_equal& value)
{
vector<key_equal>::iterator iter = m_nIndex.begin();
for (; iter != m_nIndex.end();++iter)
{
if (*iter == value)
{
cout << "查找到该元素" << value<<endl;
m_filed = true;
return;
}
}
if(iter == m_nIndex.end())
{
m_filed = false;
cout << "该元素不存在" << endl;
}
}
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::myHashAdd(const key_equal& value)
{
HashSeach(value);
if (!m_filed)
{
cout << "可以添加" << " ";
m_nIndex.push_back(value);
}
}
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::myHashAdd(vector<key_equal> list)
{
sort(list.begin(), list.end());
vector<key_equal>::iterator iter=unique(list.begin(),list.end());
list.erase(iter, list.end());//unique是删除重复元素,但不是真的删除unique只是把重复的元素放到容器的后面,而它本身会返回一个迭代器,指向这些元素的开始部分
for (vector<key_equal>::iterator it = list.begin(); it != list.end();)
{
m_nIndex.push_back(*it);
++it;
}
sort(m_nIndex.begin(), m_nIndex.end());
vector<key_equal>::iterator iter1=unique(m_nIndex.begin(),m_nIndex.end());
m_nIndex.erase(iter1, m_nIndex.end());
copy(m_nIndex.begin(),m_nIndex.end(),ostream_iterator<key_equal>(cout," ")); //输出vector容器的所有元素
}
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::HashRemove(const key_equal& value)
{
vector<key_equal>::iterator iter= m_nIndex.begin();
for (; iter != m_nIndex.end();)
{
if ( *iter == value)
{
iter = m_nIndex.erase(iter);
}
else
{
++iter;
}
}
}
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::HashRemove(vector<key_equal> list)
{
sort(list.begin(), list.end());
vector<key_equal>::iterator iter;
iter = unique(list.begin(),list.end());
list.erase(iter, list.end());
for( vector<int>::size_type del = 0; del != list.size(); ++del)
{
for (vector<key_equal>::iterator it = m_nIndex.begin(); it != m_nIndex.end();)
{
if (*it == list[del])
{
it = m_nIndex.erase(it);
}
else
{
++it;
}
}
}
}
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::HashReplace(key_equal a, key_equal b)
{
for (vector<key_equal>::iterator it = m_nIndex.begin(); it != m_nIndex.end();)
{
if (*it == a)
{
*it = b;
}
else
{
++it;
}
}
}
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::HashClear()
{
m_nIndex.clear();
}
ostream & operator<< ( ostream & theStream,const CStudent & obj)
{
theStream << obj.m_StrName<<" " <<obj.m_Sex<<" "<<obj.m_nAge<<endl;
return theStream;
}
bool CStudent::operator==( const CStudent &obj ) const
{
if (this->m_nAge==obj.m_nAge && this->m_Sex == obj.m_Sex && this->m_StrName == obj.m_StrName)
{
return true;
}
else
return false;
//return (*this==obj);
}
bool CStudent::operator!=( const CStudent &obj ) const
{
return !(*this==obj);
}
CStudent& CStudent::operator=(const CStudent& b)
{
if(this == &b)
return * this;
this->m_nAge = b.m_nAge;
this->m_Sex = b.m_Sex;
this->m_StrName = b.m_StrName;
return *this;
}
template<class key_type, class key_equal>
void CHashSet<key_type, key_equal>::HashDisplay()
{
for (vector<int>::size_type i = 0; i != m_nIndex.size(); ++i)
{
cout << m_nIndex[i] << " ";
}
cout << endl;
}
//void CHashSet::ShowSeach()
//{
//
//}
int main(int argc,char* argv[])
{
//测试代码
CHashSet<int, int> hash;
hash.myHashAdd(1);
hash.HashDisplay();
CHashSet<int, string> hash1;
hash1.myHashAdd("asdua");
hash1.HashDisplay();
hash1.HashRemove("asdua");
vector<string> vecStr;
vector<string>::iterator it =vecStr.begin();
string str[5]={"张锋","陆放","李力","陆放","胡涛"};
int i=0;
for (;i<5;i++)
{
vecStr.insert(vecStr.begin(),str[i]);
}
hash1.myHashAdd(vecStr);
hash1.HashDisplay();
CStudent struct1;
struct1.m_StrName = "小风";
struct1.m_Sex = 's';
struct1.m_nAge = 24;
CHashSet<int, CStudent> hash2;
hash2.myHashAdd(struct1);
hash2.HashDisplay();
CStudent student2;
hash2.HashReplace(struct1,student2);
hash2.HashDisplay();
hash2.HashRemove(student2);
hash2.HashDisplay();
hash.myHashAdd(2);
hash.HashDisplay();
vector<int> list;
list.push_back(1);
list.push_back(2);
list.push_back(1);
list.push_back(3);
list.push_back(5);
hash.myHashAdd(list);
hash.HashDisplay();
//hash.HashRemove(list);
hash.HashReplace(3,10);
hash.HashDisplay();
system("pause");
return 0;
}
c++用vector实现HashSet
4星 · 超过85%的资源 需积分: 45 170 浏览量
2011-08-16
16:13:35
上传
评论 1
收藏 203KB RAR 举报
MR_Pursuer
- 粉丝: 0
- 资源: 19
最新资源
- 课程设计-基于耐火材料裂缝剥落检测python源码+课件
- 基于OpenCV的视频道路车道检测python源码+文档说明+实验演示+图片+使用方法(高分毕业设计)
- 基于OpenCV的案例:图像边缘、角点和轮廓检测,图像分割,图像增强;图片拼接;运动目标检测,颜色直方图比较,三帧帧差法,抠图
- SmartPlug-html大一笔记
- SmartPlug-proteusdemo
- Preliminary Findings on Handmade Rattan Baby Crib andBassinet Designs Regarding.zip
- aveebfq_v1.2.83_downyi.com.apk
- 基于有机发光二极管(OLED)的建模优化算法的matlab仿真源码+数据+文档说明+项目说明(高分课程设计)
- hash01-test.c 本人哈希表(一)的示例代码,仅供参考!
- 处理数据.py
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页