C++ | Abseil Containers














































C++ | Abseil Containers



Abseil Containers

Abseil provides a number of containers as alternative to STL containers. These containers are designed to be more efficient with a few exceptions, where STL can be more efficient.
1. Hash Tables
  • absl::flat_hash_map
  • absl::flat_hash_set
  • absl::node_hash_map
  • absl::node_hash_set
collectively, these tables are known as Swiss Tables, and are used as a replacement of std::unordered_map and std::unordered_set.

Advantages of using Abseil containers - 
  * Guarantees O(1) erase method by returning void instead of iterator.
  * Supports heterogeneous lookup.
  
Example code : 

#include<iostream>
#include<string>
#include<vector>

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"

int main() {
// creating a flat_hash_set

absl::flat_hash_set<
std::string> set1 = {"foo", "bar", "baz"};

std::cout << "The contents of set1 are : " << std::endl;
for(std::string s : set1) {
std::cout << s << " ";
}
std::cout << std::endl;


// creating flat_hash_map

absl::flat_hash_set<
int, std::string> map1 = {{1, "one"}, {2, "two"}, {3,"three"}};

std::cout << "The contents of map1 are : " << std::endl;
for(auto i : map1) {
std::cout << i.first << " " << i.second << std::endl;
}
std::cout << std::endl;


// different types of constructors
// 1. copy constructor (for set and map)

absl::flat_hash_set<
std::string> set2(set1);

std::cout << "The contents of copied set2 is : " << std::endl;
for(string s : set2) {
std::cout << s << " ";
}
std::cout << std::endl;


absl::flat_hash_map<
int, std::string> map2(map2);

std::cout << "The contents of copied map2 are : " << std::endl;
for(auto i : map2) {
std::cout << i.first << " " << i.second << std::endl;
}
std::cout << std::endl;


// move assignment operator (used for moving contents of source to destination)
// after this operation, set2 and map2 will become empty

absl::flat_hash_set<
std::string> set3(std::move(set2));

std::cout << "The contents of copied set2 is : " << std::endl;
for(string s : set2) {
std::cout << s << " ";
}
std::cout << std::endl;


absl::flat_hash_map<
int, std::string> map3(std::move(map2));

std::cout << "The contents of map3 are : " << std::endl;
for(auto i : map3) {
std::cout << i.first << " " << i.second << std::endl;
}
std::cout << std::endl;


return 0;
}


P.S.: we can implement node_hash_set and node_hash_map in the same way, as we've implemented flat_hash_set and flat_hash_map.

Output : 




More Articles of Anuj Maurya:

Name Views Likes
C++ | Find smallest interval 222 0
C++ | Lexicographic rank of string 95 0
C++ | Counting Rooms (CSES) 92 0
C++ | Building Roads (CSES) 78 0
C++ | High Score (CSES) 199 0
C++ | Collect maximum coin in grid 75 0
C++ | Is this k-palindrome ? 75 0
C++ | Shuffle the array elements 64 0
C++ | Fizz Buzz 81 0
C++ | Check if given points form square 59 0
C++ | Klee Algorithm to find union of line segment 87 0
C++ | Check if points are collinear 75 0
C++ | Check if given point lies inside triangle 82 0
C++ | Check if two line segment intersect 78 0
C++ | Number of ways to decode message 74 0
C++ || Minimise the subset difference 60 0
C++ | Count of subsets with given sum 67 24
C++ | Currency Arbitrage 97 22
C++ | Estimating the value of π (pi) using Monte-Carlo Simulation 57 18
C++ | Find the first missing number 75 16
C++ | Finding word in grid using DFS 66 11
C++ | Implementing pow(x, y) in logarithmic time 52 9
C++ | Sum of all factors of number 80 10
C++ | Building Teams (CSES) 69 21
C++ | Positions of anagram 47 12
C++ | Integer factorization 55 13
C++ | Primality test 63 14
C++ | Subset Sum Problem 64 12
C++ | Civil Time in Abseil 66 19
C++ | Abseil Time Duration 119 15
C++ | absl::Time 92 12
C++ | Introduction to Abseil Time library 81 9
C++ | absl::StripPrefix() and absl::StripSuffix() 79 8
C++ | absl::ConsumePrefix() and absl::ConsumeSuffix() 92 19
C++ | absl::string_view 71 13
C++ | absl::Substitute() | Formatting string in abseil 84 15
C++ | absl::StrJoin() | Yet another way of joining strings 116 7
C++ | String concatenation in Abseil - 2 | absl::StrCat() 103 10
C++ | absl::StrAppend() | String concatenation in abseil - 1 78 16
C++ | absl::StrSplit() 149 15
C++ | Appending to string and Writing to stream in Abseil 86 11
C++ | absl::PrintF(), absl::FPrintF() and absl::SNPrintF() 96 0
C++ | Abseil StrFormat() 70 27
C++ | Abseil flags Library 122 348
C++ | Abseil Numeric Library 86 65
C++ | Abseil Hash library 81 0
C++ | Abseil Containers 139 46
C++ Introduction to Abseil 88 13
CSES | Shortest Route II 251 47
CSES | Shortest Routes I 186 1

Comments