How to use std sort to sort an array in C

0 votes
How to utilize the std:: template library

Sort an array specified as int v[2000] using sort();

Is there a function in C++ that can get the start and end index of an array?
Jun 1 in C++ by Nicholas
• 2,460 points
15 views

1 answer to this question.

0 votes

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays:

#include <algorithm>

int main(){
  int v[2000];
  std::sort(std::begin(v), std::end(v));
}

If you don't have access to C++0x, it isn't hard to write them yourself:

// for container with nested typedefs, non-const version
template<class Cont>
typename Cont::iterator begin(Cont& c){
  return c.begin();
}

template<class Cont>
typename Cont::iterator end(Cont& c){
  return c.end();
}

// const version
template<class Cont>
typename Cont::const_iterator begin(Cont const& c){
  return c.begin();
}

template<class Cont>
typename Cont::const_iterator end(Cont const& c){
  return c.end();
}

// overloads for C style arrays
template<class T, std::size_t N>
T* begin(T (&arr)[N]){
  return &arr[0];
}

template<class T, std::size_t N>
T* end(T (&arr)[N]){
  return arr + N;
}
answered Jun 1 by Damon
• 3,780 points

Related Questions In C++

0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20 in C++ by Damon
• 3,780 points
6 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1 in C++ by Damon
• 3,780 points
21 views
0 votes
0 answers

How to traverse stack in C++?

Is traversing std::stack possible in C++? It is not possible to traverse using the following method.  Because there is no member end in std::stack. std::stack<int> foo; // .. for (__typeof(foo.begin()) it = foo.begin(); ...READ MORE

Jun 1 in C++ by Nicholas
• 2,460 points
26 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 3,780 points
20 views
0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1 in C++ by Damon
• 3,780 points
26 views
0 votes
1 answer

implementing merge sort in C++

To respond to the question:  std::vectorT> is used to create dynamically sized arrays at runtime.  Ideally, you'd use one of these to get feedback.  If not, they are simple to convert.  For instance, you might make two arrays like this: template <typename T> void merge_sort(std::vector<T>& array) { ...READ MORE

answered Jun 10 in C++ by Damon
• 3,780 points
19 views
0 votes
0 answers

Sorting Characters Of A C++ String

Is there a built-in method for sorting characters in a string, or do I have to construct my own? for instance: string word = "dabc"; I would want to ...READ MORE

2 days ago in C++ by Nicholas
• 2,460 points
5 views
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
364 views
0 votes
1 answer

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

answered Jun 20 in C++ by Damon
• 3,780 points
11 views
0 votes
1 answer

How to use enums in C++

This will be sufficient to declare your ...READ MORE

answered Jun 20 in C++ by Damon
• 3,780 points
8 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP