-2

For example, if I have a vector in a template class, than how can I sort it, or how can I iterate on the vector? Because I tried like this:

#ifndef SAVEVIEW_H
#include <array>
#include <vector>

template<typename T>
class sorted_array_view {
public:
    sorted_array_view(T* array, size_t size) {
        append(array, size);
    }

    void append(T* array, size_t size) {
        for( int idx = 0; idx < size; ++idx)
        {
            std::cout << "value of array at " << idx <<": "<< array[idx] << std::endl;
            data.push_back(array[idx]);
        }
        sort(first(),last(), data);
    }

    T at(size_t index) const {
        return data[index];
    }

    size_t size() {
        return data.size();
    }

    const size_t size() const {
        return data.size();
    }

private:
    size_t first() {
        return data[0];
    }
    size_t last() {
        return data[data.size()-1];
    }
    void sort (sorted_array_view first, sorted_array_view last, std::vector<T>);
    std::vector<T> data;
};

#endif // SAVEVIEW_H

And use with: sort(first(),last(), data); returned me this error:

error: no matching function for call to 'sorted_array_view<int>::sort(size_t, size_t, std::vector<int, std::allocator<int> >&)' 

Any idea, advice? Thank you!

korte alma
  • 59
  • 6
  • 2
    Could you please post all code? sort is method of class or function? – Unick Jun 22 '18 at 10:09
  • I would recommend learning C++ by reading a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There's so much wrong, with the basics in your example (which isn't even [mcve]), that I don't even know where to start explaining... – Algirdas Preidžius Jun 22 '18 at 10:09
  • 3
    `std::sort(data.begin(), data.end());` You called the sort function on your access functions, while sort accepts iterators. Also your functions could be implemented with `data.first()` and `data.last()`. If that actually works (hard to tell because that's not complete code), ping me and I'll turn that into a proper answer. – Bartek Banachewicz Jun 22 '18 at 10:09
  • It is definitely not the template what is causing error here... – W.F. Jun 22 '18 at 10:10
  • @Unick I updateted my question, sorry, I thought there was the problem... – korte alma Jun 22 '18 at 10:14
  • Possible duplicate of [How to use std::sort with a vector of structures and compare function?](https://stackoverflow.com/questions/328955/how-to-use-stdsort-with-a-vector-of-structures-and-compare-function) – Joseph D. Jun 22 '18 at 10:15
  • @BartekBanachewicz I have a template vector, and I want to sort it, iterate on it. Maybe, this is the most minimal summary of this qestion. – korte alma Jun 22 '18 at 10:16
  • 1
    @kortealma If you re-sort it after every range insert, why don't you just use a `set` or a `multiset` though? If the only operations you want is inserting and iterating in order, it'll be way faster and will save you writing the entire class. This looks very much like an XY problem. What are you *actually* trying to accomplish here? – Bartek Banachewicz Jun 22 '18 at 10:17
  • 2
    Your class (implicitly) claims to be a *view* onto an array, whereas it's actually a *copy*. If your backing container held `std::reference_wrapper`s, then it'd be a *view*, as it is, it's just a (bad) `multi_set` – Caleth Jun 22 '18 at 10:25
  • 1
    @Caleth In fact, it's easier to make such a view on a multiset anyway - [Example](http://coliru.stacked-crooked.com/a/6cadeaeb348b0f84). (**danger** - meant for illustration only. Never use!) – Bartek Banachewicz Jun 22 '18 at 10:35

1 Answers1

4

std::sort takes random access iterators (e.g. vector iterators or pointers). You gave it numbers (size_t) as input.

#include <algorithm>
#include <vector>

void sortIt(std::vector<int> &vector) {
    std::sort(vector.begin(), vector.end());
}

The sort method of your class was also defined differently than you called it.

You declared it to take sorted_array_view by value as the first two arguments and then you passed it size_t, because that is what the methods first and last return.

void sort (sorted_array_view first, sorted_array_view last, std::vector<T>);

My suggestion would be to remove the sort method and replace the sort call with a call to std::sort like this:

std::sort(data.begin(), data.end()); // was sort(first(),last(), data);
PaulR
  • 3,587
  • 14
  • 24
  • I tried as you wrote, to call only the `std::sort` but this actually not sorted the vector. I printed it, and the vector remained the same... – korte alma Jun 22 '18 at 10:45
  • @korte alma: what was the type T? Perhaps you need a different comparison function. – PaulR Jun 22 '18 at 10:47
  • `int`, `string` – korte alma Jun 22 '18 at 10:52
  • Did the vector remain the same or the `T*` that you passed in? The latter will never change (despite your class being called `view`) because you are sorting a copy of the data (stored in the `vector`). – Max Langhof Jun 22 '18 at 10:54
  • @korte alma: Whatever you did, it was probably not `std::sort`'s fault: http://coliru.stacked-crooked.com/a/fe8b556115f44740. Note that the strings were sorted lexically according to the ASCII values of the characters. – PaulR Jun 22 '18 at 11:01
  • @korte alma: You do notice that you print the original array argument after the sort and not the sorted data, right? – PaulR Jun 22 '18 at 11:19