Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;二维地图?像二维阵列?_C++_Arrays_Map_2d - Fatal编程技术网

C++ C++;二维地图?像二维阵列?

C++ C++;二维地图?像二维阵列?,c++,arrays,map,2d,C++,Arrays,Map,2d,可以制作二维地图吗 像这样: map< int, int, string> testMap; 感谢您的时间:)是的,在std::map中使用std::pair std::map< std::pair<int, int>, string> testMap; testMap[std::make_pair(1,3)] = "Hello"; std::maptestMap; testMap[std::make_pair(1,3)]=“Hello”; 您可以嵌套两个

可以制作二维地图吗

像这样:

map< int, int, string> testMap;

感谢您的时间:)

是的,在
std::map
中使用
std::pair

std::map< std::pair<int, int>, string> testMap;
testMap[std::make_pair(1,3)] = "Hello";
std::maptestMap;
testMap[std::make_pair(1,3)]=“Hello”;
您可以嵌套两个贴图:

#include <iostream>
#include <map>
#include <string>

int main()
{
    std::map<int,std::map<int,std::string>> m;

    m[1][3] = "Hello";

    std::cout << m[1][3] << std::endl;

    return 0;
}
#包括
#包括
#包括
int main()
{
std::map m;
m[1][3]=“你好”;

std::cout如果它对任何人都有帮助,下面是基于andre的答案构建的类的代码,它允许通过括号操作符进行访问,就像常规2D数组一样:

模板
类图{
/*
泛型图ADT,它使用一个映射来处理大型稀疏图,可以
在对数时间内像任意大小的2d数组一样访问。
*/
私人:
typedef std::map graph_type;
图\型图;
类src顶点{
私人:
图类型&图;
尺寸垂直于src;
公众:
SrcVertex(图类型&图):图(图){}
操作员[](尺寸垂直){
返回图[std::make_pair(vert_src,vert_dst)];
}
无效集垂直(尺寸垂直){
这->垂直方向=垂直方向;
}
}src_顶点_代理;
公众:
Graph():src_顶点_代理(图){}
SrcVertex和运算符[](大小\u t垂直\u src){
src\u vertex\u proxy.set\u vert\u src(vert\u src);
返回src_vertex_proxy;
}
};

很有趣。通常,我可能会使用它,但是我实际上使用的是Qt creator,所以我认为他们没有标准库。他们可能有类似的东西,但是idk。例如,我使用的不是std::map,而是QMap。语法很好,sameQt creator只是一个IDE(集成开发环境)因此,即使Qt Creator在处理定义QMap的Qt库时是有用的,您仍然可以编写标准C++ C++(例如使用STD:MAP)。这将比RyanMcK的解决方案更加有效。@mc360pro a
std::pair
没有添加额外的
std::map
那么重。地图地图的查找时间是
O(Lg(n)*Lg(n))
,而一对地图只需要
O(Lg(n))
。这绝对不是
O(Lg(n)*Lg(n))
。它是
O(lg(n)+lg(n))
,或者只是
O(lg(n))
。第一次地图查找需要
lg(n)
,第二次地图查找也需要
lg(n)
。当然,这是假设一个方形“数组”。这可能应该分解为
m
n
,但它仍然以类似的方式简化。
#include <iostream>
#include <map>
#include <string>

int main()
{
    std::map<int,std::map<int,std::string>> m;

    m[1][3] = "Hello";

    std::cout << m[1][3] << std::endl;

    return 0;
}