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. C / C++ / MFC
  4. How to find the index of smallest 3 elements in an array

How to find the index of smallest 3 elements in an array

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structurestutorial
5 Posts 2 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    How can we find the index of smallest 3 elements in an array. Below code finds the index of largest 3 elements in an array.

    #include #include using namespace std;

    int main()
    {
    double arr[] = {0.2, 1.0, 0.01, 3.0, 0.002, -1.0, -20};

    priority_queue < pair > pQueue;

    for (int i = 0; i < 7; i++)
    {
    pQueue.push(pair(arr[i], i));
    }

    int k = 3; // number of indices we need

    for (int i = 0; i < k; ++i)
    {
    int ki = pQueue.top().second;
    cout << ki << " ";
    pQueue.pop();
    }

    }

    J 1 Reply Last reply
    0
    • L Lost User

      How can we find the index of smallest 3 elements in an array. Below code finds the index of largest 3 elements in an array.

      #include #include using namespace std;

      int main()
      {
      double arr[] = {0.2, 1.0, 0.01, 3.0, 0.002, -1.0, -20};

      priority_queue < pair > pQueue;

      for (int i = 0; i < 7; i++)
      {
      pQueue.push(pair(arr[i], i));
      }

      int k = 3; // number of indices we need

      for (int i = 0; i < k; ++i)
      {
      int ki = pQueue.top().second;
      cout << ki << " ";
      pQueue.pop();
      }

      }

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      See std::priority_queue - cppreference.com[^]:

      Quote:

      A user-provided Compare can be supplied to change the ordering, e.g. using std::greater would cause the smallest element to appear as the top().

      Another solution would be inverting the sign of the items pushed into the queue:

      pQueue.push(pair(-arr[i], i));

      L 1 Reply Last reply
      0
      • J Jochen Arndt

        See std::priority_queue - cppreference.com[^]:

        Quote:

        A user-provided Compare can be supplied to change the ordering, e.g. using std::greater would cause the smallest element to appear as the top().

        Another solution would be inverting the sign of the items pushed into the queue:

        pQueue.push(pair(-arr[i], i));

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Where should I put

        std::greater

        in the code.

        J 1 Reply Last reply
        0
        • L Lost User

          Where should I put

          std::greater

          in the code.

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          See the link from my answer. It contains example code showing that it must be passed as 3rd template parameter. So you have to pass also the 2nd parameter. As with any templates, T (uppercase) is a placeholder for the corresponding type which is std::pair in your case. So it must be (untested):

          priority_queue < pair, vector>, greater> > pQueue;

          L 1 Reply Last reply
          0
          • J Jochen Arndt

            See the link from my answer. It contains example code showing that it must be passed as 3rd template parameter. So you have to pass also the 2nd parameter. As with any templates, T (uppercase) is a placeholder for the corresponding type which is std::pair in your case. So it must be (untested):

            priority_queue < pair, vector>, greater> > pQueue;

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thanks it worked

            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