
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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