0

我正在尝试将一个元素插入到这个 2D 矢量变量中,但我不确定如何使用这种奇怪的矢量类型来做到这一点

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
        }
    }
}

我要插入的位置是用注释指定的。我希望 i 值进入该对的整数部分,而 phi 值进入双精度部分

4

2 回答 2

2

要创建一个std::pair您可以使用std::make_pair()的类型,在这种情况下将自动推断出类型:

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

或者您可以自己指定类型:

phiTable.push_back(std::pair<int,double>(i, phi));
于 2013-10-10T01:45:51.913 回答
1
pair<int, double> t(i, phi);
phiTable.push_back(t);

或者

phiTable.push_back(std::make_pair<int, double>(i, phi));
于 2013-10-10T01:45:30.497 回答