다음을 통해 공유


Queue.Peek 메서드

정의

Queue의 시작 부분에서 개체를 제거하지 않고 반환합니다.

public:
 virtual System::Object ^ Peek();
public virtual object Peek ();
public virtual object? Peek ();
abstract member Peek : unit -> obj
override this.Peek : unit -> obj
Public Overridable Function Peek () As Object

반환

Queue의 처음에 있는 개체입니다.

예외

Queue가 비어 있는 경우

예제

다음 예제에서는 에 요소를 Queue추가하거나, 에서 Queue요소를 제거하거나, 의 Queue시작 부분에 있는 요소를 보는 방법을 보여줍니다.

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "The" );
   myQ->Enqueue( "quick" );
   myQ->Enqueue( "brown" );
   myQ->Enqueue( "fox" );
   
   // Displays the Queue.
   Console::Write( "Queue values:" );
   PrintValues( myQ );
   
   // Removes an element from the Queue.
   Console::WriteLine( "(Dequeue)\t{0}", myQ->Dequeue() );
   
   // Displays the Queue.
   Console::Write( "Queue values:" );
   PrintValues( myQ );
   
   // Removes another element from the Queue.
   Console::WriteLine( "(Dequeue)\t{0}", myQ->Dequeue() );
   
   // Displays the Queue.
   Console::Write( "Queue values:" );
   PrintValues( myQ );
   
   // Views the first element in the Queue but does not remove it.
   Console::WriteLine( "(Peek)   \t{0}", myQ->Peek() );
   
   // Displays the Queue.
   Console::Write( "Queue values:" );
   PrintValues( myQ );
}

void PrintValues( IEnumerable^ myCollection )
{
   IEnumerator^ myEnum = myCollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 Queue values:    The    quick    brown    fox
 (Dequeue)       The
 Queue values:    quick    brown    fox
 (Dequeue)       quick
 Queue values:    brown    fox
 (Peek)          brown
 Queue values:    brown    fox

 */
using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes a new Queue.
      Queue myQ = new Queue();
      myQ.Enqueue( "The" );
      myQ.Enqueue( "quick" );
      myQ.Enqueue( "brown" );
      myQ.Enqueue( "fox" );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ );

      // Removes an element from the Queue.
      Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ );

      // Removes another element from the Queue.
      Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ );

      // Views the first element in the Queue but does not remove it.
      Console.WriteLine( "(Peek)   \t{0}", myQ.Peek() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ );
   }

   public static void PrintValues( IEnumerable myCollection )  {
      foreach ( Object obj in myCollection )
         Console.Write( "    {0}", obj );
      Console.WriteLine();
   }
}
/*
This code produces the following output.

Queue values:    The    quick    brown    fox
(Dequeue)       The
Queue values:    quick    brown    fox
(Dequeue)       quick
Queue values:    brown    fox
(Peek)          brown
Queue values:    brown    fox

*/
Imports System.Collections

Public Class SamplesQueue
    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("The")
        myQ.Enqueue("quick")
        myQ.Enqueue("brown")
        myQ.Enqueue("fox")
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ)
        
        ' Removes an element from the Queue.
        Console.WriteLine("(Dequeue)    {0}", myQ.Dequeue())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ)
        
        ' Removes another element from the Queue.
        Console.WriteLine("(Dequeue)    {0}", myQ.Dequeue())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ)
        
        ' Views the first element in the Queue but does not remove it.
        Console.WriteLine("(Peek)       {0}", myQ.Peek())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ)
    End Sub

    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' Queue values:    The    quick    brown    fox
' (Dequeue)    The
' Queue values:    quick    brown    fox
' (Dequeue)    quick
' Queue values:    brown    fox
' (Peek)       brown
' Queue values:    brown    fox

설명

이 메서드는 Dequeue 메서드와 유사하지만 PeekQueue를 수정하지 않습니다.

nullQueue 값으로 에 추가할 수 있습니다. null 값과 의 Queue끝을 구분하려면 속성을 검사 Count 또는 가 비어 있을 때 Queue throw되는 를 catchInvalidOperationException합니다.

이 메서드는 작업입니다 O(1) .

적용 대상

추가 정보