1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C++stl 向量 链表 栈 队列(vector list stack queue)

C++stl 向量 链表 栈 队列(vector list stack queue)

时间:2024-02-20 10:22:15

相关推荐

C++stl 向量 链表 栈 队列(vector  list  stack  queue)

随机存取的向量-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 intsvector<int> second(4, 100); // four ints with value 100vector<int> third(second.begin(), second.end()); // iterating through secondvector<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';// sortsort(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';// descendsort(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 intsstd::list<int> second(4, 100); // four ints with value 100std::list<int> third(second.begin(), second.end()); // iterating through secondstd::list<int> fourth(third); // a copy of thirddouble 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.35mylist.unique(); // 2.72, 3.14, 12.15, 12.77// 15.3, 72.25, 73.0, 73.35mylist.unique(same_integral_part); // 2.72, 3.14, 12.15// 15.3, 72.25, 73.0mylist.unique(is_near()); // 2.72, 12.15, 72.25std::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 5it = 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() //返回队列的元素个数

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。