①vector对于头部的插入删除数据低,数据量越大,效率越低。
②deque相对而言,对头部的插入删除速度比vector快。
③vector访问元素时的速度会比deque快,和两者内部实现有关。
deque
d1; //构造d1 deque
d2(d1.begin(),d1.end()); //把d1开始到结尾构造给d2 deque
d3(10,100); //将10个100放入d3 deque
d4=d3; //将d3赋值给d4
- #include
- #include
-
- void printDeque(const deque<int>& d)
- {
- for (deque<int>::const_iterator it = d.begin(); it != d.end();it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
-
-
- void text01()
- {
- deque<int> d1;
- for (int i = 0; i < 10;i++)
- {
- d1.push_back(i);
- }
- printDeque(d1);
-
- deque<int> d2(d1.begin(), d1.end());
- printDeque(d2);
-
- deque<int> d3(10, 100);
- printDeque(d3);
-
- deque<int> d4 = d3;
- printDeque(d4);
- }
deque
d2; d2 = d1; deque
d3; d3.assign(d1.begin(), d1.end()); deque
d4; d4.assign(10, 100);
- #include
- #include
-
- void printDeque(const deque<int>& d)
- {
- for (deque<int>::const_iterator it = d.begin(); it != d.end();it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- void text01()
- {
- deque<int> d1;
- for (int i = 0; i < 10;i++)
- {
- d1.push_back(i);
- }
- printDeque(d1);
-
- deque<int> d2;
- d2 = d1;
- printDeque(d2);
-
- deque<int> d3;
- d3.assign(d1.begin(), d1.end());
- printDeque(d3);
- }
empty() //判断容器是否为空,返回值=1时为空
capacity() //容器的容量
size() //返回容器中元素的个数
resize(int num) //重新指定容器的长度为num,
//若容器边长,则默认值填充,变短则超出部分被删除
resize(int num,elem) //重新指定容器的长度为num,
//若容器边长,则以elem值填充新位置,若变短,则末尾超出位置被删除
总结:
判断元素是否为空:empty 返回元素的个数:size
返回容器容量:capacity 重新指定大小:resize
- void text01()
- {
- deque<int> d1;
- for (int i = 0; i < 10; i++)
- {
- d1.push_back(i);
- }
- printDeque(d1);
-
- if (d1.empty())
- {
- cout << "d1为空!" << endl;
- }
- else
- {
- cout << "d1不为空!" << endl;
- cout << "d1的大小为:" << d1.size() << endl;
- }
-
- d1.resize(15, 1);
- printDeque(d1);
-
- d1.resize(5);
- printDeque(d1);
- }
deque
d; d.push_back(10); //尾插
d.push_front(10); //头插
d.pop_back(); //尾删
d.pop_front(); //头删
d.insert(d.begin(), 2, 10000); //在开头插入两个10000
d2.insert(d.begin(), d2.begin(), d2.end());
d.erase(d.begin()); //删除第一个
d.erase(d.begin(), d.end()); //删除所有
d.clear();
总结:
尾插:push_back 尾删:pop_back
头插:push_front 头删:pop_front
- void text01()
- {
- deque<int> d;
-
- d.push_back(10); //尾插
- d.push_back(20);
-
- d.push_front(10); //头插
- d.push_front(20);
- printDeque(d);
-
- d.pop_back(); //尾删
- d.pop_front(); //头删
- printDeque(d);
- }
-
- void text02()
- {
- deque<int> d;
- d.push_back(10);
- d.push_back(20);
- d.push_front(100);
- d.push_front(200);
- printDeque(d);
-
- d.insert(d.begin(), 2, 10000);
- printDeque(d);
-
- d.insert(d.begin(), 2, 1000);
- printDeque(d);
-
- deque<int> d2;
- d2.push_back(1);
- d2.push_back(2);
- d2.push_back(3);
-
- d2.insert(d.begin(), d2.begin(), d2.end());
- printDeque(d);
- }
-
- void text03()
- {
- deque<int> d;
- d.push_back(10);
- d.push_back(20);
- d.push_front(100);
- d.push_front(200);
- printDeque(d);
-
- d.erase(d.begin());
- printDeque(d);
-
- d.clear();
- printDeque(d);
- }
at(int idx); //返回索引idx所指的数据
operator [ ]; //返回索引idx所指的数据
front(); //返回容器中第一个数据元素
back(); //返回容器中最后一个数据元素
- void text01()
- {
- deque<int> d;
-
- d.push_back(10); //尾插
- d.push_back(20);
- d.push_front(100);
- d.push_front(200);
-
- for (int i = 0; i < d.size();i++)
- {
- cout << d[i] << " ";
- }
- cout << endl;
-
- for (int i = 0; i < d.size();i++)
- {
- cout << d.at(i) << " ";
- }
- cout << endl;
-
- cout << d.front() << endl;
- cout << d.back() << endl;
- }
sort(d.begin(), d.end()); //排序
- void text01()
- {
- deque<int> d;
-
- d.push_back(10); //尾插
- d.push_back(20);
- d.push_front(100);
- d.push_front(200);
-
- printDeque(d);
-
- sort(d.begin(), d.end());
- printDeque(d);
- }
- #include
- #include
- #include
- #include
-
- 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
&v) - {
- string nameSeed = "ABCDE";
- for (int i = 0; i < 5;i++)
- {
- string name = "选手";
- name += nameSeed[i];
-
- int score = 0;
-
- Person p(name, score);
- v.push_back(p);
- }
- }
-
- void SetScore(vector
&v) - {
- for (vector
::iterator it = v.begin(); it != v.end();it++) - {
- deque<int> d;
- for (int i = 0; i < 10;i++)
- {
- int score = rand() % 41 + 60;
- d.push_back(score);
- }
-
- //排序
- sort(d.begin(), d.end());
-
- //去掉最高分和最低分
- d.pop_back();
- d.pop_front();
-
- int sum = 0;
- for (deque<int>::iterator dit = d.begin(); dit != d.end();dit++)
- {
- sum += *dit;
- }
-
- int avg = sum / d.size();
-
- it->m_Score = avg;
- }
- }
-
- void showScore(vector
&v) - {
- for (vector
::iterator it = v.begin(); it != v.end();it++) - {
- cout << "姓名:" << it->m_Name << " 平均分:" << it->m_Score << endl;
- }
- }
-
- int main()
- {
- srand((unsigned int)time(NULL));
-
- vector
v; - createPerson(v);
- SetScore(v);
- showScore(v);
-
- system("pause");
- return 0;
- }