最近在工作中遇到一个std:map代码,其中 map.size() 应该是返回元素的个数,这让我联想到 json 实现里也用了 std::map, 然后当 json 是一个数组类型时,它的 size() 并不是它里面元素的个数,看实例:

#include "json/json.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{
    Json::Value response = Json::Value::null;
    response[0]["name"] = "video";
    response[0]["size"] = 20;

    Json::Value config = Json::Value::null;
    config["name"] = "audio";
    response.append(config);

	response[5]["name"] = "audio";
	response[5]["size"] = 30;

    if(response.isArray())
    {
        for(uint32_t i = 0; i < response.size(); i++)
        {
            printf("array[%d] = %s\n", i, response[i].toStyledString().c_str());
        }
    }
    
    printf("response = %s\n", response.toStyledString().c_str());

    return 0;
}

看上面的例子,我们可以看到 response 是一个数组类型了,它的 size() 应该为 3,中间的 append 实际也是作为一个下标元素插入,它紧跟第 0 个元素的后面。

 

这个应该跟我们使用的常规数组 int array[5] 一样,中间缺省的都是给了默认值 。一开始以为它使用 std::map,size() 也会返回 map 的元素个数,但看到源码后却是这样的:

/// Number of values in array or object
ArrayIndex Value::size() const {
  switch (type_) {
  case nullValue:
  case intValue:
  case uintValue:
  case realValue:
  case booleanValue:
  case stringValue:
    return 0;
  case arrayValue: // size of the array is highest index + 1
    if (!value_.map_->empty()) {
      ObjectValues::const_iterator itLast = value_.map_->end();
      --itLast;
      return (*itLast).first.index() + 1;
    }
    return 0;
  case objectValue:
    return ArrayIndex(value_.map_->size());
  }
  JSON_ASSERT_UNREACHABLE;
  return 0; // unreachable;
}

它返回的是最后一个元素 typedef std::map<CZString, Value> ObjectValues; 里CZString.index() + 1 。而我们使用 response[5] 时,这时 5+1 就是 size() 的返回值,所以 response.size() = 6。

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐