Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Finding index of an array element??

Finding index of an array element??

Scheduled Pinned Locked Moved ATL / WTL / STL
databasedata-structuresquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raza5680
    wrote on last edited by
    #1

    Hi, Is there any function that returns an index of a particular element in a numeric array. Thanks P.S. I am looking for an equivalent of MATLAB's find() function.

    N J 2 Replies Last reply
    0
    • R Raza5680

      Hi, Is there any function that returns an index of a particular element in a numeric array. Thanks P.S. I am looking for an equivalent of MATLAB's find() function.

      N Offline
      N Offline
      Nuri Ismail
      wrote on last edited by
      #2

      You can use a combination of std::find[^] and std::distance[^] in order to search for an element and get its index. The standard algorithms will work both on standard containers and arrays. Here is a quick example for you, which demonstrates the usage of std::find and std::distance with std::vector and regular array:

          // Prepare the test array
      int arr\[\] = {31, 53, 7, 9, 45, 20};
      const int size = (sizeof(arr) / sizeof(arr\[0\]));
      int\* arr\_beg = arr;
      int\* arr\_end = arr + size;
      
      // Prepare the test vector
      std::vector<int> vec;
      std::copy(arr, arr + size, std::back\_inserter(vec));
      
      // Search the array
      std::cout << "\\n Searching the array for element with value 7...";
      int\* res1 = std::find(arr, arr + size, 7);
      if(res1 != arr\_end)
          std::cout << "\\n The element is found at index: " << std::distance(arr\_beg, res1);
      else
          std::cout << "\\n The element is not found";
      
      std::cout << "\\n Searching the array for element with value 8...";
      int\* res2 = std::find(arr, arr + size, 8);
      if(res2 != arr\_end)
              std::cout << "\\n The element is found at index: " << std::distance(arr\_beg, res2);
      else
          std::cout << "\\n The element is not found";
      
      
      // Search the vector
      std::cout << "\\n Searching the vector for element with value 7...";
      std::vector<int>::iterator it1 = std::find(vec.begin(), vec.end(), 7);
      if(it1 != vec.end())
          std::cout << "\\n The element is found at index: " << std::distance(vec.begin(), it1);
      else
          std::cout << "\\n The element is not found";
      
      std::cout << "\\n Searching the vector for element with value 8...";
      std::vector<int>::iterator it2 = std::find(vec.begin(), vec.end(), 8);
      if(it2 != vec.end())
          std::cout << "\\n The element is found at index: " << std::distance(vec.begin(), it2);
      else
          std::cout << "\\n The element is not found";
      

      I hope this helps. :)

      1 Reply Last reply
      0
      • R Raza5680

        Hi, Is there any function that returns an index of a particular element in a numeric array. Thanks P.S. I am looking for an equivalent of MATLAB's find() function.

        J Offline
        J Offline
        jk chan
        wrote on last edited by
        #3

        you can use std::find function.you need to include algorithm header file for example if you have a vector containing some integers you can call it like this vector::iterator the_index = std::find( vcNumbers.begin(),vcNumbers.end(), number_to_search ); you can check if the index is valid by checking it against vcNumers.end().Something like this. if( vcNumbers.end() != the_index) cout << *it; hopes this helps :)

        If u can Dream... U can do it

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups