[英]Java to C++ conversion
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. 我正在尝试将一些Java代码转换为C ++,但是,Java的list.add与c ++ list.insert存在问题。 Here is the java code that I've started to convert:
这是我开始转换的Java代码:
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: 这是到目前为止转换的c ++版本:
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++: 我不知道如何将这部分转换为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. 如果我编写类似
event.insert(event, 0)
那么它将不适合插入所需的参数。
You can just use list::push_back
. 您可以只使用
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. 但是,如果只保留它,则调用函数将不会在列表中看到新项,因为您已按值将
eventList
传递给了该函数。 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. list::push_back
将项目添加到list::push_back
的末尾。 If you'd rather add the item at the start (front) of the list, you can use list::push_front
. 如果您希望将项目添加到列表的开头(最前面),则可以使用
list::push_front
。
void EventList::addEventToList(EventList& event, list<EventList>& eventList) {
eventList.push_front(event);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.