Calimero
Calimero

Reputation: 1

Inserting values of class to ordered Linked list - C++

I have class Info, and in main.cpp I would like to create object implemented with Ordered Linked List of type Info like orderedLinkedList<Info> object1;

Can some explain how to insert values (first name, last name and birth year).

#ifndef _INFO_H
#define _INFO_H

#include <iostream>
#include <string>

class Info{

    private:
        std::string _first_name;
        std::string _last_name; 
        int _birth_year;

    public:
        Info()=default;
        ~Info()=default;
        Info(string first_name, string last_name, int birth_year) : _first_name(first_name), _last_name(last_name), _birth_year(birth_year) {}

        string fName() {return _first_name;}
        string lName() {return _last_name;}
        int bYear() {return _birth_year;}

        void setInfo(string first_name, string last_name, int birth_year);          


};

void Info::setInfo(string first_name, string last_name, int birth_year)
{
    _first_name = first_name;
    _last_name = last_name;
    _birth_year = birth_year;
}

#endif

Upvotes: 0

Views: 132

Answers (1)

Werkov
Werkov

Reputation: 616

there are multiple issues in your question.

There is no such thing as ordered linked list in STL. If you structure is as small as presented, I would advise against using linked list at all.

Then you could use e.g. std::vector (you'll still need own implementation of keeping it sorted) or better std::set that already implements sorted behavior and is generally asymptotically better than vector/list (O(log(n)) for std::set vs O(n) for std::vector). In order to your Info class be usable within std::set, you must implement its less-than operator (signature in this case would be bool Info::operator<(const Info &rhs)).

BTW do not use names with leading underscores, as they're reserved in C++. (Trailing underscores are fine.)

Upvotes: 1

Related Questions