2013-10-10 97 views
0

我試圖插入一個元素到這個二維矢量可變的,但我不知道怎麼做,這種奇怪的矢量型插入元素爲2D向量C++

void CaesarCypher::caesarAttack(string inputFileName, string frequencyFileName, string  outputFileName, string phiFile) 
{ 
    vector<pair<char, double>> cipherTable = charFreqGen(inputFileName, outputFileName, numberDisplayed); 
    vector<pair<char, double>> frequencyTable = charFreqGen(frequencyFileName, outputFileName, 150); 
    vector<pair<int, double>> phiTable; 

    for (int i = 0; i <= 94; i++) 
    { 
     double phi = 0.0; 
     for (const auto& p : cipherTable) 
     { 
      char key = (char) ((int) p.first - i); 
      auto find_it = find(frequencyTable.begin(), frequencyTable.end(), [key](const pair<char, double>& x) { return x.first == key; }); 
      double value; 
      if (find_it != frequencyTable.end()) 
      { 
       value = find_it->second; 
      } 
      phi += p.second * value; 
      //Insert a new element into the phiTable with the int parameter being i and the double paramter being phi 
     } 
    } 
} 

我要插入的地方是通過註釋來指定的。我希望我的價值進入對和披值的整數部分爲雙部分

回答

2

要創建一個std::pair,你可以在這種情況下,類型使用std::make_pair()會自動推斷:

phiTable.push_back(std::make_pair(i, phi)); 

或者你也可以自己指定類型:

phiTable.push_back(std::pair<int,double>(i, phi)); 
1
pair<int, double> t(i, phi); 
phiTable.push_back(t); 

或者

phiTable.push_back(std::make_pair<int, double>(i, phi));