数模2011培训讲义.pptxVIP

  • 0
  • 0
  • 约7.56千字
  • 约 13页
  • 2022-03-20 发布于北京
  • 举报
STLSTL是一系列软件的统称,其代码从广义上分为三类:算法(algorithm)、容器(container)和迭代器(iterator),几乎所有的代码都采用了模板类和模板函数的方式,相比于传统的由函数和类组成的库,STL提供了更好的代码重用机会。容器是一种存储指定类型的值的有限集合的数据结构。顺序容器vector、list容器适配器:对容器做适当裁剪、适合于特殊用途的容器,如Stack、Queue#include stdio.h#include stackusing namespace std;typedef struct ElemType{ int x; int y;} ElemType;int main( ){ int i, n=7; ElemType t; stackElemType S; for(i=0;in;i++) { t.x = i; t.y = i*i; S.push(t); } printf(%d\n,S.size()); while (!S.empty()) { t = S.top(); S.pop(); printf (%d,%d\n,t.x,t.y); }}STL中的栈The C++ Stack is a container adapter that gives the programmer the functionality of a stack -- specifically, a FILO (first-in, last-out) data structure Stack constructors:construct a new stackempty:true if the stack has no elementspop:removes the top element of a stackpush:adds an element to the top of the stacksize:returns the number of items in the stacktop:returns the top element of the stack...#include iostream#include cstdlib#include stackusing namespace std;int main() { stackint s; s.push(1); s.pop(); s.push(10); s.push(11); cout s.top() endl; cout s.size() endl; cout s.empty() endl; return 0;} STL中的队列The C++ Queue is a container adapter that gives the programmer a FIFO (first-in, first-out) data structure. Queue constructor:construct a new queueback:returns a reference to last element of a queueempty:true if the queue has no elementsfront:returns a reference to the first element of a queuepop:removes the top element of a queuepush:adds an element to the end of the queuesize:returns the number of items in the queue...#include iostream#include cstdlib#include queueusing namespace std;int main() { queueint q; q.push(1); q.push(2); q.push(3); cout q.front() endl; cout q.back() endl; cout q.size() endl; cout q.empty() endl; return 0;} STL中的双端队列dequeSTL提供了双端队列(Double-ended Queues )Double-ended queues are like vectors, except that they allow fast insertions and deletions at the beginning (as well as the end) of the container.../icarnegie//cpp

文档评论(0)

1亿VIP精品文档

相关文档