GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::basic_string_view::data(3) C++ Standard Libary std::basic_string_view::data(3)

std::basic_string_view::data - std::basic_string_view::data


constexpr const_pointer data() const noexcept; (since C++17)


Returns a pointer to the underlying character array. The pointer is such that the
range [data(); data() + size()) is valid and the values in it correspond to the
values of the view.


(none)


A pointer to the underlying character array.


Constant.


Unlike std::basic_string::data() and string literals, data() may return a pointer to
a buffer that is not null-terminated. Therefore it is typically a mistake to pass
data() to a routine that takes just a const CharT* and expects a null-terminated
string.

// Run this code


#include <iostream>
#include <cstring>
#include <cwchar>
#include <string>
#include <string_view>
int main()
{
std::wstring_view wcstr_v = L"xyzzy";
std::cout << std::wcslen(wcstr_v.data()) << '\n';
// OK: the underlying character array is null-terminated


char array[3] = {'B', 'a', 'r'};
std::string_view array_v(array, sizeof array);
// std::cout << std::strlen(array_v.data()) << '\n';
// error: the underlying character array is not null-terminated


std::string str(array_v.data(), array_v.size()); // OK
std::cout << std::strlen(str.data()) << '\n';
// OK: the underlying character array of a std::string is always null-terminated
}


5
3


front accesses the first character
(C++17) (public member function)
back accesses the last character
(C++17) (public member function)
data returns a pointer to the first character of a string
(public member function of std::basic_string<CharT,Traits,Allocator>)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.