C++stl 向量,链表,栈,队列(vector, list, stack, queue)
来源:互联网 发布:俄罗斯军粮淘宝 编辑:程序博客网 时间:2024/05/22 10:30
随机存取的向量-vector
#include <iostream>#include <vector>#include<algorithm>using namespace std;bool comp(const int &a, const int &b){ return a > b;}void VectorMain(){ // constructors used in the same order as described above: vector<int> first; // empty vector of ints vector<int> second(4, 100); // four ints with value 100 vector<int> third(second.begin(), second.end()); // iterating through second vector<int> fourth(third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = { 16,2,77,29 }; vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int)); cout << "The contents of fifth are:"; for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) cout << ' ' << *it; cout << '\n'; // sort sort(fifth.begin(), fifth.end()); cout << "The ascend contents of fifth are:"; for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) cout << ' ' << *it; cout << '\n'; // descend sort(fifth.begin(), fifth.end(), comp); cout << "The descend contents of fifth are:"; for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) cout << ' ' << *it; cout << '\n'; // push_back, pop_back, empty, clear}
链表
#include <iostream>#include <cmath>#include <vector>#include <list>using namespace std;// a binary predicate implemented as a function:bool same_integral_part(double first, double second){ return (int(first) == int(second));}// a binary predicate implemented as a class:struct is_near { bool operator() (double first, double second) { return (fabs(first - second)<5.0); }};void ListMain(){ // constructors used in the same order as described above: std::list<int> first; // empty list of ints std::list<int> second(4, 100); // four ints with value 100 std::list<int> third(second.begin(), second.end()); // iterating through second std::list<int> fourth(third); // a copy of third double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14, 12.77, 73.35, 72.25, 15.3, 72.25 }; std::list<double> mylist(mydoubles, mydoubles + 10); mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77, // 15.3, 72.25, 72.25, 73.0, 73.35 mylist.unique(); // 2.72, 3.14, 12.15, 12.77 // 15.3, 72.25, 73.0, 73.35 mylist.unique(same_integral_part); // 2.72, 3.14, 12.15 // 15.3, 72.25, 73.0 mylist.unique(is_near()); // 2.72, 12.15, 72.25 std::cout << "mylist contains:"; for (std::list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; ///////////////////////////insert///////////////////////////// std::list<int> mylist1; std::list<int>::iterator it; // set some initial values: for (int i = 1; i <= 5; ++i) mylist1.push_back(i); // 1 2 3 4 5 it = mylist1.begin(); ++it; // it points now to number 2 ^ mylist1.insert(it, 10); // 1 10 2 3 4 5 // "it" still points to number 2 ^ mylist1.insert(it, 2, 20); // 1 10 20 20 2 3 4 5 --it; // it points now to the second 20 ^ std::vector<int> myvector(2, 30); mylist1.insert(it, myvector.begin(), myvector.end()); // 1 10 20 30 30 20 2 3 4 5 // ^ std::cout << "mylist contains:"; for (it = mylist1.begin(); it != mylist1.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; // front(), back(), push_back(value), pop_back(), push_front(value), pop_front(), empty(), clear(), remove(value)}
stack:
push(value) //向容器顶部插入元素pop() //删除容器顶部的元素top() //返回容器顶部的元素size() //返回容器的元素个数empty() //检查是否为空
queue:
back() //返回队列最后一个元素的引用empty() //检查是否为空front() //返回队列第一个元素的引用push(value) //队列尾添加一个元素pop() //删除队列第一个元素size() //返回队列的元素个数
阅读全文
0 0
- C++stl 向量,链表,栈,队列(vector, list, stack, queue)
- C++ STL 栈stack 向量vector 队列queue的用法
- stl 常用函数(包括vector list stack queue)
- stl-stack/queue/list
- STL 整理map、set、vector、list、stack、queue、deque、priority_queue
- STL详解(vector,list,deque,stack,queue,priority_queue;map,set)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL库整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 收拾(map、set、vector、list、stack、queue、deque、priority_queue)...
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- POST和GET的区别!
- BZOJ2753 滑雪与时间胶囊 (洛谷P2573)
- eclipse 全局替换文件中的内容
- HTTP协议详解
- 在Rancher中添加为中国区优化的k8s应用商店的步骤和方法
- C++stl 向量,链表,栈,队列(vector, list, stack, queue)
- 【机房重构】之修改密码
- Ubuntu 16.04 LTS 为Firefox安装flash插件
- 数组
- 欧拉筛——线性筛素数和欧拉函数
- KafkaUtils.createStream接收数据流程
- CSS Mastery2(11.6-透明度&引用&圆角)
- 2017-6-29
- Python简单爬虫的实现