deque::operator[] 和 deque::at

在 Visual C++ 演示如何使用 、向量、双端队列:: operator[]、向量、双端队列:: 在 标准 (STL)模板库函数。

const_reference operator[](
   size_type Pos
) const;
reference operator[](
   size_type Pos
);
const_reference operator[](
   difference_type _N
) const;
reference operator[](
   difference_type _N
) const;
const_reference at(
   size_type Pos
) const;
reference at(
   size_type Pos
); bool empty( ) const;

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

operator[] 成员函数返回对控件序列的元素在位置 Pos。 如果该位置无效,该行为不确定。 成员函数返回对控件序列的元素在位置 Pos。 如果该位置无效,该函数引发类 out_of_range对象。 null 成员函数返回一个空控件序列的 true

示例

// operator.cpp
// compile with: /EHsc
//
// Functions:
//    operator[]
//    at
//    empty
//    push_back
//    begin
//    end

#include <iostream>
#include <deque>

using namespace std;


typedef deque<char >  CHARDEQUE;
void print_contents (CHARDEQUE  deque, char*);

int main()
{
    //create an empty deque a
    CHARDEQUE  a;

    //check whether it is empty
    if(a.empty())
        cout<<"a is empty"<<endl;
    else
        cout<<"a is not empty"<<endl;

    //inset A, B, C and D  to a
    a.push_back('A');
    a.push_back('B');
    a.push_back('C');
    a.push_back('D');

    //check again whether a is empty
    if(a.empty())
        cout<<"a is empty"<<endl;
    else
        cout<<"a is not empty"<<endl;

    //print out the contents

    print_contents (a,"a");

    cout <<"The first element of a is " <<a[0] <<endl;
    cout <<"The first element of a is " <<a.at(0) <<endl;

    cout <<"The last element of a is " <<a[a.size()-1] <<endl;
    cout <<"The last element of a is " <<a.at(a.size()-1) <<endl;
}

//function to print the contents of deque
void print_contents (CHARDEQUE  deque, char *name)
{
    CHARDEQUE::iterator pdeque;

    cout <<"The contents of "<< name <<" :";

    for(pdeque = deque.begin();
        pdeque != deque.end();
        pdeque++)
        {
            cout <<" "  << *pdeque;
        }
    cout<<endl;
}
  

要求

标题: AMP_LT dequeAMP_GT

请参见

概念

标准模板库示例