std::istrst...::istrstream(3)

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

🌸 NAME 🌸

Bestie, welcome to std::istrst...::istrstream(3) from the C++ Standard Libary. The main character today is std::istrstream::istrstream β€” literally std::istrstream::istrstream, name matching name, constructor energy for days. πŸ’…

✨ Synopsis ✨

explicit istrstream( const char* s );           (1) (deprecated in C++98)
                                                     (removed in C++26)
explicit istrstream( char* s );                 (2) (deprecated in C++98)
                                                     (removed in C++26)
istrstream( const char* s, std::streamsize n ); (3) (deprecated in C++98)
                                                     (removed in C++26)
istrstream( char* s, std::streamsize n );       (4) (deprecated in C++98)
                                                     (removed in C++26)

πŸŽ€ Description πŸŽ€

Okay so these constructors build a fresh std::istrstream and its underlying std::strstreambuf. The underlying std::strstreambuf is lowkey doing the real work here. 🫢

  1. For (1) and (2): constructs the underlying std::strstreambuf by calling strstreambuf(s, 0), then initializes the base class with the address of the strstreambuf. Behavior is undefined if s is not pointing at an element of a null-terminated array. Not me warning you dramatically, but also yes. 😭
  2. For (3) and (4): constructs the underlying std::strstreambuf by calling strstreambuf(s, n), then initializes the base class with the address of the strstreambuf. Behavior is undefined if s is not pointing at an element of an array whose length is at least n elements.

🌸 Parameters 🌸

  • s β€” C-string or char array to use as the contents of the stream.
  • n β€” size of the array.

πŸ’… Example πŸ’…

// Run this code

#include <iostream>
#include <strstream>

int main()
{
    std::istrstream s1("1 2 3"); // string literal
    int n1, n2, n3;
    if (s1 >> n1 >> n2 >> n3)
        std::cout << n1 << ", " << n2 << ", " << n3 << '\n';

    char arr[] = {'4', ' ', '5', ' ', '6'};
    std::istrstream s2(arr, sizeof arr);
    if (s2 >> n1 >> n2 >> n3)
        std::cout << n1 << ", " << n2 << ", " << n3 << '\n';
}

πŸ“ Output: πŸ“

1, 2, 3
4, 5, 6

πŸ¦‹ See also πŸ¦‹

  • constructor β€” constructs a strstreambuf object (public member function of std::strstreambuf)
  • constructor β€” constructs an ostrstream object, optionally allocating the buffer (public member function of std::ostrstream)
  • constructor β€” constructs a strstream object, optionally allocating the buffer (public member function of std::strstream)

That’s the whole vibe check, bestie: this iconic page came from http://cppreference.com, dated 2024.06.10, for std::istrst...::istrstream(3). She’s deprecated in C++98 and removed in C++26 β€” legendary, but not forever. πŸ’–βœ¨

πŸ“¦ where this page comes from