다음을 통해 공유


Visual C++에서 set::find STL 함수 사용

이 문서에서는 Visual C++에서 STL(표준 템플릿 라이브러리) 함수를 사용하는 set::find 방법을 보여 줍니다.

원래 제품 버전: Visual C++
원본 KB 번호: 158576

필수 헤더

<set>

프로토 타입

template<class _K, class _Pr, class _A>
class set
{
    public:
    // Function 1:

    const_iterator find(const _K& _Kv) const;
}

참고

프로토타입의 클래스/매개 변수 이름이 헤더 파일의 버전과 일치하지 않을 수 있습니다. 일부는 가독성을 개선하기 위해 수정되었습니다.

set::find 함수에 대한 설명

find 함수는 제어된 시퀀스에서 요소를 찾는 데 사용됩니다. 정렬 키가 해당 매개 변수와 일치하는 제어된 시퀀스의 첫 번째 요소에 반복기를 반환합니다. 이러한 요소가 없으면 반환된 반복기는 와 같습니다 end().

샘플 코드

참고

Visual C++ .NET 및 Visual C++에서 /EHsc 는 기본적으로 설정되며 /GX와 동일합니다.

//////////////////////////////////////////////////////////////////////
// Compile options needed: -GX
// SetFind.cpp:
//      Illustrates how to use the find function to get an iterator
//      that points to the first element in the controlled sequence
//      that has a particular sort key.
// Functions:
//    find         Returns an iterator that points to the first element
//                 in the controlled sequence that has the same sort key
//                 as the value passed to the find function. If no such
//                 element exists, the iterator equals end().
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <set>
#include <iostream>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
typedef set<int,less<int>,allocator<int> > SET_INT;

void truefalse(int x)
{
  cout << (x?"True":"False") << endl;
}
void main()
{
  SET_INT s1;
  cout << "s1.insert(5)" << endl;
  s1.insert(5);
  cout << "s1.insert(8)" << endl;
  s1.insert(8);
  cout << "s1.insert(12)" << endl;
  s1.insert(12);

  SET_INT::iterator it;
  cout << "it=find(8)" << endl;
  it=s1.find(8);
  cout << "it!=s1.end() returned ";
  truefalse(it!=s1.end());  //  True

  cout << "it=find(6)" << endl;
  it=s1.find(6);
  cout << "it!=s1.end() returned ";
  truefalse(it!=s1.end());  // False
}

프로그램 출력

s1.insert(5)
s1.insert(8)
s1.insert(12)
it=find(8)
it!=s1.end() returned True
it=find(6)
it!=s1.end() returned False