Skip to content

Commit 85023b9

Browse files
committed
优化at访问map,减少一次查找
1 parent 7c87382 commit 85023b9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/jsonlib.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ namespace Jsonlib{
529529
JsonValue::JsonValue(JsonArray&& jsonArray) noexcept{
530530
type_ = JsonType::ARRAY;
531531
content_ = std::move(jsonArray);
532+
jsonArray.clear();
532533
}
533534

534535
// JsonObject拷贝构造
@@ -541,6 +542,7 @@ namespace Jsonlib{
541542
JsonValue::JsonValue(JsonObject&& jsonObject) noexcept{
542543
type_ = JsonType::OBJECT;
543544
content_ = std::move(jsonObject);
545+
jsonObject.clear();
544546
}
545547

546548

@@ -561,6 +563,7 @@ namespace Jsonlib{
561563

562564
type_ = JsonType::ARRAY;
563565
content_ = std::move(jsonArray);
566+
jsonArray.clear();
564567
return *this;
565568
}
566569

@@ -581,6 +584,7 @@ namespace Jsonlib{
581584

582585
type_ = JsonType::OBJECT;
583586
content_ = std::move(jsonObject);
587+
jsonObject.clear();
584588
return *this;
585589
}
586590

@@ -746,8 +750,8 @@ namespace Jsonlib{
746750
JsonValue& JsonValue::at(const std::string& key) {
747751
if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not Object.\n" };
748752
auto& map = std::get<JsonObject>(content_);
749-
if (map.find(key) == map.end()) throw std::out_of_range{ + "Key not find.\n" };
750-
return map.at(key);
753+
if (auto it = map.find(key); it == map.end()) throw std::out_of_range{ + "Key not find.\n" };
754+
else return it->second;
751755
}
752756
// 列表访问,可能创建新元素
753757
JsonValue& JsonValue::operator[](const size_t& index) {

0 commit comments

Comments
 (0)