C++ Library - <string_view>



The <string_view> header in C++, provides low-level functions and operations for handling strings efficiently, avoiding unnecessary copies, and passing substrings without creating temporary objects.

Reduces overhead by avoiding allocation and copying of strings, especially useful for large datasets or string manipulations. This header is part of the strings library.

Including <string_view> Header

To include the <string_view> header in your C++ program, you can use the following syntax.

#include <string_view>

Functions of <string_view> Header

Below is list of all functions from <string_view> header.

Comparison Operators

The comparison operators allows lexicographical comparisons between two views.

S.No Function & Description
1 operator==

This function checks if two objects are equal lexicographically.

2 operator!=

This function checks if two objects are not equal lexicographically.

3 operator<

This function checks if the first object is less than the second..

4 operator>

This function checks if the first object is greater than the second.

5 operator<=

This function checks if the first object is less than or equal to the second.

6 operator>=

This function checks if the first is greater than or equal to the second object.

7 operator<=>

A three way- comparison operator also known as spaceship operator that compares two objects lexicographically.

Usage of operator==

In the below example we are going to use, operator== to checks if two std::string_view objects contain the same text.

#include <iostream>
#include <string_view>
int main() {
    std::string_view view1 = "Hello";
    std::string_view view2 = "Hello";
    std::string_view view3 = "World";
    if (view1 == view2) {
        std::cout << "view1 and view2 are equal.\n";
    } else {
        std::cout << "view1 and view2 are not equal.\n";
    }
    if (view1 == view3) {
        std::cout << "view1 and view3 are equal.\n";
    } else {
        std::cout << "view1 and view3 are not equal.\n";
    }
    return 0;
}

Output

If we run the above code it will generate the following output

view1 and view2 are equal.
view1 and view3 are not equal.

Swap Function

S.No Function & Description
1 swap

This function swaps the values of two objects.

Swapping the Contents of Two Objects

In the below example we are going to use, swap() to exchange the values of two std::string_view objects

#include <iostream>
#include <string_view>
int main() {
    std::string_view view1 = "Apple";
    std::string_view view2 = "Banana";

    std::cout << "Before swap:\n";
    std::cout << "view1: " << view1 << "\n";
    std::cout << "view2: " << view2 << "\n";
    view1.swap(view2);

    std::cout << "\nAfter swap:\n";
    std::cout << "view1: " << view1 << "\n";
    std::cout << "view2: " << view2 << "\n";
    return 0;
}

Output

If we run the above code it will generate the following output

Before swap:
view1: Apple
view2: Banana

After swap:
view1: Banana
view2: Apple

Range Access Functions

The range access functions provides iterators to traverse the character sequence.

S.No Function & Description
1 begin & cbegin

These functions return an iterator to the beginning of a container or array.

2 end & cend

These functions return an iterator to the end of a container or array.

3 rbegin & crbegin

These functions returns a reverse iterator to the beginning of a container or array.

4 rend & crend

These functions returns a reverse end iterator for a container or array.

5 size & ssize

These functions returns the size of a container or array.

6 empty

This function checks whether the container is empty.

7 data

This function obtains the pointer to the underlying array.

Iterating Over the Characters

In the below example code we are going to use, begin and end to iterate over characters in a std::string_view.

#include <iostream>
#include <string_view>
int main() {
    std::string_view view = "Hello";
    std::cout << "Characters in view: ";
    for (auto it = view.begin(); it != view.end(); ++it) {
        std::cout << *it << ' ';
    }
    return 0;
}

Output

If we run the above code it will generate the following output

Characters in view: H e l l o 
Advertisements