deque::operator 및 deque::at
사용 하는 방법을 보여 줍니다 있는 deque::operator 및 deque::at Visual C++에서 표준 템플릿 라이브러리 (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;
설명
[!참고]
프로토타입에 클래스/매개 변수 이름은 헤더 파일에서 버전이 일치 하지 않습니다.일부 가독성을 높이기 위해 수정 되었습니다.
해당 연산자 멤버 함수 위치에서 제어 되는 시퀀스의 요소에 대 한 참조를 반환 합니다. Pos.해당 위치가 잘못 된 경우 동작이 정의 되지 않습니다.에서 멤버 함수 위치에서 제어 되는 시퀀스의 요소에 대 한 참조를 반환 합니다. Pos.해당 위치가 잘못 된 경우 함수는 클래스의 개체를 throw 됩니다 out_of_range.해당 빈 멤버 함수 반환 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;
}
요구 사항
헤더: < 있지 않은 deque >