2016-11-28 46 views
0

我設法具有omnetpp.ini一個向量作爲輸入,string和用作布爾按照下面,OMNET ++:多維向量作爲輸入

//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" 

任何想法如何,我可以解決這樣的問題呢?

回答

1

沒有簡單的方法來讀取多維數組。而使用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"