Skip to content

Commit

Permalink
Merge pull request apache#1484 from serverglen/flat_map_example
Browse files Browse the repository at this point in the history
Add butil::FlatMap example in the doc
  • Loading branch information
zyearn authored Jul 19, 2021
2 parents 7606930 + 187ef43 commit 53f6436
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion docs/cn/flatmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,35 @@ FlatMap - Maybe the fastest hashmap, with tradeoff of space.

# EXAMPLE

`#include <butil/containers/flat_map.h>`
```c++
#include <string>
#include <butil/logging.h>
#include <butil/containers/flat_map.h>

void flatmap_example() {
butil::FlatMap<int, std::string> map;
// bucket_count: initial count of buckets, big enough to avoid resize.
// load_factor: element_count * 100 / bucket_count, 80 as default.
int bucket_count = 1000;
int load_factor = 80;
map.init(bucket_count, load_factor);
map.insert(10, "hello");
map[20] = "world";
std::string* value = map.seek(20);
CHECK(value != NULL);

CHECK_EQ(2UL, map.size());
CHECK_EQ(0UL, map.erase(30));
CHECK_EQ(1UL, map.erase(10));

LOG(INFO) << "All elements of the map:";
for (butil::FlatMap<int, std::string>::const_iterator it = map.begin(); it != map.end(); ++it) {
LOG(INFO) << it->first << " : " << it->second;
}
map.clear();
CHECK_EQ(0UL, map.size());
}
```

# DESCRIPTION

Expand Down

0 comments on commit 53f6436

Please sign in to comment.