std::match_results::size(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

okay bestie, welcome to std::match_results::size(3) from the C++ Standard Libary β€” yes, that’s the manual name, typo and all 🫢 this page is about the tiny little count queen of std::match_results, and she’s serving main character energy πŸ’…

🌸 NAME 🌸

std::match_results::size - std::match_results::size πŸ’… β€” yes bestie, the NAME line said it twice and meant it, iconic repetitive behavior, we love a consistent brand πŸ’–

πŸŽ€ Synopsis πŸŽ€

okay so the signature is highkey short and C++11-coded:

size_type size() const noexcept;  (since C++11)

Returns the number of submatches, i.e. std::distance(begin(), end()) β€” literally just the distance from begin() to end(), no cap ✨

Returns 0 if *this does not represent the result of a successful match. Zero. None. The match said no πŸ’€

πŸ¦‹ Parameters πŸ¦‹

(none) β€” not a single parameter, she takes no arguments, she’s independent like that πŸ™ˆ

πŸ’œ Return value πŸ’œ

The number of submatches. That’s it. That’s the return value, bestie ✨

πŸ”₯ Complexity πŸ”₯

Constant. O(1) energy, periodt β€” no loops, no drama, just constant vibes ⭐

🌈 Example 🌈

okay so here’s the code, copy it exactly and watch the little count queen do her thing 🧚

// Run this code

#include <iostream>
#include <regex>
#include <string>

int main()
{
    std::regex re("a(a)*b");
    std::string target("aaab");
    std::smatch sm;

    std::cout << sm.size() << '\n';

    std::regex_match(target, sm, re);
    std::cout << sm.size() << '\n';
}

πŸ’– Output πŸ’–

0
2

two lines, zero then two, no cap πŸ“

πŸ‘‘ See also πŸ‘‘

  • begin β€” returns iterator to the beginning of the list of sub-matches (public member function)
  • cbegin β€” public member function
  • end β€” returns iterator to the end of the list of sub-matches (public member function)
  • cend β€” public member function

these are the sibling functions, the whole squad 🫢


mwah πŸ’‹ that’s the tea from http://cppreference.com, dated 2024.06.10, on the std::match_results::size(3) page β€” go count your submatches, bestie 🌟

πŸ“¦ where this page comes from