Open In App

C++23 Library – <spanstream> Header

Last Updated : 06 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The <spanstream> header is a new addition to C++ 23 Standard Libraries Collection. It provides fixed character buffer streams for input and output.

It is a collection of classes and function templates that let you manipulate letter stretch as if they were streams, much like <stringstream> or <istringstream>. However, it works with std::span<char> rather than pulling from or writing to a string or buffer.

The following image illustrates the inheritance diagram of the <spanstream> header file:

inheritance diagram of <spanstream>” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20230417170207/cpp23-spanstream-library.webp 1000w, https://media.geeksforgeeks.org/wp-content/uploads/20230417170207/cpp23-spanstream-library-100.webp 100w, https://media.geeksforgeeks.org/wp-content/uploads/20230417170207/cpp23-spanstream-library-200.webp 200w, https://media.geeksforgeeks.org/wp-content/uploads/20230417170207/cpp23-spanstream-library-300.webp 300w, https://media.geeksforgeeks.org/wp-content/uploads/20230417170207/cpp23-spanstream-library-660.webp 660w, https://media.geeksforgeeks.org/wp-content/uploads/20230417170207/cpp23-spanstream-library-768.webp 768w, ” sizes=”100vw” width=”1000″><figcaption> </figcaption></figure>
<h2>Syntax to include <spanstream></h2>
<pre>#include <spanstream></pre>
<p>The above statement imports all the function and class templates of the <spanstream> header in the std namespace of our program.</p><div id=

Class Templates in <spanstream>

Following is the list of classes defined inside the <spanstream> header file:

  1. basic_spanbuf
  2. basic_ispanstream
  3. basic_ospanstream
  4. basic_spanstream
  5. spanbuf
  6. wspanbuf
  7. ispanstream
  8. wispanstream
  9. spanstream
  10. wspanstream

All these classes have their own member functions and work. 

std::spanstream

The std:;spanstream is a typedef name defined for basic_spanstream<char> that is nothing but a span-based string I/O buffer. We can initialize the objects of this class using std::span objects and then we can use it like a separate string buffer for input and output.

Example of <spanstream>

The following C Program demonstrates the use of spanstream class:

C++




// C Program to demonstrate the use of spanstream
#include <iostream>
#include <spanstream>
 
int main()
{
    // A string with some data
    std::string data = "3.45 5.67 4.64";
 
    // A span<char> from the string
    std::span<char> inSpan(data);
 
    // creating a spanstream object initialized with spn
    std::spanstream inSpanStream(inSpan);
 
    // Use the >> operator to read three double values from
    // the spanstream
    double geeks_double1, geeks_double2, geeks_double3;
    inSpanStream >> geeks_double1 >> geeks_double2
        >> geeks_double3;
 
    // Print out the value of double1, double2 and double3
    std::cout << "geeks_double1 = " << geeks_double1
              << "\n";
    std::cout << "geeks_double2 = " << geeks_double2
              << "\n";
    std::cout << "geeks_double3 = " << geeks_double3
              << "\n";
 
    // Another span container with test string data
    std::string data_t = "This is a test";
    std::span<char> outSpan(data_t);
 
    //  A spanstream from the span<char>
    std::spanstream outSpanstream(outSpan);
 
    // Use the << operator to write data to the spanstream
    // just like normal stream
    outSpanstream << "GeeksforGeeks ";
 
    // Print out the data stored in outSpanstream
    std::cout << "Result: " << outSpanstream.rdbuf()
              << "\n";
 
    return 0;
}


Output

geeks_double1 = 3.45
geeks_double2 = 5.67
geeks_double3 = 4.64
Result: GeeksforGeeks 

In the above example, we have used spanstream case to perform both input and output using >> and << operators.

Note: The above code can only be run on the compilers with C++ 23 standard support such as GCC 12 and MSVC 19.31 and onwards.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads