I was reading Chapter 9 of the 2nd edition of A Tour of C++ by Bjarne Stroustrup and was puzzled by the use of {&king[0],2}
where king
is a string
variable. I get that it returns the first 2 strings but I don't know what the name is for this to look up more details about it like:
- Does the
&
ampersand indicate that it is a reference or does it switch it to pointer? - In what version was this feature introduced? I know the book is based on C++17 but don't know what version began supporting it.
// Strings and Regular Expressions
#pragma once
#include <string>
using namespace std;
string cat(string_view sv1, string_view sv2) {
string res(sv1.length()+sv2.length(), '\0');
char* p = &res[0];
for (char c : sv1) // one way
*p++ = c;
copy(sv2.begin(), sv2.end(), p); // another way
return res;
}
void tryCat() {
string king = "Harold";
auto s1 = cat(king, "William"); // string and const char*
auto s2 = cat(king, king); // string and string
auto s3 = cat("Edward", "Stephen"sv); // const char* and string_view
auto s4 = cat("Canute"sv, king);
auto s5 = cat({&king[0],2}, "Henry"sv); // HaHenry
auto s6 = cat({&king[0],2}, {&king[2],4}); // Harold
cout << s6 << endl;
}
int main () {
tryCat();
}