Results 1 to 6 of 6

Thread: Implement the equal_range() in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    26

    Implement the equal_range() in C++

    Hello, I am the student of the Bsc-IT. I had learned the C++ language in first year of Bsc-IT. I can know about the functions in C++ language. I have to submit the assignment on the equal_range() function. So, I would like to know about the equal_range() function. I would also like to know how can I Implement the equal_range() function in C++ language. If there is anyone who can have the solution on the equal_range() functions then reply me!!!
    Last edited by Garrick; 15-02-2010 at 06:36 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    The equal_range()

    Hi, The equal_range() function can be the member of the <algorithm> header file. The following can be the syntax of the equal_range() function in the C++ language :
    Code:
    template <class ForwrdItrtr, class S>
      pair<ForwrdItrtr,ForwrdItrtr>
        equal_range ( ForwrdItrtr first, ForwrdItrtr last, const S& value );

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Implement the equal_range() in C++

    The following code of lines can demonstrates you about how can you implement the equal_range() function in the C++ language :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    bool mgretr (int k,int l) 
    { 
    return (k>l); 
    }
    int main ()
     {
      int mint[] = {10,20,30,30,20,10,10,20};
      vector<int> s(mint,mint+8);    
      pair<vector<int>::iterator,vector<int>::iterator> bounds;
      sort (s.begin(), s.end());                    
      bounds=equal_range (s.begin(), s.end(), 20); 
      sort (s.begin(), s.end(), mgretr);   
      bounds=equal_range (s.begin(), s.end(), 20, mgretr); 
      cout << "bounds at positions " << int(bounds.first - s.begin());
      cout << " and " << int(bounds.second - s.begin()) << endl;
      return 0;
    }
    Output :
    Code:
    bounds at positions 2 and 5

  4. #4
    Join Date
    Nov 2005
    Posts
    1,323

    Implement the equal_range()

    PROGRAM :
    Code:
    int main()
    {
      int B[] = { 1, 2, 3, 3, 3, 5, 8 };
      const int M = sizeof(B) / sizeof(int);
      for (int j = 2; j <= 4; ++j) 
    {
        pair<int*, int*> rslt = equal_range(B, B + M, j);
        cout << endl;
        cout << "Searching for " << j << endl;
        cout << "  First position where " << j << " could be inserted: "
             << rslt.first - B << endl;
        cout << "  Last position where " << j << " could be inserted: "
             << rslt.second - B << endl;
        if (rslt.first < B + M)
          cout << "  *rslt.first = " << *rslt.first << endl;
        if (rslt.second < B + M)
          cout << "  *rslt.second = " << *rslt.second << endl;
      }
    }
    The output output of above can be as follows :
    Code:
    Searching for 2
      First position where 2 could be inserted: 1
      Last position where 2 could be inserted: 2
      *rslt.first = 2
      *rslt.second = 3
    Searching for 3
      First position where 3 could be inserted: 2
      Last position where 3 could be inserted: 5
      *rslt.first = 3
      *rslt.second = 5
    Searching for 4
      First position where 4 could be inserted: 5
      Last position where 4 could be inserted: 5
      *rslt.first = 5
      *rslt.second = 5
    Last edited by absolute55; 15-02-2010 at 06:49 PM.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    equal_range() in C++

    The equal_range() function can returns the bounds of the largest sub-range that can includes all of the elements of the fst element and lst element with the values that can be equivalent to the val. The comparison can be test or performed using either the comp for the second version or operator< for the first version. For example :- A value, p can be considered as similar to another, (!comp(p,q) && !comp(q,p)) or q, when (!(p<q) && !(q<p)).

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: Implement the equal_range() in C++

    Without interfering with the numbering of the values, the largest sub-range in a specified range of values into which a given value can be inserted by the Algorithm. The equal_range() function algorithm can performs a binary search on a numbered/ordered range to demonstrate where the value of the element can be inserted without interfering with the numbering of the ranges. The library of the C++ language can includes the two versions of the algorithm.

Similar Threads

  1. How to implement sum() faster in c#?
    By Luis-Fernando in forum Software Development
    Replies: 4
    Last Post: 27-02-2010, 09:25 PM
  2. How can I implement the remove() in CPP
    By Servant in forum Software Development
    Replies: 5
    Last Post: 22-02-2010, 04:18 PM
  3. Implement the copy_backward() in C++
    By Agustíne in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 09:46 PM
  4. How to implement IP Sec
    By Enriquee in forum Technology & Internet
    Replies: 5
    Last Post: 12-01-2010, 06:28 AM
  5. Want to implement differentiation in C++
    By ADEN in forum Software Development
    Replies: 1
    Last Post: 25-10-2008, 06:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,513,145.48302 seconds with 17 queries