Results 1 to 6 of 6

Thread: How the sort() can be used in CPP programming

  1. #1
    Join Date
    Dec 2009
    Posts
    23

    How the sort() can be used in CPP programming

    Hi, I am the student of the BSC-IT. I could had the well knowledge of the C++ programming language. I had also created the one project in the C++ programming language. But I could not know much more about the built-in functions of C++ programming language. So, I would like to know about the sort() function of the C++ language. I also would like to know about how can I use the sort() function in the C++ programming language. Thus, Can anyone has the proper answer on the sort() function in the C++ language. Reply Me!!!

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    The sort()

    Hello, The sort() function can be used in the C++ language to Sorts the elements in the given specified range [fst,lst) into an ascending order. The following can be the general syntax of the sort(0 function in the C++ language as :
    Code:
    template <class RndmAcesItrtr>
      void sort ( RndmAcesItrtr fst, RndmAcesItrtr lst );

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

    How the sort() can be used in CPP

    The following can be the parameters of the sort() function in the C++ programming language :
    1. comp : This parameter can be used in the sort() function of the C++ language for an object of the Comparison function that can taking two values of the same type than those can be contained in the given specified range.
    2. fst, lst : These parameters can be used in the sort() function of the C++ language for Random-Access iterators to the final and starting positions of the sequence that can be sorted. The range used can be [fst,lst).

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

    Re: How the sort() can be used in CPP programming

    The following program could states you that how you can use the sort() function in the C++ language programming while using the sort() function in the programming as shown below :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <cassert>
    using namespace std;
    int main() 
    {
      int b[1000];
      int j;
      for (j = 0; j < 1000; ++j) 
        b[j] = 1000 - j - 1;
      sort(&b[0], &b[1000]);
      for (j = 0; j < 1000; ++j) 
        assert (b[j] == j);
      return 0;
    }

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

    sort() function can be used in CPP programming

    Program :
    Code:
    #include <iostream>
    #include <algorchm>
    #include <vector>
    using namespace std;
    bool mfnctn (int i,int j) 
    { 
    return (i<j); 
    }
    struct myclass 
    {
      bool operator() (int i,int j) 
    {
     return (i<j);
    }
    } 
    myobject;
    int main ()
     {
      int mint[] = {32,71,12,45,26,80,53,33};
      vector<int> mvctr (mint, mint+8);        
      vector<int>::cerator c;
      sort (mvctr.begin(), mvctr.begin()+4);  
      sort (mvctr.begin()+4, mvctr.end(), mfnctn); 
      sort (mvctr.begin(), mvctr.end(), myobject);    
      cout << "mvctr contains:";
      for (c=mvctr.begin(); c!=mvctr.end(); ++c)
        cout << " " << *c;
      cout << endl;
      return 0;
    }
    The output of the above program can be :
    Code:
    mvctr contains: 12 26 32 33 45 53 71 80

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

    Re: How the sort() can be used in CPP programming

    The sort() function algorithm can be used to sort the elements in the given specified range [strt,nd) in the ascending order. Suppose that there can be the two elements with the equal value, there can be no guarantee to in what order these two elements could be in. If there can be the strict weak ordering function of an object cp can be given, then it could be used to compare two objects in-spite of the < operator.

Similar Threads

  1. How to sort the name in Excel
    By Sanju Baba in forum Windows Software
    Replies: 1
    Last Post: 06-01-2012, 10:53 PM
  2. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  3. Socket programming: Is any new Programming Language?
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 11:13 AM
  4. How to use Bubble sort in C# to sort parallel arraylists
    By Ground 0 in forum Software Development
    Replies: 3
    Last Post: 03-08-2009, 12:12 PM
  5. Replies: 3
    Last Post: 13-12-2008, 01:49 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,714,194,545.80403 seconds with 17 queries