The std::queue is a container adapter that implements the interface of a FIFO queue.
The options for the backing containers are std::deque and std::list.
Compiler Explorer link: https://compiler-explorer.com/z/5Gbo53hMx
#cpp #cplusplus #coding #programming #dailybiteofcpp
If we want to iterate over the elements of a bidirectional range in reverse order, we do not have to mutate it.
However, if we do want to mutate the range, we can use the std::reverse algorithm.
Note that std::list and std::forward_list provide a reverse method that reorders the nodes (instead of reversing the order of the values).
Compiler Explorer link: https://compiler-explorer.com/z/PEvznc99o
#cpp #cplusplus #coding #programming #dailybiteofcpp
Despite recent developments (<format> and <print>), iostreams will be with us for the foreseeable future.
One of the quirks of iostreams is their approach to error handling, with errors represented using flags and error states.
Compiler Explorer link: https://compiler-explorer.com/z/eh5noa66f
#cpp #cplusplus #coding #programming #dailybiteofcpp
Before C++20, using common mathematical constants relied on either POSIX or a compiler extension through the <math.h> header.
C++20 introduced a new <numbers> header that provides common mathematical constants as templates that can be specialized for user types. The standard library provides float, double and long double specialisations with an alias for the double variant.
Compiler Explorer link: https://compiler-explorer.com/z/9rd5neahn
#cpp #cplusplus #coding #programming #dailybiteofcpp
Tuesday common C++ interview problem: Calculate h-index.
Given information about research paper citations as std::vector<int>, where each element represents the number of citations for a paper, calculate the h-index: https://en.wikipedia.org/wiki/H-index.
Your solution should run in O(n).
Solve it yourself: https://compiler-explorer.com/z/5K5Wv1ha9
Solution: https://compiler-explorer.com/z/9efEYcor9
#cpp #cplusplus #coding #programming #dailybiteofcpp
The std::shared_future is a C++11 synchronization tool suitable for one-shot single-producer/many-consumers situations.
Unlike std::future, std::shared_future is copyable, allowing multiple instances of std::shared_future to refer to the same shared state.
Similar to std::future, std::shared_future<void> can be used for signalling.
Compiler Explorer link: https://compiler-explorer.com/z/aah5fWc4v
#cpp #cplusplus #coding #programming #dailybiteofcpp
Sunday common C++ interview problem: O(1) randomized container.
Implement a data structure that provides the following O(1) operations:
- insert one copy of a value (multiple copies allowed)
- remove one copy of a value
- get a random value with uniform distribution (taking into account the number of copies)
Solve it yourself: https://compiler-explorer.com/z/oYYT359df
Solution: https://compiler-explorer.com/z/7ffPvcMnT
#cpp #cplusplus #coding #programming #dailybiteofcpp
The std::lerp is a C++20 mathematical function that handles linear interpolation (and extrapolation) for floating-point types.
The function takes three arguments: the two boundary values and an interpolation factor. The implementation will correctly handle infinities and boundary values for the interpolation factor.
Compiler Explorer link: https://compiler-explorer.com/z/xdvT4434Y
#cpp #cplusplus #coding #programming #dailybiteofcpp
The std::stable_sort is a slower version of std::sort that additionally provides stability, i.e. equivalent elements maintain their relative positions.
This is important, notably for interactive cases when one range can be repeatedly sorted based on different aspects.
Compiler Explorer link: https://compiler-explorer.com/z/jEsW9Gzsf
#cpp #cplusplus #coding #programming #dailybiteofcpp
Besides filesystem exploration, the std::filesystem offers the typical file manipulation operations.
Each operation offers two variants, one throwing one that returns the potential error as an outparameter.
The following example relies on throwing versions of operations to minimize error handling.
Compiler Explorer link: https://compiler-explorer.com/z/cn6Yde1aY
#cpp #cplusplus #coding #programming #dailybiteofcpp
On top of the standard containers providing lexicographical comparison, the standard also provides two algorithms std::lexicographical_compare and std::lexicographical_compare_three_way (C++20) that can operate on any input range.
Compiler Explorer link: https://compiler-explorer.com/z/adK95j8os
#cpp #cplusplus #coding #programming #dailybiteofcpp
The std::transform_inclusive_scan and std::transform_exclusive_scan compute the inclusive/exclusive prefix sum from the results of a transformation applied to each element.
Unlike std::partial_sum, the prefix sum is generalized and not evaluated in strict order, requiring associative reduction operation for deterministic results.
Compiler Explorer link: https://compiler-explorer.com/z/7baj15Eve
#cpp #cplusplus #coding #programming #dailybiteofcpp
Sunday common C++ interview problem: Optimizing a conference experience.
Given a conference schedule as std::vector<Talk> (start, end, value), return the maximum total value you can obtain by attending at most k talks.
Start and end times are exclusive (i.e. the earliest start time for the next event is end+1).
Solve it yourself: https://compiler-explorer.com/z/31nxxG85W
Solution: https://compiler-explorer.com/z/8EM8vGj4x
#cpp #cplusplus #coding #programming #dailybiteofcpp
Standard C++ containers provide lexicographical comparison through the standard set of comparison operators (before C++20) and the three-way comparison operator (since C++20).
The support covers std::array, std::vector, std::deque, std::(forward_)list, std::string (and variants), std::(multi)set, std::(multi)map, std::stack and std::queue.
Compiler Explorer link: https://compiler-explorer.com/z/Tar5TTKna
#cpp #cplusplus #coding #programming #dailybiteofcpp
The C++17 std::invoke is a utility that can invoke any callable with the provided arguments. Note that this includes member functions and even members.
If we don't need the type erasure properties of std::function or std::move_only_function, std::invoke can be a lower-level alternative (with the callable and arguments statically deduced).
Compiler Explorer link: https://compiler-explorer.com/z/q59nzrzr4
#cpp #cplusplus #coding #programming #dailybiteofcpp
Before C++20, obtaining information about the source code location (line, file, function) required reliance on (sometimes non-portable) macros.
The std::source_location is a small C++20 utility that encapsulates source code location information in a C++ class. Note that the returned values are still implementation-defined.
Compiler Explorer link: https://compiler-explorer.com/z/M713vnW6f
#cpp #cplusplus #coding #programming #dailybiteofcpp
Tuesday common C++ interview problem: Merging intervals.
Given a list of potentially overlapping intervals (start and end represented by integers), merge all overlapping intervals and return the resulting list of non-overlapping intervals.
Intervals {a,b}, {b,c} are considered overlapping.
Solve it yourself: https://compiler-explorer.com/z/3GhjWrG6b
Solution: https://compiler-explorer.com/z/Go5xd1nKr
#cpp #cplusplus #coding #programming #dailybiteofcpp
The C++20 std::binary_semaphore is a specialization of the more general std::counting_semaphore that only supports two values, 0 and 1.
The main use case of a binary semaphore is for simple signalling, where the alternative approach would be to use the combination of std::mutex, std::condition_variable and a boolean variable.
Compiler Explorer link: https://compiler-explorer.com/z/444vWT94f
#cpp #cplusplus #coding #programming #dailybiteofcpp
Common C++ interview problem: Top k frequent values.
Given a list of integers as std::vector<int> and the count k, return the top k most frequent values in the list in any order.
You can assume that the input leads to a unique solution, and your solution is required to operate faster than O(n*logn).
Solve it yourself: https://compiler-explorer.com/z/7E5bjq8sb
Solution: https://compiler-explorer.com/z/eKqcYPxfT
#cpp #cplusplus #coding #programming #dailybiteofcpp
The std::array is a container that represents fixed-sized arrays. Besides the range interface, std::array also avoids the implicit decay into a pointer (e.g. int[3] into int*).
On top of that, std::array doesn't have explicit constructors, allowing it to maintain the trivially copyable property of the underlying data.
Compiler Explorer: https://compiler-explorer.com/z/eYbjoq76o
#cpp #cplusplus #coding #programming #dailybiteofcpp