Sending array of ptrs to function of class

Hello all,

So I can't seem to find anything via google on this, so I figured I'd ask you
Essentially I have an array of pointers that I want to send to a sort function within a class to reorder them within the function. It is an array of the class of the function that I want to send it to. What syntax would I use to do this, or do I have to individually increment and send them one at a time?

Any reference to a page regarding this issue would be greatly appreciated
:)
Why don't you use std::sort? Here is an example of what I think you might mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <array>
#include <iostream>

struct obj {
    int a, b;
};

int main() {
    std::array<obj*, 5> arr;
    // fill up the array somehow

    std::sort(arr.begin(), arr.end(), [](obj* a, obj* b) {
        // Sort the array based off the variables within the pointers
        return (a->a + a->b) < (b->a + b->b);
    });

    // print out array
    for (auto x : arr)
        std::cout << x->a + x->b << " ";

    return 0;
}


See http://www.cplusplus.com/reference/algorithm/sort/ for details.
Last edited on
using std::sort, and provide a predicate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <algorithm>
using namespace std;

struct comp
{
	bool operator () (const int * lhs, const int * rhs) const
	{ return *lhs < *rhs; }
};

int main()
{
	int *ptrArray[] = { new int(1), new int(2), new int(-4), new int(45), new int(17) };
	for (int t = 0; t < 5; ++t)
	{
		cout << *(ptrArray[t]) << " ";
	}
	cout << endl;
	
	sort(&ptrArray[0],  &ptrArray[5], comp());
	
	for (int t = 0; t < 5; ++t)
	{
		cout << *(ptrArray[t]) << " ";
	}
	return 0;
}
Hmm I don't believe we're supposed to use the algorithm library, as this class I am taking is based around developing your own algorithms
closed account (j3Rz8vqX)
A possible example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
void func1(int **myArray,const int arraySize)       //Undefined array size; size passed by constant value
{
    cout<<"==Array undefined size=="<<endl;
    cout<<"Address of [0]: "<<myArray[0]<<endl;
    cout<<"Address of [1]: "<<myArray[1]<<endl;
    cout<<"Value of [0]: "<<*myArray[0]<<endl;
    cout<<"Value of [1]: "<<*myArray[1]<<endl;
    *myArray[0]+=1;
    *myArray[1]+=2;
}
void func2(int *myArray[2])                         //Predefined array size; defined during compile time
{
    cout<<"==Array predefined size=="<<endl;
    cout<<"Address of [0]: "<<myArray[0]<<endl;
    cout<<"Address of [1]: "<<myArray[1]<<endl;
    cout<<"Value of [0]: "<<*myArray[0]<<endl;
    cout<<"Value of [1]: "<<*myArray[1]<<endl;
    *myArray[0]+=1;
    *myArray[1]+=2;
}
int main()
{
    int *myArray[2];                                //Array of type (pointer)int; size 2
    int i = 1;
    int d = 2;
    myArray[0] = &i;                                //pointerInteger@arrayIndex0 = referenceOf(i)
    myArray[1] = &d;                                //pointerInteger@arrayIndex0 = referenceOf(d)
    func1(myArray,2);                               //passing variable muArray - an array of pointer
    func2(myArray);                                 //passing variable muArray - an array of pointer
    cout<<"==Array in main=="<<endl;
    cout<<"Address of [0]: "<<myArray[0]<<endl;
    cout<<"Address of [1]: "<<myArray[1]<<endl;
    cout<<"Value of [0]: "<<*myArray[0]<<endl;
    cout<<"Value of [1]: "<<*myArray[1]<<endl;
    return 0;
}

Edit: Arrays are passed default as reference.
Last edited on
Arrays are passed default as reference.


Arrays are passed as pointers (by value) to their first element.

void func2(int* myArray[2]) is the same as void func2(int**myArray).

http://ideone.com/m1Vzbu
Topic archived. No new replies allowed.