How to acess elements of structs inside of a list created with <list> library?

I'm new to C++, coming from C. How do I access each element of each struct in a std::list created with the <list> library?

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <list>
#include <funcoes.h>

using namespace std;

typedef struct candidato{
    int inscricao;
    int idade;
    int cod;
    int nota;
 }candidato_c;

int main(){
    list<candidato_c> l;
    startlist(l);
}

funcoes.h

void startlist (list<candidato_c>& lista1){
    //How to access each element of each index?
}

Upvotes: 0

Views: 160

Answers (1)

Gearoid
Gearoid

Reputation: 86

This is how to to access each element

struct candidato {
    int inscricao;
    int idade;
    int cod;
    int nota;
 };

int main(){
    list<candidato> l;
    startlist(l);
}

static void startlist(const std::list<candidato>& lista1) {
    for (const candidato& c : lista1)
    {
        std::cout << c.cod << std::endl;
    }
}

Upvotes: 1

Related Questions