0

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
1
  • Do you mean a forward_list? Commented Apr 27, 2015 at 23:56

1 Answer 1

1

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.)

Sign up to request clarification or add additional context in comments.

Comments

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.