簡體   English   中英

如何訪問 map 的值<string, vector<int> &gt; 在 C++ 中?</string,>

[英]How to access the values of a map<string, vector<int>> in C++?

我正在研究我的遺傳算法版本,以解決 C++ 中的背包問題。 我有一個字符串到矢量的 map

map<string, vector<int>> objects
{
    {"Object 1", {7, 20, 15}},
    {"Object 2", {3, 50, 10}},
    {"Object 3", {5, 80, 12}},
    {"Object 4", {4, 80, 8}},
    {"Object 5", {2, 40, 11}}
};

和一個向量的向量

vector<vector<int>> population;

我將在哪里存儲信息,例如

population[0] = {0, 0, 1, 1, 0};
population[1] = {1, 0, 0, 0, 1};
population[2] = {1, 0, 1, 0, 1};
...

每個向量稱為individual ,給定個體的每個元素表示對應的 object 的存在 ( 1 ) 或不存在 ( 0 )。 因此,例如,第三個人 ( population[2] ) 具有Object 1Object 3Object 5

我想要做的是編寫一個 function 它將接收來自population的索引並返回來自objects的相應值的總和。 population[2]的情況下,我想要另一個包含{14, 140, 38}的向量( 7+5+2, 20+80+40, 15+12+11 )。

但我正在努力訪問objects map 的值。

map<string, vector<int>> objects {/*...*/}

vector<vector<int>> population;

void initializePopulation() {/*...*/}

void getScore(vector<int> individual, vector<int>& sum)
{
    for(int i = 0; i < 3; i++)
    {
        sum.push_back(0);
        for(int j = 0; j < 5; j++)
        {
            if(individual[j] == 1)
            {
                sum[i] += ???;
            }
        }
    }

int main()
{
   /*...*/
   initializePopulation();
   vector<int> sum;
   getScore(population[2], sum);

}

因此,如您所見,我不確定如何繼續sum[i] 有什么建議么? 我對 C++ 不是很流利,所以更詳細的答案將不勝感激!

對於向量的向量以及 map,您可以使用每個循環! 當您的 map 充滿值時

for(auto x: objects){
    cout<<x.first<<" "<<x.second<<endl;
}

這將在 map 中打印鍵值對,中間有空格!

在這個問題中,您也必須在 map 中迭代值(即第二個)!

{"Object 1", {7, 20, 15}}
{"Object 2", {3, 50, 10}}
{"Object 3", {5, 80, 12}}
{"Object 4", {4, 80, 8}}}
{"Object 5", {2, 40, 11}}

對於這樣的事情,以下代碼應該可以工作:

for(auto x: objects){
    cout<<x.first<<" ";
    for(auto y: x.second){
        cout<<y<<" ";
    }
    cout<<endl;
}

對於向量的向量,您可以使用相同的概念!

嘗試將Object 1, Object 2, ....重命名為1,2, ....這將允許您使用 for 循環中的 j 訪問 map 中的值!

對於更簡化的版本,請考慮使用此原型,因為您使用諸如人口和遺傳之類的詞,我假設您的數據非常龐大,因此您最好在傳遞數據時使用const 引用(const&,它們不會被復制和將變為只讀)。 全局變量通常是一個壞主意。

void getScore(map<string, vector<int>> const& objects, vector<int> const& individual, vector<int>& sum)
{
    // iterate over each object for the individual
    for(int i = 0; i < 5; i++)
    {
            // are you sure you want sum as {14, 140, 38} (7+5+2, 20+80+40, 15+12+11) 
            // not {14,0, 140,0, 38} (7+5+2, 0, 20+80+40, 0, 15+12+11)
            // use else part for later
            if(individual[i] == 1)
            {
                // compute sum for each object
                // retrieve object vector
                auto it = objects.find("KEY"); // KEY generation discussed later
                if(it!=objects.end()){ // validate key :::: important
                  vector<int> ob = objects["KEY"];      //it->second
                  sum.push_back(std::accumulate(ob.begin(),ob.end(),0) ); //  https://www.cplusplus.com/reference/numeric/accumulate/
                }
            } /*else {
                sum.push_back(0);
            }*/
        }
    }

密鑰生成:

1)。 生成“對象 1”:

string key = "Object " + to_string(i+1)
auto it = objects.find(key);

2)。 建議:使用整數作為鍵或 go 與枚舉類似

enum ObjList{
    OBJECT_1,
    OBJECT_2,
    OBJECT_3
}
auto it = objects.find(i); //mind your indexes

希望它有幫助,快樂編碼XD

我認為通過一些小的線性代數,您的問題有一個簡單的解決方案:確實,如果您將與對象相關的數值數據存儲到矩陣 A 中,那么對於每個population向量p ,您想要的結果只是p^TA (或等效地, A^T p ) 其中^T表示矩陣或向量的轉置

如果您不打算使用任何線性代數庫,您可以自己實現標量積 下面是實現上述想法的代碼。

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <numeric>

std::vector<int> SP(std::vector<std::vector<int>> const &M, std::vector<int> const &P){
    
    std::vector<int> sums(3,0);

    // perform p^T * A operation
    for (int j=0;j<3;j++)
        for (int i=0;i<5;i++)
            sums[j] += M[i][j] * P[i];

    return sums;
}
int main(){
    
    std::map<std::string, std::vector<int>> objects {
    {"Object 1", {7, 20, 15}},
    {"Object 2", {3, 50, 10}},
    {"Object 3", {5, 80, 12}},
    {"Object 4", {4, 80, 8}},
    {"Object 5", {2, 40, 11}}
    };

    std::vector<std::vector<int>> population(3);
    population[0] = {0, 0, 1, 1, 0};
    population[1] = {1, 0, 0, 0, 1};
    population[2] = {1, 0, 1, 0, 1};


    std::vector<std::vector<int>> A;
    // Extract the numerical data from the map 
    for (auto const& [key, val] : objects)
        A.push_back(val);

    // vector in which the desired values are stored for the 3rd element of the population 
    std::vector<int> s = SP(A,population[2]);

    // Just for checking
    for (int it=0; it<s.size(); it++)
        std::cout << s[it] << std::endl;
    

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM