Files
parallel-hashmap/examples/btree.cc
T

50 lines
1.3 KiB
C++
Raw Normal View History

#include <iostream>
#include "btree_fwd.h"
#include <parallel_hashmap/btree.h>
int main()
{
2023-04-23 19:43:18 -04:00
// initialise map with some values using an initializer_list
2019-12-31 12:29:45 -05:00
phmap::btree_map<std::string, int> map =
{ { "John", 35 },
{ "Jane", 32 },
{ "Joe", 30 },
};
2019-12-31 12:29:45 -05:00
// add a couple more values using operator[]()
map["lucy"] = 18;
map["Andre"] = 20;
2020-01-01 12:56:26 -05:00
auto it = map.find("Joe");
map.erase(it);
2019-12-31 12:29:45 -05:00
map.insert(std::make_pair("Alex", 16));
map.emplace("Emily", 18); // emplace uses pair template constructor
for (auto& p: map)
std::cout << p.first << ", " << p.second << '\n';
IntString map2; // IntString is declared in btree_fwd.h
2019-12-31 12:29:45 -05:00
map2.emplace(std::piecewise_construct, std::forward_as_tuple(0), std::forward_as_tuple(10, 'c'));
map2.try_emplace(1, 10, 'a'); // phmap::btree_map supports c++17 API
for (auto& p: map2)
std::cout << p.first << ", " << p.second << '\n';
// create a btree_set of tuples
using X = std::tuple<float, std::string>;
2019-12-31 12:29:45 -05:00
phmap::btree_set<X> set;
for (int i=0; i<10; ++i)
2019-12-31 12:29:45 -05:00
set.insert(X((float)i, std::to_string(i)));
set.emplace(15.0f, "15");
set.erase(X(1.0f, "1"));
2019-12-31 12:29:45 -05:00
for (auto& e: set)
std::cout << std::get<0>(e) << ", \"" << std::get<1>(e) << "\" \n";
return 0;
}