deque容器
功能:双端数组,可以对头端举行插入删除使用
deque和vector区别:vector对于头部插入删除服从低,数据量越大 服从越低
deque相对而言,对头部的插入删除速率回比vector快
vector访问元素时速率比deque快
构造方式和vector根本同等,而且插入和删除提供的位置是迭代器
排序 使用算法对deque举行排序- void test01()
- {
- deque<int>d1;
- d1.push_back(2);
- d1.push_back(1);
- d1.push_back(6);
- d1.push_back(5);
- printinfo(d1);
- //默认从小到大 升序
- sort(d1.begin(), d1.end());
- printinfo(d1);
- }
复制代码 stack栈容器
stack是一种先辈后出(First in last out,FILO)的数据结构,只有一个出口 。
栈中只有顶端元素才可以被外界使用,因此栈不允许有遍历使用。
栈可以使用empty判断容器是否为空。
栈可以用size返回元素个数。
入栈push 出栈pop。
- stack常用构造 stack<T> stk; stack(const stack&stk)
- stack & operator=(const stack&stk )
- push()向栈顶添加元素
- pop移除栈顶元素
- top()返回栈顶元素
- size()栈的大小
- empty 是否为空
复制代码 须要注意的是,假如想知道栈内里的元素,必须要一个一个拿出来才气知道。这种使用不能叫做遍历,由于遍历优劣质变算法。那么怎样查察栈的元素呢。只要栈不为空就查察栈顶 而且实行出栈使用。- void test01()
- {
- stack<int>stk;
- stk.push(10);
- stk.push(30);
- stk.push(2);
- //看栈中含有的元素不叫遍历,需要先拿出去
- //只要栈不为空就查看栈顶 并且执行出栈操作
- while (!stk.empty())
- {
- cout << stk.top() << endl;
- stk.pop();
- }
- cout << stk.size() << endl;
- }
复制代码 queue容器
队列是一种先辈先出的数据结构。有两个出口,一端新增数据 、一端移除数据。
只有对头和队尾才可以被外界使用,因此队列也不允许有遍历活动。
进入队列中数据称为-入队push() 出队pop()从对头移除。- empty size
- back()返回最后一个元素
- front()返回第一个元素
复制代码 list容器
链表是一种物理存储单位上非连续存储结构,数据元素的逻辑序次是通过链表中的指针链接实现的 。作用:将数据举行链式存储。
链表构成:链表由一系列结点构成。
结点构成:一个是存储数据元素的数据域,另一个是存储下一个结点所在的指针域。
STL中链表是一个双向循环链表。下图是链表的结构
优点:可以对恣意位置举行快速插入大概删除元素。
缺点:容器的遍历速率没有数组快 占用的空间比力大。
链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。
STL中list和vector是最常使用的容器。
list有一个紧张性子:插入和删除使用都不会造成原有迭代器失效,这在vector是不创建的。
构造、赋值、巨细使用、插入和删除同vector ,多了一个remove(elem)删除全部与elem匹配的元素,而且取元素不支持[] 和 at使用 ,取front 和 back.
反转和排序 reverse()反转链表 sort()链表排序 。 下面展示一段sort对链表举行排序的代码- void printinfo(const list<int>& L)
- {
- for(list<int>::iterator it = L.begin() ; it != L.end() ; it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
- void test01()
- {
- list<int>L1;
- L1.push_back(1);
- L1.push_back(4);
- L1.push_back(5);
- L1.push_back(2);
- printinfo(L1);
-
- //注意这边使用的sort不是标准算法,而是自带的成员函数
- L1.sort();//默认使用的是升序排列
- printinfo(L1);
- }
- //如果要使用降序排序 需要写一个仿函数
- bool compare(int a , int b )
- {
- return a>b;
- }
- void test02()
- {
- list<int>L1;
- L1.push_back(1);
- L1.push_back(4);
- L1.push_back(5);
- L1.push_back(2);
- printinfo(L1);
- L1.sort(compare);//便可使用降序
- printinfo(L1);
- }
复制代码 排序案例
将Person自界说数据范例举行排序 ,Person中属性有姓名年事身高
排序规则:按照年事举行升序,假如年事雷同按照身高举行降序- class Person
- {
- public:
- Person(string name , int age , int height)
- {
- this->m_name = name;
- this->m_age = age;
- this->m_height = height;
- }
- string m_name;
- int m_age;
- int m_height;
- };
- void printinfo(list<Person>&L)
- {
- for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
- {
- cout << "姓名:" << (*it).m_name << " 年龄:" << (*it).m_age
- << " 身高:" << (*it).m_height << endl;
- }
- cout << endl;
- }
- bool compare(Person &p1 , Person &p2)
- {
- if(p1.m_age != p2.m_age)
- {
- return p1.m_age < p2.m_age ; // 升序
- }
- else
- {
- return p1.m_height > p2.m_height;
- }
- }
- void test01()
- {
- Person p1("zhangsan", 15 , 150);
- Person p2("lisn", 16, 176);
- Person p3("wangzha", 15, 173);
- Person p4("wanwu", 18, 190);
- list<Person>l;
- l.push_back(p1);
- l.push_back(p2);
- l.push_back(p3);
- l.push_back(p4);
- printinfo(l);
- //按照年龄升序 , 这边如果使用自定义的数据类型,必须要指定排序规则
- l.sort(compare);
- printinfo(l);
- }
复制代码 评委打分案例
5名选手ABCDE 10个评委打分 去除最高分 去除最低分 取匀称分
5名选手放入vector
遍历vector 取出选手 实行for循环 把10个评分存到deque中
sort举行排序 去除deque最高和最低分
deque遍历一遍 , 累加总分- class Person
- {
- public:
- Person(string name, int score )
- {
- this->m_name = name;
- this->m_score = score;
- }
- string m_name;
- int m_score;
- };
- void createPerson(vector<Person> & V)
- {
- string seedname = "ABCDE";
- for(int i = 0 ; i< 5 ; i++)
- {
- string name = "选手" ;
- name += seedname[i];
- int score = 0;
- Person p(name , score);
- v.push_back(p);
- }
- }
- void setscore(vector<Person> &v)
- {
- for(vector<Person>::iterator it = v.begin() ; it != v.end() ; it++)
- {
- deque<int>d;
- for(int i = 0 ; i < 10 ; i++ )
- {
- sco = rand()%41 + 60;
- d.push_back(score);
- }
- cout << "选手:" << it->m_name << " 得分:"<< endl;
-
- sort(d.begin() , d.end());
- d.pop_back();
- d.pop_front();
- int num = 0;
- for(deque<int>::iterator it = d.begin() ; it != d.end() ; it ++)
- {
- num += (*it);
- }
- it->m_score = num / d.size();
- }
- }
- void printinfo(vector<Person> &V)
- {
- for(vector<Person>::iterator it = V.begin() ; it != V.end() ; it++)
- {
- cout << "姓名:" << it->m_name << " 平均分:" << it->m_score << endl;
- }
- }
- int main()
- {
- //随机时间
- srand((unsigned int)time(NULL));
- vector<Person>v;
- createPerson(v);
- setscore(v);
- printinfo(v);
- system("pause");
- return 0 ;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |