Searching in STL array Container - C++ STL

C++ examples for STL:array

Description

Searching in STL array Container

Demo Code

#include <iostream> 
#include <array> 
#include <vector> 
#include <ios> 


using namespace std;

int main(int argc, char *argv[])
{

  const unsigned int ARRAY_SIZE = 5;

  using MyArray = std::array<int, ARRAY_SIZE>;
  MyArray myArray = { 0, 1, 2, 3, 4 };/*from   ww w .j a v a2s  . c o  m*/

  for (unsigned int i = 0; i < ARRAY_SIZE; ++i)
  {
    if (myArray[i] == 2)
    {
      cout << "Found: " << myArray[i]
        << " at position: " << i << endl;
    }
  }

  for (auto iter = myArray.begin(); iter != myArray.end(); ++iter)
  {
    if (*iter == 2)
    {
      cout << "Found: " << *iter << endl;
    }
  }

  for (auto& number : myArray)
  {
    if (number == 2)
    {
      cout << "Found : " << number << endl;
    }
  }

  MyArray::iterator found = find(myArray.begin(), myArray.end(), 2);
  if (found != myArray.end())
  {
    cout << "Found : " << *found << endl;
  }
  return 0;
}

Related Tutorials