-2

我正在使用 C++,我正在从这样的文件行中读取:

D x1 x2 x3 y1

我的代码有:

struct gate {
    char name;
    vector <string> inputs;
    string output;
};

main函数中:

vector <gate> eco;

int c=0;
int n=0;
int x = line.length();
while(netlist[c][0])
{
    eco.push_back(gate());
    eco[n].name = netlist[c][0];
    eco[n].output[0] = netlist[c][x-2];
    eco[n].output[1] = netlist[c][x-1];
}

netlist我已将文件复制到的二维数组在哪里。

我需要帮助来循环输入并将它们保存在 vector 中eco

4

1 回答 1

3

我不完全理解 2D 数组的含义,但我怀疑它是多余的。您应该使用以下代码:

ifstream somefile(path);
vector<gate> eco;
gate g;

while (somefile >> g)
    eco.push_back(g);

// or, simpler, requiring #include <iterator>
vector<gate> eco(std::istream_iterator<gate>(somefile),
                 std::istream_iterator<gate>());

operator >>并为您的类型适当地重载gate

std::istream& operator >>(std::istream& in, gate& value) {
    // Error checking … return as soon as a failure is encountered.
    if (not (in >> gate.name))
        return in;

    gate.inputs.resize(3);
    return in >> gate.inputs[0] >>
                 gate.inputs[1] >>
                 gate.inputs[2] >>
                 gate.output;
}
于 2013-03-11T14:11:26.657 回答