Omnet :多维矢量作为输入

Omnet++: multidimensional vector as input

本文关键字:输入 Omnet      更新时间:2023-10-16

我设法将一个向量从omnetpp.ini用作字符串的输入,并用作Bool,如下所示,

//omnetpp.ini
**.setGate = "true false false false true false false"
//mynetwork.cc
bool MyQueue::gateState()
{
// reading input from omnetpp.ini as string
const char *vstr = par("setGate").stringValue();
std::vector<std::string> v = cStringTokenizer(vstr).asVector();
//Converting String Vector as bool Vector
bool mygate[6];
for (int x = 6; x>=0; x--){
    if (v[x] == "true")
        mygate[x] = true;
    else mygate[x] = false;
...
    };

我在Omnet 手册中没有找到任何使我可以根据以下的多维输入的东西,

//omnetpp.ini
**.setGate = "true false false false true false false, 
              false false false false false false false,
              true false false false true false false"

如何克服这样的问题?

没有简单的方法可以读取多维数组。使用cStringTokenizer可能是最好的主意。我建议以下代码读取该数组:

bool gatesBool[10][10]; // let's assume these dimensions
const char * tableStr = par("setGate").stringValue();
cStringTokenizer table(tableStr, ","); // a comma separates rows
int x = 0;
while (table.hasMoreTokens()) {
    cStringTokenizer row(table.nextToken(), " "); // a space separates elements
    int y = 0;
    while (row.hasMoreTokens()) {
        if (strcmp(row.nextToken(), "true") == 0) {
            gatesBool[x][y] = true;
        } else {
            gatesBool[x][y] = false;
        }
        y++;
    }
    x++;
}

注意:

  1. 您必须事先知道数组的尺寸。

  2. 使用许多行在omnetpp.ini中编写字符串参数,您应该在每行的末端放置后斜线,但最后一行,例如:

    **.setGate = "true true false,
                  false false false"