stack容器
stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口,形式如图所示。stack容器允许新增元素,移除元素,取得栈顶元素,但是除了最顶端外,没有任何其他方法可以存取stack的其他元素。换言之,stack不允许有遍历行为。
有元素推入栈的操作称为:push,将元素推出stack的操作称为pop.
Stack所有元素的进出都必须符合”先进后出”的条件,只有stack顶端的元素,才有机会被外界取用。Stack不提供遍历功能,也不提供迭代器。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stack>
using namespace std;
//打印栈容器中的元素
void PrintStack(stack<int> stk) {
while (!stk.empty()) {
cout << stk.top() << " ";
stk.pop();
}
cout << endl;
}
//stack容器
void test01(){
//创建栈容器 栈容器必须符合先进后出
stack<int> s1,s2;
//向栈顶添加元素,叫做 压栈(入栈)
for (int i = 0; i < 6; i++) {
s1.push(i + 3);
}
s2 = s1;
stack<int> s3(s2);
PrintStack(s1);
PrintStack(s2);
PrintStack(s3);
cout << "s1 size:" << s1.size() << endl;
cout << "s2 size:" << s2.size() << endl;
cout << "s3 size:" << s3.size() << endl;
}
int main(){
test01();
return EXIT_SUCCESS;
}
queue容器
Queue是一种先进先出(First In First Out,FIFO)的数据结构,它有两个出口,queue容器允许从一端新增元素,从另一端移除元素。
Queue所有元素的进出都必须符合”先进先出”的条件,只有queue的顶端元素,才有机会被外界取用。Queue不提供遍历功能,也不提供迭代器。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<queue>
using namespace std;
//队列容器 先进先出
void test01(){
//创建队列
queue<int> q;
//向队列中添加元素 入队操作
q.push(10);
q.push(20);
q.push(30);
q.push(40);
//队列不提供迭代器,更不支持随机访问
while (!q.empty()){
//输出队首元素
cout << q.front() << " ";
//弹出队首元素
q.pop();
}
cout << endl;
cout << "size:" << q.size() << endl;
}
int main(){
test01();
return EXIT_SUCCESS;
}
list容器
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
相较于vector的连续线性空间,list就显得负责许多,它的好处是每次插入或者删除一个元素,就是配置或者释放一个元素的空间。因此,list对于空间的运用有绝对的精准,一点也不浪费。而且,对于任何位置的元素插入或元素的移除,list永远是常数时间。
List和vector是两个最常被使用的容器。list容器是一个双向链表。
n 采用动态存储分配,不会造成内存浪费和溢出
n 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素
n 链表灵活,但是空间和时间额外耗费较大
List容器不能像vector一样以普通指针作为迭代器,因为其节点不能保证在同一块连续的内存空间上。List迭代器必须有能力指向list的节点,并有能力进行正确的递增、递减、取值、成员存取操作。所谓”list正确的递增,递减、取值、成员取用”是指,递增时指向下一个节点,递减时指向上一个节点,取值时取的是节点的数据值,成员取用时取的是节点的成员。
由于list是一个双向链表,迭代器必须能够具备前移、后移的能力,所以list容器提供的是Bidirectional Iterators.
List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效。这在vector是不成立的,因为vector的插入操作可能造成记忆体重新配置,导致原有的迭代器全部失效,甚至List元素的删除,也只有被删除的那个元素的迭代器失效,其他迭代器不受任何影响。
list容器不仅是一个双向链表,而且还是一个循环的双向链表。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<list>
#include<vector>
using namespace std;
//1. 初始化
/*
list<T> lstT;//list采用采用模板类实现,对象的默认构造形式:
list(beg,end);//构造函数将[beg, end)区间中的元素拷贝给本身。
list(n,elem);//构造函数将n个elem拷贝给本身。
list(const list &lst);//拷贝构造函数。
*/
//1.list构造函数
void test01(){
list<int> list1; //无参构造
list<int> list2(10, 10);
list<int> list3(list2.begin(), list2.end());
for (list<int>::iterator it = list2.begin(); it != list2.end(); it++) {
cout << *it << " ";
}
cout << endl;
list3.push_back(100);
//正向打印
for (list<int>::iterator it = list3.begin(); it != list3.end(); it ++){
cout << *it << " ";
}
cout << endl;
//逆向打印
for (list<int>::reverse_iterator rit = list3.rbegin(); rit != list3.rend();rit ++){
cout << *rit << " ";
}
cout << endl;
}
//正向打印list容器中的所有元素
void PrintList(const list<int>& L){
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){
cout << *it << " ";
}
cout << endl;
}
//2. 插入和删除
/*
push_back(elem);//在容器尾部加入一个元素
pop_back();//删除容器中最后一个元素
push_front(elem);//在容器开头插入一个元素
pop_front();//从容器开头移除第一个元素
insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
clear();//移除容器的所有数据
erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
erase(pos);//删除pos位置的数据,返回下一个数据的位置。
remove(elem);//删除容器中所有与elem值匹配的元素。
*/
void test02(){
list<int> L;
L.push_back(10);
L.push_back(20);
L.push_front(100);
L.push_back(20);
L.push_front(200);
L.push_back(20);
//指定位置插入元素
list<int>::iterator it = L.begin();
it++;
L.insert(it,300);
PrintList(L);
L.pop_back();//尾删
PrintList(L);
L.pop_front();//头删
PrintList(L);
//指定位置删除
it = L.begin();
it++;
L.erase(it);
PrintList(L);
//按值删除 把所有值相同的元素全部删除
L.remove(20);
PrintList(L);
//L.clear();//删除容器中的所有元素
L.erase(L.begin(), L.end());//相当于L.clear()
cout << L.size() << endl;
}
//3. 大小操作
/*
size();//返回容器中元素的个数
empty();//判断容器是否为空
resize(num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置;如果容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置,如果容器变短,则末尾超出容器长度的元素被删除。
*/
void test03(){
list<int> L;
L.push_back(10);
L.push_back(20);
L.push_front(100);
L.push_back(20);
L.push_front(200);
L.push_back(20);
cout << "size:" << L.size() << endl;
if (L.empty()){
cout << "空!" << endl;
}
else{
cout << "不空!" << endl;
}
L.resize(8);
PrintList(L);
L.resize(10,1);
PrintList(L);
L.resize(3);
PrintList(L);
}
//4. 赋值操作
/*
assign(beg, end);//将[beg, end)区间中的数据拷贝赋值给本身。
assign(n, elem);//将n个elem拷贝赋值给本身。
list& operator=(const list &lst);//重载等号操作符
swap(lst);//将lst与本身的元素互换。
*/
void test04(){
list<int> L1;
list<int> L2;
L1.assign(10, 1);
L2.assign(10, 2);
cout << "交换之前:" << endl;
PrintList(L1);
PrintList(L2);
cout << "交换之后:" << endl;
L1.swap(L2);
PrintList(L1);
PrintList(L2);
}
//5. 存取和反转 排序
/*
front();//返回第一个元素。
back();//返回最后一个元素。
reverse();//反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
sort(); //list排序
*/
bool MyCompare(int v1,int v2){
return v1 > v2;
}
void test05(){
list<int> L;
L.push_back(90);
L.push_back(30);
L.push_back(20);
L.push_back(70);
cout << "front:" << L.front() << endl;//获得容器首元素
cout << "back:" << L.back() << endl;//获得容器尾部元素
//反转容器的元素
cout << "反转之前:" << endl;
PrintList(L);
cout << "反转之后:" << endl;
L.reverse();
PrintList(L);
cout << "从小到大排序:" << endl;
//排序
//list容器自身提供的排序成员方法sort(),默认的排序规则是从小到大
L.sort();
PrintList(L);
cout << "从大到小排序:" << endl;
//指定规则,从大到小
L.sort(MyCompare);
PrintList(L);
}
class Person{
public:
Person(int id,int age){
mID = id;
mAge = age;
}
public:
int mID;
int mAge;
};
bool ComparePerson(Person& p1, Person& p2){
if (p1.mAge == p2.mAge){
return p1.mID < p2.mID;
}
else if (p1.mAge > p2.mAge){
return true;
}
else
return false;
}
void test06(){
list<Person> lPerson;
Person p1(1, 10);
Person p2(2, 10);
Person p3(3, 30);
Person p4(4, 40);
lPerson.push_back(p1);
lPerson.push_back(p4);
lPerson.push_back(p2);
lPerson.push_back(p3);
lPerson.sort(ComparePerson);
for (list<Person>::iterator it = lPerson.begin(); it != lPerson.end(); it ++){
cout << "Age:" << it->mAge << " ID:" << it->mID << endl;
}
}
int main(void)
{
//test01();
//test02();
//test03();
//test04();
//test05();
test06();
return EXIT_SUCCESS;
}
set/multiset容器
Set的特性是。所有元素都会根据元素的键值自动被排序。Set的元素不像map那样可以同时拥有实值和键值,set的元素即是键值又是实值。Set不允许两个元素有相同的键值。
我们可以通过set的迭代器改变set元素的值吗?不行,因为set元素值就是其键值,关系到set元素的排序规则。如果任意改变set元素值,会严重破坏set组织。换句话说,set的iterator是一种const_iterator.
set拥有和list某些相同的性质,当对容器中的元素进行插入操作或者删除操作的时候,操作之前所有的迭代器,在操作完成之后依然有效,被删除的那个元素的迭代器必然是一个例外。
multiset特性及用法和set完全相同,唯一的差别在于它允许键值重复。set和multiset的底层实现是红黑树,红黑树为平衡二叉树的一种。
树的简单知识:
二叉树就是任何节点最多只允许有两个字节点。分别是左子结点和右子节点。
二叉搜索树,是指二叉树中的节点按照一定的规则进行排序,使得对二叉树中元素访问更加高效。二叉搜索树的放置规则是:任何节点的元素值一定大于其左子树中的每一个节点的元素值,并且小于其右子树的值。因此从根节点一直向左走,一直到无路可走,即得到最小值,一直向右走,直至无路可走,可得到最大值。那么在儿茶搜索树中找到最大元素和最小元素是非常简单的事情。下图为二叉搜索树:
上面我们介绍了二叉搜索树,那么当一个二叉搜索树的左子树和右子树不平衡的时候,那么搜索依据上图表示,搜索9所花费的时间要比搜索17所花费的时间要多,由于我们的输入或者经过我们插入或者删除操作,二叉树失去平衡,造成搜索效率降低。
所以我们有了一个平衡二叉树的概念,所谓的平衡不是指的完全平衡。
RB-tree(红黑树)为二叉树的一种。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<string>
using namespace std;
//1. 初始化
/*
3.7.2.1 set构造函数
set<T> st;//set默认构造函数:
mulitset<T> mst; //multiset默认构造函数:
set(const set &st);//拷贝构造函数
3.7.2.2 set赋值操作
set& operator=(const set &st);//重载等号操作符
swap(st);//交换两个集合容器
3.7.2.3 set大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
3.7.2.4 set插入和删除操作
insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);//删除容器中值为elem的所有元素。
*/
//打印set容器的所有元素
void PrintSet(set<int> s){
for (set<int>::iterator it = s.begin(); it != s.end(); it ++){
cout << *it << " ";
}
cout << endl;
}
//打印multiset容器的所有元素
void PrintMultiSet(multiset<int> mts) {
for (multiset<int>::iterator it = mts.begin(); it != mts.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01(){
set<int> s1,s2;
multiset<int> s3;
s1.insert(10);
s1.insert(90);
s1.insert(60);
s1.insert(30);
s1.insert(50);
s2.insert(100);
s2.insert(90);
s2.insert(60);
s2.insert(20);
s2.insert(50);
s3.insert(12);
s3.insert(25);
s3.insert(20);
s3.insert(10);
s3.insert(12);
PrintSet(s1);
PrintSet(s2);
PrintMultiSet(s3);
//根据值删除
s1.erase(30);
s2.erase(s2.begin());
s3.erase(12);//删除所有匹配的值
PrintSet(s1);
PrintSet(s2);
PrintMultiSet(s3);
s1.swap(s2);
PrintSet(s1);
PrintSet(s2);
}
//2. 查找操作
/*
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//查找键key的元素个数
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/
void test02(){
set<int> s1;
s1.insert(10);
s1.insert(90);
s1.insert(60);
s1.insert(30);
s1.insert(50);
//find返回迭代器,如果没有找到返回end迭代器
set<int>::iterator pos = s1.find(900);
if (pos == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << *pos << endl;
}
//统计键值为key的元素个数
cout << s1.count(60) << endl;
PrintSet(s1);
//lower_bound upper_bound
pos = s1.lower_bound(30);
if (pos == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << *pos << endl;
}
pos = s1.upper_bound(30);
if (pos == s1.end()){
cout << "No 找到!" << endl;
}
else{
cout << "找到:" << *pos << endl;
}
//equal_range 返回lower_bound 和 upper_bound的迭代器
pair<set<int>::iterator,set<int>::iterator> it = s1.equal_range(30);
if (it.first == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << *(it.first) << endl;
}
if (it.second == s1.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << *(it.second) << endl;
}
}
//3. set、multiset区别
void test03(){
//set不允许有两个相同键值
set<int> s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second){
cout << "第一次插入成功!" << endl;
}
else{
cout << "第一次插入失败!" << endl;
}
ret = s.insert(10);
if (ret.second){
cout << "第二次插入成功!" << endl;
}
else{
cout << "第二次插入失败!" << endl;
}
//multiset允许键值重复
multiset<int> ms;
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it ++){
cout << *it << " ";
}
cout << endl;
}
//4. 指定set容器的排序规则
//写个函数对象
class MyCompare01{
public:
//重载()运算符
bool operator()(int v1,int v2){
return v1 > v2;
}
};
void test04(){
//set容器在赋值时就会默认从小到大排序,为了从大到小排序就需要在赋值时就让它从大到小排序
//自定义一个函数对象,并作为set容器的参数传入即可
set<int, MyCompare01> s1;
s1.insert(10);
s1.insert(90);
s1.insert(60);
s1.insert(30);
s1.insert(50);
//默认从小到大
//PrintSet(s1);//error,此时由于set容器的类型是set<int,MyCompare>
//所以需要重新写打印输出的代码
for (set<int, MyCompare01>::iterator it = s1.begin(); it != s1.end(); it ++){
cout << *it << " ";
}
cout << endl;
}
//set容器中存放对象
/*
由于set容器在赋值的时候默认就会排序,但当存入的元素时对象的时候,
//set容器就不知道该按对象中的哪个成员属性去排序,
所以这时候就应该重载()运算符告诉set容器该按哪个成员属性去排序。
*/
class Person {
public:
Person(string name, int age) {
this->mName = name;
this->mAge = age;
}
public:
string mName;
int mAge;
};
//自定义函数对象(仿函数),告诉set容器该按哪个成员属性去排序
class MyCompare02{
public:
//重载()运算符
bool operator()(const Person& p1, const Person& p2) {
return p1.mAge > p2.mAge;
}
};
void test05() {
//创建set容器对象s,并传入自定义的比较类
//使得在插入元素时set容器能按照自定义的函数对象(仿函数)对类对象进行排序
set<Person, MyCompare02> s;
Person p1("aaa", 20);
Person p2("bbb", 30);
Person p3("ccc", 60);
Person p4("ddd", 50);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, MyCompare02>::iterator it = s.begin(); it != s.end(); it++) {
cout << "Name:" << it->mName << " Age:" << it->mAge << endl;
}
}
int main(void)
{
//test01();
//test02();
//test03();
//test04();
test05();
return 0;
}
对组(pair)
对组(pair)将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可以分别用pair的两个公有属性first和second访问。
类模板:template <class T1,class T2> struct pair.
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
int main(){
//1. 创建对组
pair<int, int> p(10, 20);
cout << p.first << endl;
cout << p.second << endl;
pair<string, int> p2 = make_pair("hello", 10);
cout << p2.first << endl;
cout << p2.second << endl;
system("pause");
return EXIT_SUCCESS;
}
map/multimap容器
Map的特性是,所有元素都会根据元素的键值自动排序。Map所有的元素都是pair,同时拥有实值和键值,pair的第一元素被视为键值,第二元素被视为实值,map不允许两个元素有相同的键值。
我们可以通过map的迭代器改变map的键值吗?答案是不行,因为map的键值关系到map元素的排列规则,任意改变map键值将会严重破坏map组织。如果想要修改元素的实值,那么是可以的。
Map和list拥有相同的某些性质,当对它的容器元素进行新增操作或者删除操作时,操作之前的所有迭代器,在操作完成之后依然有效,当然被删除的那个元素的迭代器必然是个例外。
Multimap和map的操作类似,唯一区别multimap键值可重复。
Map和multimap都是以红黑树为底层实现机制。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
#include<string>
using namespace std;
//1. 初始化
/*
map<T1, T2> mapTT;//map默认构造函数:
map(const map &mp);//拷贝构造函数
map所有元素都是一个对组
*/
void test01(){
map<int, int> m;
//第一种插入方式
m.insert(pair<int,int>(1,1));
//第二种插入方式
m.insert(make_pair(2, 2));
//第三种插入方式
m.insert(map<int, int>::value_type(3, 3));
//第四种插入方式
m[4] = 4; //map只是重载了[],但是并不支持随机访问
for (map<int, int>::iterator it = m.begin(); it != m.end();it ++){
cout << "key:" << it->first << " value:" << it->second << endl;
}
//如果通过[]方式访问一个不存在的key,那么map会创建一个指定key的元素,value默认值,插入到map中
cout << m[5] << endl;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){
cout << "key:" << it->first << " value:" << it->second << endl;
}
}
//2. 删除
void test02(){
map<int, int> m;
//第一种插入方式
m.insert(pair<int, int>(1, 1));
//第二种插入方式
m.insert(make_pair(2, 2));
//第三种插入方式
m.insert(map<int, int>::value_type(3, 3));
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){
cout << "key:" << it->first << " value:" << it->second << endl;
}
cout << "------------" << endl;
m.erase(2);
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){
cout << "key:" << it->first << " value:" << it->second << endl;
}
}
//3. map查找
/*
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;/若不存在,返回map.end();
count(keyElem);//返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。
lower_bound(keyElem);//返回第一个key<=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/
void test03(){
map<int, int> m;
//第一种插入方式
m.insert(pair<int, int>(1, 1));
//第二种插入方式
m.insert(make_pair(2, 2));
//第三种插入方式
m.insert(map<int, int>::value_type(3, 3));
map<int, int>::iterator it = m.find(2);
if (it == m.end()){
cout << "找不到!" << endl;
}
else{
cout << "找到:" << it->first << ":" << it->second << endl;
}
cout << "----------------" << endl;
//lower_bound upper_bound equal_range
it = m.lower_bound(2);
if (it == m.end()){
cout << "找不到!" << endl;
}
else{
cout << "找到:" << it->first << ":" << it->second << endl;
}
it = m.upper_bound(2);
if (it == m.end()){
cout << "找不到!" << endl;
}
else{
cout << "找到:" << it->first << ":" << it->second << endl;
}
cout << "----------------" << endl;
pair< map<int, int>::iterator, map<int, int>::iterator> ret = m.equal_range(2);
if (ret.first == m.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << ret.first->first << ":" << ret.first->second << endl;
}
if (ret.second == m.end()){
cout << "没有找到!" << endl;
}
else{
cout << "找到:" << ret.second->first << ":" << ret.second->second << endl;
}
}
//4. 指定map的排序规则
class MyCompare{
public:
bool operator()(int v1,int v2){
return v1 > v2;
}
};
void test04(){
//默认从小到大排序
map<int, int, MyCompare> m;
m.insert(make_pair(7, 2));
m.insert(make_pair(2, 4));
m.insert(make_pair(3, 9));
m.insert(make_pair(8, 1));
m.insert(make_pair(9, 9));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++){
cout << "key:" << it->first << " value:" << it->second << endl;
}
}
int main(){
//test01();
//test02();
//test03();
test04();
system("pause");
return EXIT_SUCCESS;
}
员工分组案例
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<time.h>
using namespace std;
enum{CAIWU,RENLI,YANFA};
class Worker{
public:
string mName;
int mSalary;
};
//创建员工
void Create_Worker(vector<Worker>& wlist){
string nameSeed = "ABCDE";
for (int i = 0; i < 5;i++){
Worker worker;
worker.mName = "员工";
worker.mName += nameSeed[i];
worker.mSalary = rand() % 10000 + 10000;
wlist.push_back(worker);
}
}
//员工分组
void Set_Group(vector<Worker>& wlist, multimap<int, Worker>& group){
srand((unsigned int)time(NULL));
for (vector<Worker>::iterator it = wlist.begin(); it != wlist.end(); it ++){
//随机产生部门编号
int departmentID = rand() % 3;
//cout << departmentID << endl;
//员工插入到分组中
group.insert(make_pair(departmentID,*it));
}
}
//按组打印员工信息
void ShowWorkersByGroup(multimap<int, Worker>& group){
cout << "财务部门:" << endl;
multimap<int, Worker>::iterator pos = group.find(CAIWU);
//统计下本组多少人
int wcount = group.count(CAIWU);
int index = 0;
for (; pos != group.end() && index < wcount;pos++,index++){
cout << "Name:" << pos->second.mName << " Salary:" << pos->second.mSalary << endl;
}
cout << "----------------" << endl;
cout << "人力部门:" << endl;
pos = group.find(RENLI);
wcount = group.count(RENLI);
index = 0;
for (; pos != group.end() && index < wcount; pos++,index++){
cout << "Name:" << pos->second.mName << " Salary:" << pos->second.mSalary << endl;
}
cout << "----------------" << endl;
cout << "人力部门:" << endl;
pos = group.find(YANFA);
wcount = group.count(YANFA);
index = 0;
for (; pos != group.end() && index < wcount; pos++,index++){
cout << "Name:" << pos->second.mName << " Salary:" << pos->second.mSalary << endl;
}
cout << "----------------" << endl;
}
int main(){
//创建员工
vector<Worker> vWorker;
Create_Worker(vWorker);
//保存员工的分组信息
multimap<int, Worker> mGroup;
Set_Group(vWorker, mGroup);
//打印员工信息
ShowWorkersByGroup(mGroup);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;
class myclass{
public:
myclass(char* data){
int len = strlen(data) + 1; //计算传进来的字符串长度
this->data = new char[len]; //在堆区分配了len字节内存
strcpy(this->data, data); //将数据拷贝到我们在堆分配的内存中
}
//增加拷贝构造函数
myclass(const myclass& mc){
int len = strlen(mc.data) + 1;
this->data = new char[len];
strcpy(this->data, mc.data);
}
//重载operator=操作符
myclass& operator=(const myclass& mc){
if (this->data != NULL){
delete[] this->data;
this->data = NULL;
}
int len = strlen(mc.data) + 1;
this->data = new char[len];
strcpy(this->data, mc.data);
return *this;
}
//既然我们在堆区分配了内存,需要在析构函数中释放内存
~myclass(){
if (NULL != this->data){
delete[] this->data;
this->data = NULL;
}
}
private:
char* data;
};
void test_deep_copy(){
//STL中所有的容器都是值寓意,用户提供的元素必须能够被拷贝
vector<myclass> v;
myclass m("hello");
v.push_back(m);
}
int main(){
test_deep_copy();
system("pause");
return EXIT_SUCCESS;
}