Constructor That Uses a Range

A constructor that uses a range uses an iterator-defined range in the style of the STL:

template<class InputIterator>
basic_string(InputIterator begin, InputIterator end,
                const Allocator& a = Allocator());

The begin iterator points to the element in the source at which copying begins, and end points to one past the last location to be copied.

You can use this form with arrays, strings, or STL containers:

char cole[40] = "Old King Cole was a merry old soul.";
string title(cole + 4, cole + 8);
vector<char> input;
char ch;
while (cin.get(ch) && ch != ' ')
    input.push_back(ch);
string str_input(input.begin(), input.end());

In the first use, InputIterator is evaluated to type const char *. In the second use, InputIterator is evaluated to type vector<char>::iterator.

The following relationships hold after the constructor is called:

• The data() method returns a pointer to the first element of a string formed by copying elements from the range [begin, end).

• The size() method returns the distance between begin and end. (The distance is measured in units equal to the size of data type obtained when the iterator is dereferenced.)

• The capacity() method returns a value at least as large as size().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.157