Aracılığıyla paylaş


deque::operator ve deque::at

Nasıl kullanılacağı gösterilmiştir [deque::operator] ve deque::at Visual C++ standart şablon kitaplığı (stl) işlevleri.

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;

Notlar

[!NOT]

Prototip sınıfı/parametre adları üstbilgi dosyasında sürüm eşleşmiyor.Bazıları, okumayı kolaylaştırmak için değiştirildi.

Operator [ üye işlevini verir konumundaki denetlenen sırasının öğesine başvuru Pos.Bu konum geçersizse, tanımsız bir davranıştır.En üye işlevini verir konumundaki denetlenen sırasının öğesine başvuru Pos.Bu konum geçersizse, sınıfın bir nesnesi işlevi atar out_of_range.Boş üye işlevini verir doğru boş bir denetlenen sırası.

Örnek

// 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;
}
  

Gereksinimler

Başlık: < deque >

Ayrıca bkz.

Kavramlar

Standart şablon kitaplığı örnekleri