std::basic_...stream::view(3)

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

πŸŽ€ NAME πŸŽ€

okay so bestie, this is the std::basic_...stream::view(3) page from the C++ Standard Libary manual, and it's here to explain one very specific little member function πŸ’–. The name is std::basic_stringstream::view - std::basic_stringstream::view β€” yes, it says the same thing twice, and honestly? iconic. slay. periodt.

✨ Synopsis ✨

This is the signature, and it's been around since C++20:

std::basic_string_view<CharT, Traits> view() const noexcept;

(since C++20) β€” yep, that's the vibe check on the version.

It obtains a std::basic_string_view over the underlying string object. Equivalent to:

return rdbuf()->view();

That's highkey the whole equivalence, no cap.

🌸 Parameters 🌸

(none) β€” literally none, not a single parameter. the vibes are lowkey minimal and that's fine.

πŸ¦‹ Return value πŸ¦‹

A std::basic_string_view over the underlying string object. That's the return, and it's giving short and sweet β€” not me being obsessed with a one-line return πŸ’….

πŸ”₯ Example πŸ”₯

The example literally starts with // Run this code, so I'm just gonna present it exactly as the page does. Run it, bestie 🫢:

        #include <iostream>
        #include <sstream>

        int main()
        {
            // input/output stream
            std::stringstream buf1;
            buf1 << 69;
            int n = 0;
            buf1 >> n;
            std::cout << "1) buf1 = [" << buf1.view() << "], n = " << n <<
       '\n';

            // output stream in append mode
            std::ostringstream buf2("test", std::ios_base::ate);
            buf2 << '1';
            std::cout << "2) buf2 = [" << buf2.view() << "]\n";

            // input stream
            std::istringstream inbuf("-42");
            inbuf >> n;
            std::cout << "3) inbuf = [" << inbuf.view() << "], n = " << n <<
       '\n';
        }

🌈 Output 🌈

Here's the output, exactly:

1) buf1 = [69], n = 69
2) buf2 = [test1]
3) inbuf = [-42], n = -42

πŸ‘‘ See also πŸ‘‘

  • view β€” obtains a view over the underlying character sequence (C++20) (public member function of std::basic_stringbuf<CharT,Traits,Allocator>)

k bye bestie 🫢 this came from cppreference.com, version/date 2024.06.10, for std::basic_...stream::view(3) β€” mwah πŸ’‹ iykyk

πŸ“¦ where this page comes from