std::array
#include <iostream> #include <array>
int main() { std::array<int, 5> myArray{1, 2, 3, 4, 5}; std::cout<<myArray.at(1)<<std::endl; //reference at ( size_type n ); 返回的值是一个左值. std::cout<<myArray[0]<<std::endl; //reference operator[] (size_type n); 返回的值是一个左值. myArray.fill(0); //把array中的5个元素都填充为0; std::cout<<myArray[0]<<std::endl; std::cout<<myArray.front()<<std::endl; // reference front(); 返回的仍然是一个左值. std::cout<<myArray.back()<<std::endl; // reference back(); 返回的仍然是一个左值. std::cout<<myArray.size()<<std::endl;// 获得当前array中元素的数量. std::cout<<myArray.max_size()<<std::endl; //获得当前array最多容纳多少个元素的数量. return 0; }
std::deque(双端队列)
#include <deque> #include <iostream>
int main() { // constructors used in the same order as described above: std::deque<int> first; // empty deque of ints std::deque<int> second (4,100); // four ints with value 100 std::deque<int> third (second.begin(),second.end()); // iterating through second std::deque<int> fourth (third); first.assign({1, 2, 3, 4}); //void assign (initializer_list<value_type> il); first.assign(second.begin(), second.end()); //void assign (InputIterator first, InputIterator last); first.assign(5, 10); //void assign (size_type n, const value_type& val); std::cout<<"size(): "<<first.size()<<std::endl; //size_type size(); std::cout<<"max_size(): "<<first.max_size()<<std::endl; // size_type max_size(); first.shrink_to_fit(); //减少deque的内存使得其与其中存储的元素个数相对应. std::cout<<first.at(1)<<std::endl; //reference at(size_type n); std::cout<<first.front()<<std::endl; //返回双端队列第一个元素的的引用是一个左值. std::cout<<first.back()<<std::endl; //返回双端队列最后一个元素的引用是一个左值. std::deque<int>::iterator iter = first.begin(); std::deque<int>::iterator it = first.emplace(iter, 1); //默认插入元素1到first.begin()前面. std::cout<<first[0]<<std::endl; //下标操作随机访问任意一个元素. first.emplace_front(111); //在双端队列首创建元素111作为其首元素. first.emplace_back(222); //在尾部创建元素222作为其尾部元素. first.resize(100); //设置deque的尺寸为可以承载100个元素. 假如其中已有了10个元素,那么就再创建90个且这90都默认初始化为0; first.push_back(333); //在deque放入元素333作为尾元素. first.push_front(444); //在deque的头部放入元素444做为首元素. first.pop_front(); //删除首元素. first.pop_back(); //删除尾元素. return 0; }
std::forward_list(单向向前链表)
#include <iostream> #include <forward_list> //单向向前链表. #include <typeinfo> //获得类型的字符串. #include <functional> bool funct(const int& number, const int& flag)//用于std::bind { return number > flag ? true : false; } bool getTruth(const int& number) //用于std::function { return number == 0 ? true : false; } int main() { // constructors used in the same order as described above: std::forward_list<int> first; // default: empty std::forward_list<int> second (3,77); // fill: 3 seventy-sevens std::forward_list<int> third (second.begin(), second.end()); // range initialization std::forward_list<int> fourth (third); // copy constructor std::forward_list<int> fifth (std::move(fourth)); // move ctor. (fourth wasted) std::forward_list<int> sixth = {3, 52, 25, 90}; std::forward_list<int> seventh{1, 2, 3, 4}; //initializer_list //分配操作 assign() first.assign(4, 10); //创建4个10到链表内. first.assign(second.begin(), second.end()); //在assign中创建second.begin()到second.end()(左闭右开区间)范围内的元素. first.assign({1, 2, 3}); //利用initializer_list 创建元素. //insert_after() first.insert_after(first.before_begin(), 11); //把11放到链表的头部做为首元素. //begin() std::forward_list<int>::iterator iter = first.begin(); std::cout<<*iter<<std::endl; //emplace_after(); 在指定的iterator后创建一个新元素. 返回指向新创建元素的iterator. //其中emplace与insert还是不同的,详细见下面例子. std::forward_list< std::pair<int,char> > mylist; auto it = mylist.before_begin(); it = mylist.emplace_after ( it, 100, 'x' ); //注意这里100和x被自动转换为一个std::pair; iter = first.emplace_after(first.begin(), 100); //在链表的首元素后创建一个新元素做为第二个元素.返回新放入的元素的iterator. std::cout<<*iter<<std::endl; //void emplace_front(); //在指定iterator前面创建一个新元素,返回指向新元素的iterator. //其不同于insert详细见下. std::forward_list< std::pair<int,char> > myList; myList.emplace_front(10,'a'); myList.emplace_front(20,'b'); myList.emplace_front(30,'c'); //mylist contains: (30,c) (20,b) (10,a).注意各个std::pair的顺序. first.emplace_front(111);//在链表的头部创建一个新元素111. 注意emplace_front()操作是没有返回类型的. std::cout<<*iter<<std::endl; //iterator biegin(); //iterator erase_after(); std::forward_list<int> myli{10, 20, 30, 40, 50}; // 10 20 30 40 50 auto its = myli.begin(); its = myli.erase_after(its); // 10 30 40 50 its = myli.erase_after(its, myli.end()); // 10 30 //reference front(); //注意返回的是forward_list中首元素的引用. first.front() = 222; //注意由于是单向链表因此只有front操作.这样才符合forward嘛. std::cout<< first.front() <<std::endl; //iterator insert_after(); iter = first.insert_after(first.begin(), 333); //在first首元素的后面插入元素222. std::cout<<*iter<<std::endl; //size_type max_size(); //返回forward_list中最多能装的元素的个数. std::cout<<first.max_size()<<std::endl; /*std::forward_list<double> first = {4.2, 2.9, 3.1}; std::forward_list<double> second = {1.4, 7.7, 3.1}; std::forward_list<double> third = {6.2, 3.7, 7.1}; first.sort(); second.sort(); first.merge(second); std::cout << "first contains:"; for (double& x: first) std::cout << " " << x; std::cout << std::endl; first.sort (std::greater<double>()); third.sort (std::greater<double>()); first.merge (third, std::greater<double>()); std::cout << "first contains:"; for (double& x: first) std::cout << " " << x; std::cout << std::endl;*/ //void remove (const value_type& val); first.remove(222); //移除指定内容.注意参数不是iterator. //template <class Predicate> //void remove_if (Predicate pred); //remove_if()接受一元谓词. auto preid = std::bind(funct, std::placeholders::_1, 2); //bind. std::cout<<typeid(preid).name()<<std::endl; std::function<bool (const int&)> judge = getTruth; first.remove_if(preid); //std::bind first.remove_if([](const int& temporary)->bool { temporary == 0 ? true : false; });//lambda表达式. first.remove_if(judge); //std::function; first.remove_if(getTruth); //直接使用接受一个参数的函数. 也可以重载一个类的operator()(const int& number); // void resize (size_type n); // void resize (size_type n, const value_type& val); first.resize(10); //如果first中没有10个元素.假如此时first中只有4个元素那么剩下6个元素就被默认初始化为0,如果first中有超过10个元素那么超过的部分就被删除掉. first.resize(10, 333); //把first中的元素设为10个,假如此时first中有3个元素那么就再创建7个元素333. // void reverse() noexcept; first.reverse(); //反转first中的元素. // void sort(); // template <class Compare> // void sort (Compare comp); first.sort(); //默认以从大到小来给元素进行排序, 可以传递一个函数进去. //merge(other forward_list),在合并之前必须对这两个单向链表进行排序. first.sort(); second.sort(); first.merge(second); //splice_after(); 假如把first 拼接到 second那么frist拼接后为空. /*std::forward_list<int> first = { 1, 2, 3 }; std::forward_list<int> second = { 10, 20, 30 }; auto it = first.begin(); // points to the 1 first.splice_after ( first.before_begin(), second ); // first: 10 20 30 1(it) 2 3 // second: (empty) //注意这里. // "it" still points to the 1 (now first's 4th element) second.splice_after ( second.before_begin(), first, first.begin(), it); // first: 10 1 2 3 // second: 20 30 first.splice_after ( first.before_begin(), second, second.begin() ); //左开右开区间. // first: 30 10 1 2 3 // second: 20 // * notice that what is moved is AFTER the iterator */ first.clear(); return 0; }
std::list(双向链表注意链表意味着不支持随机访问)
#include <iostream> #include <list> //双向链表. 其与队列的区别就是不支持operator[];
int main() { //注:括号内的function代表这是list的成员函数. std::list<int>::iterator iter; //构造函数. std::list<int> myList; //default; explicit list (const allocator_type& alloc = allocator_type()); std::list<int> first(4, 100); //这里有4个100位于first中. explicit list (size_type n); //list (size_type n, const value_type& val, const allocator_type& alloc = allocator_type()); std::list<int> second(first.begin(), second.end()); //把first中两个iterator范围内的元素(左闭右开区间)的元素用来初始化second. //template <class InputIterator> //list (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); //std::list::assign(function) myList.assign(first.begin(), first.end()); //分配两个iterator范围内的元素作为myList的元素. 如果myList内原本有元素那么会被清空. //template <class InputIterator> //void assign (InputIterator first, InputIterator last); myList.assign(4, 100); //分配4个100作为myList的现有元素.如果链表内已有元素会被覆盖. //void assign (size_type n, const value_type& val); myList.assign({1, 2, 3, 4}); //由于其参数类型为std::initializer_list<value_type> il; // void assign (initializer_list<value_type> il); //std::list::back(function) std::cout<<myList.back()<<std::endl; //返回该双向链表内的最后一个元素. //reference back(); //返回的均为左值. //const_reference back() const; //std::list::clear(function) //myList.clear(); //清除list内的所有元素. //std::list::emplace(fucntion) //std::list< std::pair<int,char> > mylist; //mylist.emplace ( mylist.begin(), 100, 'x' ); //注意这里100和'x'被自动转换为std::pair类型. //mylist.emplace ( mylist.begin(), 200, 'y' ); //返回一个iterator指向新插入的元素. //std::list::emplace_back(function) //mylist.emplace_back(100, 'x'); //mylist.emplace_back(200, 'y'); //没有返回类型. //std::list::emplace_front(function) //mylist.emplace_front(100,'x'); //在链表的头部创建一个pair作为首元素. 没有返回类型. //std::empty(function) bool b = myList.empty(); //判断myList中是不是含有元素有的话返回false.没有返回true; //std::erase(function) //返回指向被删除的元素后的第一个元素的iterator. myList.erase(myList.begin(), myList.begin()+2); myList.erase(myList.begin()); //std::list::front(function) std::cout<<myList.front()<<std::endl; //返回链表内首元素的引用 //std::get_allocator(); int* p = myList.get_allocator().allcate(1); p[1] = 0; std::cout<<*p<<std::endl; //std::insert(function); //return An iterator that points to the first of the newly inserted elements. /*std::list<int> mylist; std::list<int>::iterator it;
// set some initial values: for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5.注意这里push_back.
it = mylist.begin(); ++it; // it points now to number 2 ^
mylist.insert (it,10);//插入操作 默认插入新元素到给定迭代器的后面. 1 10 2 3 4 5
// "it" still points to number 2 ^ mylist.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); mylist.insert (it,myvector.begin(),myvector.end()); // 1 10 20 30 30 20 2 3 4 5 // ^ std::cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; */ //std::list::remove(function) //return none; myList.remove(100); //如果链表内有不止一个100那么都会被移除. //std::list::remove_if //接受一个一元谓词.
/* a predicate implemented as a function: bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class: struct is_odd { bool operator() (const int& value) { return (value%2)==1; } };
int main () { int myints[]= {15,36,7,17,20,39,4,1}; std::list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20
std::cout << "mylist contains:"; for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it) std::cout << ' ' << *it; std::cout << '\n';
return 0; } */ }