Struct – Pairs

Правка en2, от Nourhan_Abo-Heba, 2026-02-25 13:41:30

Hi everyone This is Lecture 1 of the series. We’ll start with structs, how to use them, constructors, operator overloading, and finally pairs.


1. Motivation (Why Struct?)

We all know the basic data types:

int x;
string y;

But let’s say we have a company with 100 persons. Each person has:

  • name (string)
  • age (int)
  • salary (float)

Naive approach:

string name[100];
int age[100];
float salary[100];

cin >> name[0] >> age[0] >> salary[0];

Problem: arrays are stored in different places in memory → not grouped.

Solution → Struct:

struct Person {
    string name;
    int age;
    float salary;
};

2. Using Struct

int main() {
    Person x;
    x.name = "Nourhan";
    x.age = 21;
    x.salary = 3000;

    cout << x.name << " " << x.age << " " << x.salary << endl;
}

Array of Persons:

Person p[5];
for (int i = 0; i < 5; i++) {
    cin >> p[i].name >> p[i].age >> p[i].salary;
}
  • Just like int arr[5];
  • p[i] = person i
  • p[i].name = person i’s name

3. Declaration vs Initialization

int x;      // declaration
int y = 7;  // initialization

In structs we use constructor for initialization:

struct Person {
    string name;
    int age;
    float salary;

    Person(string a, int b, float c) {
        name = a;
        age = b;
        salary = c;
    }

    Person() {} // empty constructor
};

Usage:

int main() {
    Person p("Nourhan", 21, 4000);
    cout << p.name << " " << p.age << " " << p.salary;
}

4. Functions Inside vs Outside Struct

Inside:

struct Person {
    string name;
    int age;
    float salary;

    void read() {
        cin >> name >> age >> salary;
    }
};

Outside:

void read(Person &x) {
    cin >> x.name >> x.age >> x.salary;
}

Usage:

for (int i = 0; i < n; i++) {
    p[i].read();   // inside
    // read(p[i]); // outside
}

5. Operator Overloading

Problem: comparing two persons → which attribute?

struct Person {
    string name;
    int age;
    float salary;

    bool operator<(const Person &tmp) const {
        return salary < tmp.salary; // compare by salary
    }
};

Now we can:

sort(p, p + n); // uses operator<

For descending order:

return salary > tmp.salary;

6. Compare Function (Outside Struct)

bool compare(Person a, Person b) {
    return a.salary < b.salary;
}

sort(p, p + n, compare);

7. Pair

Like a mini-struct with just 2 elements.

pair<int,int> p;
cin >> p.first >> p.second;

Comparison: first → then second.

pair<int,int> p1 = {1,2};
pair<int,int> p2 = {1,5};

Custom compare:

bool comp(pair<int,int> &a, pair<int,int> &b) {
    if (a.first == b.first) return a.second < b.second;
    return a.first < b.first;
}

Stls problems D — LR insertion D — String Formation B — String Rotation D — Cylinder D — Pair of Balls Problem — 1511C — Codeforces Problem — 1374C — Codeforces Problem — 26B — Codeforces Problem — 1452C — Codeforces Problem — 1579E1 — Codeforces Problem — 381A — Codeforces Problem — 1169B — Codeforces Problem — 1029C — Codeforces Problem — 706B — Codeforces Problem — 600B — Codeforces


Summary

  • Struct groups multiple variables in one type
  • Can use arrays of structs
  • Declaration vs initialization (constructors)
  • Functions inside/outside struct
  • Operator overloading (<)
  • Compare functions
  • Pair = built-in struct of 2 values

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский Nourhan_Abo-Heba 2026-02-25 13:44:11 557
en2 Английский Nourhan_Abo-Heba 2026-02-25 13:41:30 557
en1 Английский Nourhan_Abo-Heba 2025-09-01 15:23:06 3403 Initial revision (published)