cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : set : find
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
set
comparison operators
set::set
set::~set
member functions:
· set::begin
· set::clear
· set::count
· set::empty
· set::end
· set::equal_range
· set::erase
· set::find
· set::get_allocator
· set::insert
· set::key_comp
· set::lower_bound
· set::max_size
· set::operator=
· set::rbegin
· set::rend
· set::size
· set::swap
· set::upper_bound
· set::value_comp

-

set::find public member function
iterator find ( const key_type& x ) const;

Get iterator to element

Searches the container for an element with a value of x and returns an iterator to it if found, otherwise it returns an iterator to set::end (the element past the end of the container).

Parameters

x
Value to be searched for.
key_type is a member type defined in set containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.

Return value

An iterator to the element, if the specified value is found, or set::end if the specified value is not found in the container.
iterator is a member type, defined in set as a bidirectional iterator type.

Example

// set::find
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50

  it=myset.find(20);
  myset.erase (it);
  myset.erase (myset.find(40));

  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); it++)
    cout << " " << *it;
  cout << endl;

  return 0;
}

Output:

myset contains: 10 30 50

Complexity

Logarithmic in size.

See also

set::count Count elements with a specific key (public member function)
set::lower_bound Return iterator to lower bound (public member function)
set::upper_bound Return iterator to upper bound (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us