将元素插入到2D矢量C ++中

时间:2013-10-10 01:37:50

标签: c++ vector 2d

我试图在这个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值进入双部分

2 个答案:

答案 0 :(得分:2)

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

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

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

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

答案 1 :(得分:1)

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

或者

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