user2699298
user2699298

Reputation: 1476

How can I pass the whole struct to a function including all elements?

I have a structure that I want to pass to a function which will sort the struct. However, I don't know how to pass the WHOLE structure.

What I've done is this until now:

void sort_datoteka_sifra(pole &artikli){
}

And I call it like sort_datoteka_sifra(artikli[0]) etc.. but it only passes the [0] elements, I want to pass the whole structure, so that I can use it in the function without having to call artikli[0], artikli[1] and so on in the main function.

Upvotes: 0

Views: 145

Answers (2)

leemes
leemes

Reputation: 45675

You have several options here.

  1. Pass the array as a pointer to its first element as well as the number of elements:

    void sort_datoteka_sifra(pole *artikli, int count){
    
    }
    

    If count is static (known at compile time), you can also pass the array by reference:

    void sort_datoteka_sifra(pole (&artikli)[100]){
    
    }
    

    If you don't want to hardcode the count, use a function template:

    template <int N>
    void sort_datoteka_sifra(pole (&artikli)[N]){
    
    }
    
  2. Use std::vector instead of C-arrays:

    void sort_datoteka_sifra(std::vector<pole> &artikli){
    
    }
    
  3. Use std::sort instead of your custom sort function (#include <algorithms>) and use it with either your existing C-array or (recommended) a std::vector:

    std::sort(std::begin(artikli), std::end(artikli));
    

    You have to provide a way to compare two objects; this is done by either overloading operator< or by passing a function (or functor) to the sort algorithm:

    bool comparePole(const pole & a, const pole & b) {
        return /* condition when you want to have a before b */
    }
    
    std::sort(std::begin(artikli), std::end(artikli), &comparePole);
    

    If you don't want to write a function and have C++11, you can use a lambda function:

    std::sort(std::begin(artikli), std::end(artikli), [](const pole & a, const pole & b) {
        return /* condition when you want to have a before b */
    });
    

    If you want to compare the elements by some member (which has a corresponding operator< overload, which is the case for simple types like int, std::string, etc.), use compareByMember from my other answer at https://stackoverflow.com/a/20616119/592323, e.g. let's say pole has an int ID by which you want to sort:

    std::sort(std::begin(artikli), std::end(artikli), compareByMember(&pole::ID));
    

    To sort a sub-array of size count, don't use std::end but:

    std::sort(std::begin(artikli), std::begin(artikli) + count, &comparePole);
    

Of course you can combine the third option with one of the first two, i.e. provide a sort function which is implemented in terms of std::sort.

Upvotes: 3

Sebastian
Sebastian

Reputation: 8164

Your function requests a reference to a single element. And you obviously also pass only a single element. So, to pass the complete array, you should use a pointer, if it's an array allocated with new Or a statically allocated array, e.g.

void fun(pole* artikli);

Otherwise for C++, it's common to use std::vector and pass it by reference:

std::vector<pole> artikli;

void fun(std::vector<pole>& artikli);

Upvotes: 1

Related Questions