0

I am fairly new to C++ and pointers, and would greatly appreciate any help. I am trying to print a sorted array of pointers without changing the original array of structs. I cannot properly sort the pointers. I am using the std::sort that worked on the original array, but I fail at using it on pointers. To make things worse, my failed attempts all change the original. Thank you for your time.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string.h>

using namespace std;

struct Student
{
    int age;
    char name[30];

};

void displayStudent(Student s)
{

    cout << endl<< s.age<< "    "<< s.name;

}


int main()
{
    Student s1;
    s1.age = 10;
    strcpy(s1.name, "Guy");

    Student s2;
    s2.age = 33;
    strcpy(s2.name, "Buddy");

    Student s3;
    s3.age = 16;
    strcpy(s3.name, "Friend");

    Student s4;
    s4.age = 55;
    strcpy(s4.name, "Pal");

    Student myClass[4];
    myClass[0] = s1;
    myClass[1] = s2;
    myClass[2] = s3;
    myClass[3] = s4;

    Student *myClassPt;
    myClassPt = &myClass[0];
    Student *SBN[4];
    Student *SBG[4];
    Student *SBA[4];
    for (int i = 0; i < 4; i++)
    {
        SBN[i] = &(myClassPt[i]);
        SBA[i] = &(myClassPt[i]);

    }
    cout << "Original" << endl;
    for (int i = 0; i < 4; i++)
    {
        displayStudent(myClass[i]);
    }

    std::sort(*SBN, *SBN + 4, [](Student  &a, Student  &b){ return a.name < b.name; });
    std::sort(*SBA, *SBA + 3, [](Student const &a, Student const &b){ return a.age < b.age; });

    cout <<endl<<endl<< "Sorted by name" << endl;
    for (int i = 0; i < 4; i++)
    {
        displayStudent(*SBN[i]);
    }
    cout << endl << endl << "Sorted by age" << endl;
    for (int i = 0; i < 4; i++)
    {
        displayStudent(*SBA[i]);
    }

    cout << endl <<endl<< "Original" << endl;
    for (int i = 0; i < 4; i++)
    {
        displayStudent(myClass[i]);
    }

    return 0;
}
2
  • What do you mean by sorting pointers? Commented Oct 21, 2014 at 6:06
  • I'm trying to create an array of pointers that point to an array of structs. Then sort that array of pointers by name or age of the struct without changing the original array. I hope that makes sense. Commented Oct 21, 2014 at 6:19

1 Answer 1

1

It seems you want to sort the pointers according to the objects they point to. So you need to sort the pointers according to the objects they point to, instead of trying to sort the objects they point to directly:

std::sort(SBN, SBN + 4, [](const Student* a, const Student* b)
                        { return a->name < b->name; });

Here's a working example:

#include <iostream>
#include <algorithm>

struct student { int age; };

int main()
{
  student ss[] = { {23}, {12}, {42}, {9}};
  std::cout << "students\n";
  for (const auto& s : ss) std::cout << s.age << " ";
  std::cout << std::endl;

  student* ps[] = { &ss[0], &ss[1], &ss[2], &ss[3]};
  std::cout << "pointers to student\n";
  for (auto p : ps) std::cout << p->age << " ";
  std::cout << std::endl;

  std::sort(ps, ps + 4, [](const student* a, const student* b)
                        { return a->age < b->age;});
  std::cout << "pointers to student after sorting\n";
  for (auto p : ps) std::cout << p->age << " ";
  std::cout << std::endl;
}

Output:

students
23 12 42 9 
pointers to student
23 12 42 9 
pointers to student after sorting
9 12 23 42 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your @juanchopanza answer but unfortunately when I print out the pointers remain unsorted after using this. I am thinking about what you said and hopefully it moves me in the right direction
Thanks! I'll try and see what else it could be

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.