c++ 0x 3d map初始化类似于php的关联数组
C++0x 3d map initializing like in php associative arrays
我刚刚进入新的c++0x的东西,并实例化一个像这样的映射:
std::map<int, std::map<int, int>> foo;
foo[1][2] = 3;
是很容易实现的。但我能在php中做一些事情吗?
$array = array(
1 => array(
2 => array(
3
)
)
);
我不熟悉语法。也许像这样
foo[][][] = {
1 {
2 {3}
}
};
所以我不需要一直写索引:
foo[1][2] = 3;
foo[1][3] = 4;
foo[1][4] = 5;
是,使用c++11特性统一初始化:
#include <iostream>
#include <map>
int main()
{
// The value_type of a map is pair<const Key, T>.
// To initialize a map an initializer list
// of pair<Key, T> objects must be specified.
// To initialize a pair:
//
std::pair<int, int> p{9, 10};
std::cout << "pair:n (" << p.first << ", " << p.second << ")nn";
// To initialize a simple map (no nesting)
// with value_type of pair<int, int>:
//
std::map<int, int> simple_map
{ // K V
{ 5, 6 },
{ 7, 8 }
};
std::cout << "simple_map:n";
for (auto const& i: simple_map)
{
std::cout << " (" << i.first << ", " << i.second << ")n";
}
std::cout << "n";
// To initialize a complex map (with nesting)
// with value_type of pair<const int, map<int, int>>
//
const std::map<int, std::map<int, int>> complex_map
{ // K V
// k v
{ 1, { {3, 4},
{5, 6} }
},
{ 2, { {7, 8},
{8, 8},
{9, 0} }
}
};
std::cout << "complex_map:n";
for (auto const& mi: complex_map)
{
std::cout << " (" << mi.first << ", ";
for (auto const& p: mi.second)
{
std::cout << '(' << p.first << ", " << p.second << ')';
}
std::cout << ")n";
}
}
输出:<>之前对:(9、10)simple_map:(5、6)(7、8)complex_map:(1, (3,4)(5,6))(2, (7,8)(8,8)(9,0))之前
你可以使用统一初始化,但它只适用于初始化,不适用于其他地方。
std::map<int, std::map<int, int>> foo = {
{1, {{2, 3}}}
};
注意{2,3}周围额外的{}。要初始化映射,需要initialization_list中的pair。然后使用统一的初始化构造pair。
相关文章:
- 关联容器的下界复杂性:成员函数与非成员函数
- std::future_error:无关联状态
- 查找自动生成键并具有线性内存消耗的小型关联数组
- 如何在 OpenCV 中将图像与另一个图像相关联
- 删除提升存档对象也会删除与其关联的流?
- 变量如何以及在何处可能没有C++关联的名称?
- 将正态随机变量与任意 RHO(corrcoef) 相关联
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- 试图(稍微)概括C++模板.关联容器键:值反转
- 如何打印源文件中与 GDB 中的程序流关联的所有行号
- 当前不会命中断点。没有调试器目标代码类型的可执行代码与此文件关联
- PHP Profiler语言 - 使用 C++ 从 PHP 扩展获取客户端 IP 和 URL
- 与 std::unique_ptr 和抽象类关联的编译错误
- 如何创建一个对象创建函数,该函数将由与其关联的名称调用?
- Nginx RTMP与InstagramLive-PHP不起作用
- 访问与使用 TraceLoggingWrite 提供的事件关联的用户数据
- 创建一个结构的关联数组,以创建一个缓冲区,供键快速访问
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- PHP & C++ $_FILES 和 HttpSendRequestA
- c++ 0x 3d map初始化类似于php的关联数组