Topic: sorting vectors


Author: Florian Eisele <florian.eisele@gmx.net>
Date: Sat, 13 Jan 2001 23:32:40 GMT
Raw View
Is there any standard function that sorts vectors? It seems that qsort()
does only sort arrays. Is that right?

Florian

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: James Dennett <james@evtechnology.com>
Date: Sun, 14 Jan 2001 18:02:23 GMT
Raw View
Florian Eisele wrote:

> Is there any standard function that sorts vectors?

std::sort is a template function which will probably do what you
want.  std::stable_sort is a related function which will preserve
the order of "equal" elements, but might be slower.

std::sort is typesafe and given a good compiler can be faster
that the qsort function which was inherited from C.  It certainly
makes code less ugly.

If you're writing C++ you ought to have a good reference book;
looking up sort in the index will probably take you to std::sort
at least to give you more details.

 > It seems that qsort() does only sort arrays.  Is that right?


Yes, qsort is only designed to sort arrays.

-- James Dennett <jdennett@acm.org>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Edward Diener <eddielee@abraxis.com>
Date: Sun, 14 Jan 2001 18:03:26 GMT
Raw View
Try std::sort. It sorts any container which has random access iterators.
Qsort ? Bah ! This is a C++ forum. Real men and women not only eat quiche but
they also use C++ standard algorithms <g>. BTW my main compiler's version
std::sort uses a quicksort algorithm.

Florian Eisele wrote:

> Is there any standard function that sorts vectors? It seems that qsort()
> does only sort arrays. Is that right?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: James Kuyper <kuyper@wizard.net>
Date: Sun, 14 Jan 2001 18:02:50 GMT
Raw View
Florian Eisele wrote:
>
> Is there any standard function that sorts vectors? It seems that qsort()
> does only sort arrays. Is that right?

std::sort(v.begin(), v.end()) will do the job quite nicely.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Sebastian Moleski \(SurakWare\)" <smoleski@surakware.com>
Date: Mon, 15 Jan 2001 17:23:00 GMT
Raw View
"Florian Eisele" <florian.eisele@gmx.net>:
> Is there any standard function that sorts vectors? It seems that qsort()
> does only sort arrays. Is that right?

How about std::sort.

sm



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: graywane@NOT-AT.home.com (graywane)
Date: Mon, 15 Jan 2001 17:25:43 GMT
Raw View
In article <93q9cj$imv$1@news.netcologne.de>, Florian Eisele wrote:
> Is there any standard function that sorts vectors? It seems that qsort()
> does only sort arrays. Is that right?

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

int
main(int argc, char** argv)
{
  vector<int> vec;
  vector<int>::const_iterator iter;

  // Insert some random numbers.
  srand(time(0));
  for (int i = 0; i < 6; i++)
    vec.push_back(rand() % 20);

  // Print the vector contents.
  for (iter = vec.begin(); iter != vec.end(); ++iter)
    cout << *iter << ' ';
  cout << endl;

  // Sort the vector.
  sort(vec.begin(), vec.end());

  // Print the vector contents again.
  for (iter = vec.begin(); iter != vec.end(); ++iter)
    cout << *iter << ' ';
  cout << endl;

  return 0;
}


A sample output for the above program is:

4 1 14 19 12 5
1 4 5 12 14 19

Note that sorting the vector is as simple as calling sort() with the start
and end of the range you want sorted.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: pagey@drcsdca.com (Manish P. Pagey)
Date: Mon, 15 Jan 2001 18:18:25 GMT
Raw View
>>>>> "Florian" == Florian Eisele <florian.eisele@gmx.net> writes:

    Florian> Is there any standard function that sorts vectors? It
    Florian> seems that qsort() does only sort arrays. Is that right?

There is a set of sorting operations available under the standard
algorithms library. See section 25.3 in the C++ standard or look up
"algorithms" in a C++ book.

--
Pagey

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: "Scott Robert Ladd" <scott@coyotegulch.com>
Date: Tue, 16 Jan 2001 02:20:50 GMT
Raw View
"Florian Eisele" <florian.eisele@gmx.net> wrote...
> Is there any standard function that sorts vectors? It seems that qsort()
> does only sort arrays. Is that right?

#include <vector>
#include <algorithm>

using namespace std;

void somefunc()
{
    // create a vector
    vector<int> v;

    // do something to fill vector

    // sort the vector using this template from <algorithms>
    sort(v.begin(),v.end());
}

I'm not at a compiler right now, but I believe the above is essentially
correct... the <algorithms> header file includes a number of useful tools.

I hope that helps!

--
Scott Robert Ladd
http://www.coyotegulch.com


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Tue, 16 Jan 2001 02:20:30 GMT
Raw View
In article <93q9cj$imv$1@news.netcologne.de>, Florian Eisele
<florian.eisele@gmx.net> writes
>Is there any standard function that sorts vectors? It seems that qsort()
>does only sort arrays. Is that right?

To even ask this question means that you have completely missed what the
STL is about. There are a whole range of template functions to sort
vectors that include sort, partial_sort, and stable_sort. Assuming the
objects you wish to sort have an operator< provided the following line
sorts a vector v of objects in ascending order:

sort(v.begin(), v.end() );

That could hardly be simpler. Actually the same function will sort an
array a of n objects:

sort(a, a+n);


There are also versions that allow you to provide comparison functions.
The whole system of containers and algorithms in the C++ Standard
Library is orders of magnitude more sophisticated than the C facilities
and they can almost all be applied to raw C arrays as well as to C++
containers.

However you really should be asking questions like this on
comp.lang.c++.moderated

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]