Šimon Tóth · @simontoth
268 followers · 795 posts · Server hachyderm.io

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: compiler-explorer.com/z/5Gbo53

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
268 followers · 793 posts · Server hachyderm.io

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: compiler-explorer.com/z/PEvznc

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
268 followers · 792 posts · Server hachyderm.io

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: compiler-explorer.com/z/eh5noa

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
267 followers · 789 posts · Server hachyderm.io

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: compiler-explorer.com/z/9rd5ne

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
267 followers · 788 posts · Server hachyderm.io

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: en.wikipedia.org/wiki/H-index.

Your solution should run in O(n).

Solve it yourself: compiler-explorer.com/z/5K5Wv1
Solution: compiler-explorer.com/z/9efEYc

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
267 followers · 787 posts · Server hachyderm.io

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: compiler-explorer.com/z/aah5fW

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
267 followers · 784 posts · Server hachyderm.io

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: compiler-explorer.com/z/oYYT35
Solution: compiler-explorer.com/z/7ffPvc

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
266 followers · 783 posts · Server hachyderm.io

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: compiler-explorer.com/z/xdvT44

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
266 followers · 782 posts · Server hachyderm.io

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: compiler-explorer.com/z/jEsW9G

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
265 followers · 778 posts · Server hachyderm.io

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: compiler-explorer.com/z/cn6Yde

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
265 followers · 777 posts · Server hachyderm.io

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: compiler-explorer.com/z/adK95j

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
264 followers · 775 posts · Server hachyderm.io

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: compiler-explorer.com/z/7baj15

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
264 followers · 772 posts · Server hachyderm.io

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: compiler-explorer.com/z/31nxxG
Solution: compiler-explorer.com/z/8EM8vG

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
264 followers · 756 posts · Server hachyderm.io

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: compiler-explorer.com/z/Tar5TT

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
264 followers · 754 posts · Server hachyderm.io

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: compiler-explorer.com/z/q59nzr

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
263 followers · 745 posts · Server hachyderm.io

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: compiler-explorer.com/z/M713vn

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
265 followers · 724 posts · Server hachyderm.io

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: compiler-explorer.com/z/3GhjWr
Solution: compiler-explorer.com/z/Go5xd1

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
265 followers · 717 posts · Server hachyderm.io

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: compiler-explorer.com/z/444vWT

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
264 followers · 715 posts · Server hachyderm.io

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: compiler-explorer.com/z/7E5bjq
Solution: compiler-explorer.com/z/eKqcYP

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago

Šimon Tóth · @simontoth
264 followers · 709 posts · Server hachyderm.io

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: compiler-explorer.com/z/eYbjoq

#cpp #cplusplus #coding #programming #dailybiteofcpp

Last updated 1 year ago