13 os << ms.
a <<
" " << ms.
b;
25template <
typename MapType>
26void print_map(
const MapType& map,
const std::string& header) {
27 std::cout << header << std::endl;
28 for (
const auto& pair : map) {
29 std::cout <<
"Key: " << pair.first <<
", Value: " << pair.second << std::endl;
36 config.
db_path =
"example_struct.db";
43 map_db.
insert(1, {10, 1.1});
44 map_db.
insert(2, {20, 2.2});
45 map_db.
insert(3, {30, 3.3});
48 std::cout <<
"Number of keys in the database: " << map_db.
count() << std::endl;
51 std::cout <<
"Is the database empty? " << (map_db.
empty() ?
"Yes" :
"No") << std::endl;
55 if (map_db.
find(2, value)) {
56 std::cout <<
"Found value for key 2: " << value << std::endl;
58 std::cout <<
"Key 2 not found." << std::endl;
62 std::map<int, MyStruct> my_map;
64 print_map(my_map,
"Contents of my_map after using operator():");
67 std::map<int, MyStruct> my_map2;
69 print_map(my_map2,
"Contents of my_map2 after using load:");
72 std::map<int, MyStruct> all_entries = map_db.
retrieve_all<std::map>();
73 print_map(all_entries,
"Contents of database using retrieve_all:");
76 std::cout <<
"Number of keys in the database: " << map_db.
count() << std::endl;
81 print_map(all_entries,
"Contents of database after removing key 3:");
84 map_db.
insert(4, {40, 4.4});
86 print_map(all_entries,
"Contents of database after inserting key 4:");
89 my_map[5] = {50, 5.5};
92 print_map(all_entries,
"Contents of database after append:");
96 my_map[6] = {60, 6.6};
99 print_map(all_entries,
"Contents of database after reconcile:");
103 my_map[7] = {70, 7.7};
106 print_map(all_entries,
"Contents of database after using operator= to reconcile:");
109 std::cout <<
"Number of keys in the database: " << map_db.
count() << std::endl;
110 std::cout <<
"Is the database empty? " << (map_db.
empty() ?
"Yes" :
"No") << std::endl;
115 if (all_entries.empty()) {
116 std::cout <<
"Database is empty after clear." << std::endl;
118 print_map(all_entries,
"Contents of database after clear:");
122 std::cout <<
"Is the database empty? " << (map_db.
empty() ?
"Yes" :
"No") << std::endl;
Declaration of the KeyValueDB class for managing key-value pairs in a SQLite database.
void connect()
Connects to the database using the current configuration. Initializes a connection to the database by...
Configuration class for SQLite database settings.
std::string db_path
Path to the SQLite database file.
Template class for managing key-value pairs in a SQLite database.
std::size_t count() const
Returns the number of elements in the database.
void clear()
Clears all key-value pairs from the database.
void remove(const KeyT &key)
Removes a key-value pair from the database.
void reconcile(const ContainerT< KeyT, ValueT > &container)
Reconciles the database with the container.
void insert(const KeyT &key, const ValueT &value)
Inserts a key-value pair into the database.
ContainerT< KeyT, ValueT > retrieve_all()
Retrieves all key-value pairs.
bool empty() const
Checks if the database is empty.
bool find(const KeyT &key, ValueT &value)
Finds a value by key.
void append(const ContainerT< KeyT, ValueT > &container)
Appends data to the database.
void load(ContainerT< KeyT, ValueT > &container)
Loads data from the database into the container.
void print_map(const MapType &map, const std::string &header)
friend std::istream & operator>>(std::istream &is, MyStruct &ms)
friend std::ostream & operator<<(std::ostream &os, const MyStruct &ms)