gpt4 book ai didi

带 vector 的 C++ 2d unordered_map

转载 作者:行者123 更新时间:2023-11-30 04:23:58 26 4
gpt4 key购买 nike

我来自 PHP,我会在那里做

$um['Im a string'][1] = 3;

对于二维关联数组,其中第一个键是字符串,第二个是整数,值也是整数。我尝试在 C++ 中做同样的事情。这是我的尝试:

// experiment.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <unordered_map>
#include <vector>
#include <string>

using std::vector;
using std::string;
using std::unordered_map;

int _tmain(int argc, _TCHAR* argv[])
{

unordered_map <string,vector<int,int>> um;

um["Im a string"][1] = 3;
printf("Out: %d", um["Im a string"][1]);
return 0;
}

显然这不是正确的语法;

最佳答案

vector<int,int>不正确( vector 不是关联容器),您可能需要一个嵌套的 unordered_map<int> .所以:

unordered_map <string,unordered_map<int,int>> um;

关于带 vector 的 C++ 2d unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13058278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com