0

I have an array of ints. How to create an iterator for it that will sort every x element in the array? I would like to use std::sort.

template<class T>
class SomeClass
{
private:
    int intArray[100];
};
JeJo
  • 30,635
  • 6
  • 49
  • 88
Ann
  • 53
  • 1
  • 6
    `std::sort(std::begin(intArray), std::end(intArray));`? – Scheff's Cat Sep 12 '19 at 10:08
  • 1
    A bit more sophisticated (with variable `n` of used elements): [Demo on coliru](http://coliru.stacked-crooked.com/a/271a6ae60f3b0b0e). – Scheff's Cat Sep 12 '19 at 10:11
  • Why not make your demo on coliru an answer? – dtell Sep 12 '19 at 10:13
  • 1
    In general, (concerning the array with a variable number of used elements) `std::vector` is prefereable over plain C arrays. – Scheff's Cat Sep 12 '19 at 10:13
  • @Scheff Why is std::array almost never recommended here on SO? It can be very useful to let the compiler know the arraysize, if it's fixed. – nada Sep 12 '19 at 10:36
  • @nada I don't know. ;-) Though, I recently read on SO that `std::array` should be preferred over plain C arrays in modern C++ [here](https://stackoverflow.com/a/57783976/7478597). Old bones like me might be a bit inflexible to replace something that's known and works. ;-) – Scheff's Cat Sep 12 '19 at 10:40

3 Answers3

3

For arrays their iterators are pointers to their elements.

You can use standard generic functions std::begin and std::end declared in the header <iterator> to get a range of iterators.

So for example to sort the array defined in the class you can use standard algorithm std::sort. For example

template<class T>
class SomeClass
{
public:
    void sort()
    {
        std::sort( std::begin( intArray ), std::end( intArray ) );
    }

    template <typename Comparison>
    void sort( Comparison comp )
    {
        std::sort( std::begin( intArray ), std::end( intArray ), comp );
    }

private:
    int intArray[100];
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • You do not need to overload `sort` member with a template function. Simply provide the default template parameter for `Comparision`. That means: `template > void sort(Comparison comp = {}) { std::sort(std::begin(intArray), std::end(intArray), comp); }`. See here: https://wandbox.org/permlink/oRLKY7T9IbkPYk9K – JeJo Sep 15 '19 at 10:40
2

If you meant of providing iterators to the SomeClass class, you can as follows:

#include <iostream>
#include <algorithm>// std::sort
#include <iterator> // std::begin. std::end

template<class T>
class SomeClass
{
private:
    int intArray[10]{ 2, 3, 1, 4, 5, 7, 6, 8, 9, 10 };
    // or fill the array as per
public:
    // provide begin() and end() iterators
    auto begin()->decltype(std::begin(intArray)) { return std::begin(intArray); }
    auto end()->decltype(std::end(intArray)) { return std::end(intArray); }

    // or since C++14, no trailing return is needed!
    // auto begin() { return std::begin(intArray); }
    // auto end() { return std::end(intArray); }
};
int main()
{
    SomeClass<int> obj;
    // now you can apply `std::sort` like this
    std::sort(obj.begin(), obj.end());
    // also you can iterate using range based for - loop
    for (const auto ele : obj) std::cout << ele << " ";
    return 0;
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
-1
int myArray[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myArray, myArray+8); // 32 71 12 45 26 80 53 33
// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
//OR use the below line for myArray[100] it will work fine
std::sort(std::begin(myArray), std::end(myArray));

I hope the above code may help to answer your question. I am using a default array, however you can freely use myArray[100] and add the values from user or randomly.

Ruchir
  • 1,018
  • 7
  • 17