Minimal Reproducible example:
using namespace std;
#include<string>
#include<string_view>
#include<iostream>
int main()
{
string s = "someString";
string_view sV = string_view(s);
string_view sVTwo = string_view(begin(s), end(s));
return 0;
}
Is the time complexity of creating the string_view sV linear relative to the number of chars in the string s or is it irrelevant how long the string s is? Likewise, is the time complexity of the construction of sVTwo linear or constant depending on how many chars are in the string?
The reason I am confused is I cannot tell which of these constructors here: https://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view is being used to construct the string.
std::stringknows its own length since it has aconstsize()function. It's when a rawchar *is used where the length of the string comes into play, since a character pointer knows nothing about the length of the string, and must iterate until a null is reached.string_viewconstructors. It's using the "stringtostring_view" conversion operator.using namespace std;above#includedirectives, and ideally never useusing namespace, instead import specific type-names.