1.集合set的基本操作
集合中不能包含重复的元素,如果包含重复的元素,则将被自动剔除。同时实现自动排序
红黑树容器:
例如数据:1 2 3 4 5 6 7
经过排序后的结果为:
4
2 6
1 3 5 7
则中序遍历的结果为:1 2 3 4 5 6 7
#include<iostream>
#include<set>
#include<stdio.h>
using namespace std;
//红黑树容器,实现自动排序,中序遍历,舍弃重复的元素
void mains()
{
set<int> myset;
myset.insert(1);
myset.insert(2);
myset.insert(3);
myset.insert(6);
myset.insert(5);
myset.insert(4);
auto findpos = myset.find(6);
cout<<myset.size() << "find->" << *findpos << endl;
auto ib = myset.begin();
auto ie = myset.end();
for (;ib != ie;ib++)
{
cout << *ib << endl;
printf("%p,%p\n", ib._Ptr, ib);// 智能指针类
}
cin.get();
}
2. 通过重载实现利用集合set处理类对象与字符串
#include<iostream>
#include<set>
#include<string>
using namespace std;
//红黑树容器,实现自动排序,中序遍历,舍弃重复的元素
struct strless
{
bool operator()(const char *str1,const char *str2)
{
return strcmp(str1,str2) < 0;
}
};
//红黑树处理类对象和字符串
void mains()
{
//set不能包含重复元素
const char *cmd[] = { "calc","mspaint","notepad" };
set<const char *, strless> myset(cmd,cmd+3,strless()); //默认构造
//const char *p = "abc";
//cout << sizeof(cmd) << endl;指针数组
myset.insert(myset.begin(),"abc"); //插入元素
auto ib = myset.begin();
auto ie = myset.end();
for (;ib != ie;ib++)
{
cout << *ib << endl;
}
//查找
set<const char *, strless>::iterator pfind = myset.find("calc");
cout << "\n\n" << *pfind<<endl;
//复合集合 pair起到获取插入返回值,第一个类型,类型比大小的方式
pair<set<const char *>::iterator, bool>p=myset.insert("9876");
cout<< "pair\n"<< *(p.first) <<' '<< p.second << endl;
cin.get();
}
3. multiset 红黑树的每一个结点为链表,且允许用重复的元素
通过类的重载,对数据元素进行处理
#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<iostream>
#include<string>
//multiset每个结点都是一个链表,set每个结点都是一个结点
using namespace std;
struct student
{
int id;
char name[30];
};
//重载运算符进行排序
struct stuless
{
bool operator()(const student &s1, const student &s2)
{
return s1.id < s2.id;
}
};
void mainms()
{
student sarray[3] = { {5,"lianlian"},{3,"panpan"},{6,"manman"} };
multiset<student, stuless> myset(sarray,sarray+3,stuless());//默认构造
student stu1;
stu1.id = 8;
strcpy(stu1.name, "papi1");
myset.insert(stu1);
strcpy(stu1.name, "papi2");
myset.insert(stu1);
strcpy(stu1.name, "papi3");
myset.insert(stu1);
auto ib = myset.begin();
auto ie = myset.end();
for (;ib != ie;ib++)
{
cout << (*ib).id << " " << (*ib).name << endl;
}
cin.get();
}
void mainse()
{
multiset<int> myset;
myset.insert(100);
myset.insert(100);
myset.insert(100);
myset.insert(100);
myset.insert(111);
myset.insert(123);
auto pfind = myset.find(111);
cout << *pfind << endl;
//找到红黑树的链表结点,遍历所有元素
auto allfind = myset.equal_range(100);
//first链表的头结点,second左、最后一个空结点,遍历所有元素
for (auto it = allfind.first;it != allfind.second;it++)
{
cout << *it << endl;
}
cin.get();
}
4. bitset指定数据通过二进制数表示。
#include<set>
#include<bitset>
#include<iostream>
#include<string>
using namespace std;
void main()
{
//数据进行八位二进制表示
bitset<8> bs(255);
for (int i = 7;i>=0;i--)
{
cout << bs[i];
}
cout << endl;
for (int i = 7;i>=0;i--)
{
cout << ~bs[i];
}
int num = 255;
bitset<32> bs32(num);
cout << endl;
for (int i = 31;i >= 0;i--)
{
cout << bs32[i];
}
cin.get();
}
void mainbs2()
{
int num = 5;
bitset<32> myset(num);
for (int i = 31;i >= 0;i--)
{
cout << myset[i];
}
string str = myset.to_string();
cout<<'\n' << str << endl;
unsigned int data;
data = myset.to_ulong();
cout << data << endl;
cin.get();
}
void mainbs4()
{
bitset<8>bs(255);
bs.set(7, 1);//操作二进制位
cout << bs.size() << endl;
//cout << bs.reset;全部清零
cout << bs.none();//测试有没有越界
for (int i = 7;i >= 0;i--)
{
cout << bs[i];
}
cin.get();
}
5.映射map的基本操作,map也会进行自动排序,包含重复的元素会将映射值相同的元素覆盖。
#include<map>//也是红黑树
#include<iostream>
using namespace std;
void mainm1()
{
map<const char *, int> mymap;
mymap["司令"]=10; //映射,左边映射右边
mymap["军长"] = 9;
mymap["师长"] = 8;
mymap["旅长"] = 7;
cout << mymap["司令"] << endl;
cout << mymap["军长"] << endl;
cout << mymap["师长"] << endl;
cout << mymap["旅长"] << endl;
map<int,const char *> mymap1;
mymap1[10] = "司令"; //映射,左边映射右边
mymap1[9] = "军长";
mymap1[8] = "师长";
mymap1[7] = "旅长";
cout << mymap1[10] << endl;
cout << mymap1[9] << endl;
cout << mymap1[8] << endl;
cout << mymap1[7] << endl;
cin.get();
}
struct student
{
char *name;
int year;
};
struct stuinfo
{
int id;
student stu;
};
void main()
{
stuinfo infoarry[] = { {3,{"wang",1993}},
{ 1,{ "zhang",1994 } },
{ 2,{ "li",1995 } } };
map< int, student> m;
for (int i = 0;i < 3;i++)
{ //编号映射结构体学生的信息
m[infoarry[i].id] = infoarry[i].stu;
}
stuinfo infoarrys = { 4,{"zhao",1994} };
m[infoarrys.id] = infoarrys.stu;
auto ib = m.begin();
auto ie = m.end();
for (;ib != ie;ib++)
{
cout << (*ib).first << endl;
cout << (*ib).second.name << ' ' << (*ib).second.year << endl;
}
cin.get();
}
6. multimap 的基本操作
multimap每一个结点都是一个链表,允许有重复的元素, 映射的为链表的开头,同时也可以包含相同的元素,通过equal_range()函数找出与关键字相同的元素
#include<iostream>
#include<map>
#include<string>
using namespace std;
//multimap每一个结点都是一个链表,允许有重复的元素, 映射的为链表的开头
void mainmm()
{
multimap<const char *, int> mymap;
mymap.insert(pair<const char *,int>("司令",10)); //pair进行相应值的匹配
mymap.insert(pair<const char *, int>("司令", 100));
mymap.insert(pair<const char *, int>("司令2", 1000));
multimap<const char *, int>::iterator ib = mymap.begin();
auto ie = mymap.end();
for (;ib != ie;ib++)
{
cout << (*ib).first<<' '<< (*ib).second << endl;
}
cin.get();
}
void mainme()
{
multimap<string,string> mymap;
mymap.insert(pair<string, string>("zhaosi","wangmazi"));
mymap.insert(pair<string, string>("zhaosi", "wangmazi1"));
mymap.insert(pair<string, string>("zhaosi", "wangmazi2"));
mymap.insert(pair<string, string>("zhaosi", "wangmazi3"));
auto ib = mymap.begin();
auto ie = mymap.end();
for (;ib != ie;ib++)
{
cout << (*ib).first << ' '<<(*ib).second << endl;
}
auto pfind = mymap.find("zhaosi");
cout<<"\n\n\n\n" << (*pfind).first <<' '<< (*pfind).second << endl;
//从红黑树上找出关键字相同的元素
auto it = mymap.equal_range("zhaosi");
for (auto i = it.first;i != it.second;i++)
{
cout << (*i).first << ' ' << (*i).second << endl;
}
cin.get();
}
7. hash, hash_set,hash_map 基本操作方法
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include<hash_set>
#include<hash_map>
#include<iostream>
#include<algorithm>
using namespace std;
//hash_set与hash_map不会进行排序
//哈希表原理:哈希表对数据取余数,根据余数获得数据
// 精确查找,快速一次找到,比二分查找快
/*hash_set*/
void main1()
{
hash_set<int> hs;
hs.insert(11);
hs.insert(31);
hs.insert(21);
hs.insert(41);
hs.insert(61);
auto ib = hs.begin();
auto ie = hs.end();
for (;ib != ie;ib++)
{
cout<<*ib<< endl;
}
auto pfind = hs.find(21);
if (pfind == ie)
{
cout << "Not Find" << endl;
}
else
{
cout << *pfind << endl;
}
cin.get();
}
//字符串hash表
void main2()
{
hash_set<const char *>hs;
hs.insert("dgdgdgd");
hs.insert("ffgthrhrt");
hs.insert("fhsdthrth");
auto pfind = hs.find("dgdgdfgfd");
if (pfind == hs.end())
{
cout << "Not Find" << endl;
}
else
{
cout << *pfind << endl;
}
cin.get();
}
/*hash_map*/
void main()
{
hash_map<const char *, int>myhmap;
myhmap["zhangsan"] = 19;
myhmap["lisi"] = 18;
myhmap["zhaosi"] = 20;
cout<<myhmap["zhangsan"]<<endl;
cout<<myhmap["lisi"]<<endl;
cout<<myhmap["zhaosi"]<<endl;
cin.get();
}
下一篇:string容器,容器与算法以及兰不达表达式(Lambda)