Question

I am trying to convert some java code to c++, however, I am having an issue with java's list.add versus c++ list.insert. Here is the java code that I've started to convert:

public class SimulationQueue {
    private String arrivalFilePath;
    private int currentTime;

    private class Event {
        private boolean arrival;
        private int start;
        private int span;

        public Event() {
            this.arrival = true;
            this.start = 0;
            this.span = 0;
        }

        public Event(boolean isArrival, int startTime, int span) {
            this.arrival = isArrival;
            this.start = startTime;
            this.span = span;
        }

        public int at() { return start; }
        public boolean isArrival() { return arrival; }
        public int duration() { return span; }

        public void getArrivalEvent(Scanner arrivalFile) {
            this.arrival = true;
            this.start = arrivalFile.nextInt();
            this.span = arrivalFile.nextInt();
        }
    }

    public SimulationQueue(String arrivalFilePath) {
        this.arrivalFilePath = arrivalFilePath;
        this.currentTime = 0;
    }

    private void addEventToList(Event event, List<Event> eventList) {
        if (eventList.isEmpty()) eventList.add(0, event);
        else if (eventList.get(0).at() < event.at()) eventList.add(event);
        else eventList.add(0, event);
    }

And here is the so far converted c++ version:

struct EventList {
    bool arrival;
    int start, span, currentTime;
    string arrivalFilePath;
    EventList(bool isArrival, int startTime, int span);
    void getArrivalEvent(istream& arrivalFile);
    void simulationQueue (string arrivalFilePath);
    void addEventToList(EventList& event, list<EventList> eventList);
    void simulate();

    EventList() {
    this->arrival = true;
    this->start = 0;
    this->span = 0;
    }

    int at() {
        return start;
    }

    bool isArrival() {
        return arrival;
    }

    int duration() {
        return span;
    }
};

EventList::EventList(bool isArrival, int startTime, int span) {
    this->arrival = isArrival;
    this->start = startTime;
    this->span = span;
}

void EventList::getArrivalEvent(istream& arrivalFile) {
    this->arrival = true;
    int first = this->start;
    int duration = this->span;
    arrivalFile >> first;
    arrivalFile >> duration;
}

void EventList::simulationQueue (string arrivalFilePath) {
    this->arrivalFilePath = arrivalFilePath;
    this->currentTime = 0;
}

void EventList::addEventToList(EventList& event, list<EventList> eventList) {
    if (eventList.empty())
}

I'm not very experienced so I know I'm probably approaching this wrong but it's compiling alright. The issue I have is with:

void EventList::addEventToList(EventList& event, list<EventList> eventList) {
        if (eventList.empty())
    }

I don't know how to convert this part to c++:

private void addEventToList(Event event, List<Event> eventList) {
            if (eventList.isEmpty()) eventList.add(0, event);
            else if (eventList.get(0).at() < event.at()) eventList.add(event);
            else eventList.add(0, event);
        }

If I write something like event.insert(event, 0) then it won't fit the parameters that insert takes.

Was it helpful?

Solution

You can just use list::push_back.

void EventList::addEventToList(EventList& event, list<EventList> eventList) {
    eventList.push_back(event);
}

However, if you leave it just like that, the calling function won't see the new item in the list since you passed eventList to the function by value. You need to pass it by reference.

void EventList::addEventToList(EventList& event, list<EventList>& eventList) {
    eventList.push_back(event);
}

list::push_back adds items to the end (back) of the list. If you'd rather add the item at the start (front) of the list, you can use list::push_front.

void EventList::addEventToList(EventList& event, list<EventList>& eventList) {
    eventList.push_front(event);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top