An empty brace {} as the end of a range

I am running Xcode, Yosemite.

After compiling the code but crashing at runtime, why?

I intentionally used "{}" in the second std :: copy as the "end of range".

I experimented with this code because of a working example that used "{}" as the "default constructed stream iterator as end of range".

So, why does this (see 2nd code) work, but this (1st code) failed?

#include <algorithm> #include <iterator> #include <vector> #include <iostream> using namespace std; int main() { vector<int> coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // copy the elements of coll1 into coll2 by appending them vector<int> coll2; copy (coll1.cbegin(), coll1.cend(), // source back_inserter(coll2)); // destination vector<int> coll3; copy (coll1.begin(), {}, back_inserter(coll3)); } 

The following code is from the second edition of the C ++ standard library.

The line with "// end of source" can be either "istream_iterator ()", or simply "{},"

both work because: quoted from book

β€œNote that with C ++ 11, you can pass empty curly braces instead of the standard constructed stream iterator as the end of the range. This works because the type of the argument that defines the end of the original range is inferred from the previous argument, which defines the beginning of the source range.

 /* The following code example is taken from the book * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" * by Nicolai M. Josuttis, Addison-Wesley, 2012 * * (C) Copyright Nicolai M. Josuttis 2012. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */ #include <iterator> #include <algorithm> #include <vector> #include <string> #include <iostream> using namespace std; int main() { vector<string> coll; // read all words from the standard input // - source: all strings until end-of-file (or error) // - destination: coll (inserting) copy (istream_iterator<string>(cin), // start of source {}, // end of source back_inserter(coll)); // destination // sort elements sort (coll.begin(), coll.end()); // print all elements without duplicates // - source: coll // - destination: standard output (with newline between elements) unique_copy (coll.cbegin(), coll.cend(), // source ostream_iterator<string>(cout,"\n")); // destination } 
+5
source share
1 answer

The first one failed because your iterator type is not stream_iterator .

For the stream_iterator case, the default constructor has a special meaning - EOF. The iterator representing the end of the container is not configured by default. (In practice, for simple containers, iterators can be just pointers).

The default iterators, with the exception of stream iterators, usually do not make much sense and do not have the semantics that you would like in this instance.

(Some other boost iterators, in addition to stream iterators, execute the same patterns as stream iterators).

+5
source

All Articles