【发布时间】:2015-12-05 17:43:54
【问题描述】:
我正在查看 rapidjson 代码以进行可能的集成。我可以看到,由于新的 C++11,您实际上可以在 C++ 中执行关联数组,尽管我不确定速度优势。但是,在他们的示例代码中,我看到了这个:
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
char buffer[sizeof(json)];
memcpy(buffer, json, sizeof(json));
if (document.ParseInsitu(buffer).HasParseError())
return 1;
printf("\nAccess values in document:\n");
assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
assert(document.HasMember("hello"));
assert(document["hello"].IsString());
printf("hello = %s\n", document["hello"].GetString());
看起来 Document 是一个具有被调用方法的类,但同时他能够使用 document["hello"] 作为关联数组来访问它?这是怎么回事?
【问题讨论】:
-
我在这里没有看到任何矛盾。这只是一个语法问题。如果在一种语言中,集合是对象(如在 C++ 中),为什么不能同时使用下标语法和方法调用来使用它们?
-
C++ 在标准库中有关联数组(
std::map和std::unordered_map),所以这不足为奇...
标签: c++ arrays c++11 associative-array rapidjson