简体   繁体   English

将元素插入2D向量C ++

[英]Insert an element into a 2D vector C++

I am trying to insert an element into this 2D vector variable but I am not sure how to do that with this strange vector type 我正在尝试将元素插入此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
        }
    }
}

The place I want to insert is specified with a comment. 我要插入的位置带有注释。 I want the i value to go into the integer portion of the pair and phi value into the double portion 我希望i值进入对的整数部分,而phi值进入双精度部分

To create a std::pair you can use std::make_pair() in which case the types will be deduced automatically: 要创建一个std::pair ,可以使用std::make_pair()在这种情况下,将自动推导类型:

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

Or you can specify the types yourself: 或者,您可以自己指定类型:

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

Or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM