以下代码创建一个Task结构列表,其中每个任务都包含一个指令列表:
#include <list>
using namespace std;
/////////////////////////////////////////////
struct Insts{
int dest, src1, src2, imm;
bool is_mem, is_int;
string op;
};
struct Task{
int num_insts, task_id;
list<Insts> inst;
};
//////////////////////////////////////
list<Task> tasks; //Global list of tasks
//////////////////////////////////////
int main(){
Insts tmp_inst;
Task task1;
tmp_inst.dest = 9; tmp_inst.src1 = 3; tmp_inst.src2 = 2;
tmp_inst.imm = 11; tmp_inst.op = "add";
task1.num_insts = 1;
task1.task_id = 1;
task1.inst.push_back(tmp_inst);
//... add more instructions and updating .num_insts
//pushback task1 into global "tasks" list
tasks.pushback(task1); //????
//>>print() all insts for all tasks? (Debugging purposes)
}
根据评论:
1)正在推回task1正确以获取任务列表?
2)如何打印任务列表中的指令元素列表? (即打印所有任务的所有说明)
答案 0 :(得分:1)
使用list
迭代器:
for (std::list<Task>::iterator it = tasks.begin(); it != tasks.end(); ++it){
// `it` is a reference to an element of the list<Task> so you must dereference
// with *it to get the object in your list
for (std::list<Insts>::iterator it2 = it->inst.begin(); it2 != it->inst.end(); ++it2){
// it2 will now reference the elements within list<Insts> of an object Task
}
}