Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is std::string an array of two iterators?

I do not understand the behavior of the following snippet.
How could this be happening?

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s = "apple";
  string foo = {s.begin(), s.end()};
  cout <<  foo << endl;
}

output:
apple

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Don’t confuse how an object is constructed over what it fundamentally is.

A constructor can, and will, take in all kinds of things. Quite often these arguments are converted in some way, transformed into the form that’s a more natural fit for the class in question.

In this case you’re constructing a string out of a range of characters, or in other words, an arbitrary substring. There are many other methods, including converting from char*, which is something you’ll see all the time:

std::string example = "example";

Here you can read that as "example is initialized with the value "example"".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading