The Wayback Machine - https://web.archive.org/web/20240717024344/https://www.geeksforgeeks.org/cpp-23-library-spanstream-header/
Open In App

C++23 Library – <spanstream> Header

Last Updated : 06 May, 2023
Improve
Suggest changes
Post a comment
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>
<h2>Class Templates in <spanstream></h2>
<p>Following is the list of classes defined inside the <spanstream> header file:</p>
<ol>
<li><strong>basic_spanbuf</strong></li>
<li><strong>basic_ispanstream</strong></li>
<li><strong>basic_ospanstream</strong></li>
<li><strong>basic_spanstream</strong></li>
<li><strong>spanbuf</strong></li>
<li><strong>wspanbuf</strong></li>
<li><strong>ispanstream</strong></li>
<li><strong>wispanstream</strong></li>
<li><strong>spanstream</strong></li>
<li><strong>wspanstream</strong></li>
</ol>
<p>All these classes have their own member functions and work. </p>
<h2>std::spanstream</h2>
<p>The std:;spanstream is a typedef name defined for <strong>basic_spanstream<char></strong> 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.</p>
<h2>Example of <spanstream></h2>
<p>The following C Program demonstrates the use of spanstream class:</p>
<div 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.



Similar Reads

random header in C++ | Set 1(Generators)
This header introduces random number generation facilities. This library allows to produce random numbers using combinations of generators and distributions. Generators: Objects that generate uniformly distributed numbers.Distributions: Objects that transform sequences of numbers generated by a generator into sequences of numbers that follow a spec
11 min read
clocale header file in C++
clocale: This header file contains declaration of a set of functions and a type for internationalization support tasks. It supports date format or country specific currency symbols. For example, date/time formatting, monetary formatting and many more. Methods in clocale header: localeconv(): This function returns an object which represents numeric
2 min read
C++ 11 - &lt;cfenv&gt; Header
C++ 11 comes with new features and improvements and one of the main addition is the &lt;cfenv&gt; header that helps the programmers or developers with great control over the floating-point environment. It governs critical features of floating-point arithmetic like rounding modes and exceptions. &lt;cfenv&gt; gives programmers the tools they need to
4 min read
C++ 20 - &lt;latch&gt; Header
In C++20, the header was introduced, which allows a synchronization primitive known as a latch. Latches allow one or more threads to wait until a certain number of operations have been finished before proceeding. A synchronization object called a latch is initialized with a preset count value. This object enables threads to pause until the count is
4 min read
C++ 11 - &lt;ratio&gt; Header
The header file, brought about by C++11, offers a class template for ratios and a collection of aliases to represent the same. The purpose of this is to enable the definition and manipulation of ratios while also providing a means to express the links between diverse units of measurement. Ratio Header in C++ The Ratio class is a template class that
3 min read
Use of "stdafx.h" header in C++ with examples
A header file contains the set of predefined standard library functions. The header file can be included in the program with the C preprocessing directive "#include". All the header files have ".h" extension. Syntax: #include &lt;header_file&gt; / "header_file" Here, #: A preprocessor directiveheader_file: Name of the header file The #include tells
3 min read
C++ 23 - &lt;flat_set&gt; Header
&lt;flat_set&gt; is a header file that provides the implementation of the std::flat_set container class in C++, which is a sorted associative container that stores unique elements and allows for fast access, insertion, and removal of elements. The flat_set is a container similar to the set but its elements are stored contiguously in memory. This ca
10 min read
C++ 20 - &lt;numbers&gt; Header
C++20 has a recently developed header file labeled that incorporates mathematical functions and constants. Its purpose is to provide standard library support for mathematical operations, simplifying the process for C++ programmers to incorporate mathematical functions and constants into their programming. Our focus in this essay is the header file
3 min read
C++17 - &lt;memory_resource&gt; Header
C++17 introduced the &lt;memory_resource&gt; header, which provides a mechanism for customizing the allocation and deallocation of memory in C++ programs. This header defines the memory_resource class and several derived classes that implement different memory allocation strategies. The C++ Standard Library has incorporated the header which can be
4 min read
C++ 11 - &lt;cinttypes&gt; Header
The header offers type aliases and functions for manipulating integer types with particular sizes and signedness, as a part of the C++11 standard. Its aim is to present a standard group of integer types across diverse platforms and architectures. &lt;cinttypes&gt; header The &lt;cinttypes&gt; header presents numerous type aliases that are utilized
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg