Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_UTIL_VECTOR_H6#define BITCOIN_UTIL_VECTOR_H8#include <functional>9#include <initializer_list>10#include <optional>11#include <type_traits>12#include <utility>13#include <vector>15/** Construct a vector with the specified elements.16*17* This is preferable over the list initializing constructor of std::vector:18* - It automatically infers the element type from its arguments.19* - If any arguments are rvalue references, they will be moved into the vector20* (list initialization always copies).21*/22template<typename... Args>23inline std::vector<std::common_type_t<Args...>> Vector(Args&&... args)24{25std::vector<std::common_type_t<Args...>> ret;26ret.reserve(sizeof...(args));27// The line below uses the trick from https://www.experts-exchange.com/articles/32502/None-recursive-variadic-templates-with-std-initializer-list.html28(void)std::initializer_list<int>{(ret.emplace_back(std::forward<Args>(args)), 0)...};29return ret;30}32/** Concatenate two vectors, moving elements. */33template<typename V>34inline V Cat(V v1, V&& v2)35{36v1.reserve(v1.size() + v2.size());37for (auto& arg : v2) {38v1.push_back(std::move(arg));39}40return v1;41}43/** Concatenate two vectors. */44template<typename V>45inline V Cat(V v1, const V& v2)46{47v1.reserve(v1.size() + v2.size());48for (const auto& arg : v2) {49v1.push_back(arg);50}51return v1;52}54/** Clear a vector (or std::deque) and release its allocated memory. */55template<typename V>56inline void ClearShrink(V& v) noexcept57{58// There are various ways to clear a vector and release its memory:59//60// 1. V{}.swap(v)61// 2. v = V{}62// 3. v = {}; v.shrink_to_fit();63// 4. v.clear(); v.shrink_to_fit();64//65// (2) does not appear to release memory in glibc debug mode, even if v.shrink_to_fit()66// follows. (3) and (4) rely on std::vector::shrink_to_fit, which is only a non-binding67// request. Therefore, we use method (1).69V{}.swap(v);70}72template<typename V, typename L>73inline std::optional<V> FindFirst(const std::vector<V>& vec, const L fnc)74{75for (const auto& el : vec) {76if (fnc(el)) {77return el;78}79}80return std::nullopt;81}83#endif // BITCOIN_UTIL_VECTOR_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.