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
:)
#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;
}
#include <iostream>
usingnamespace std;
void func1(int **myArray,constint 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;
}