I noticed some weird behavior of c++ vector constructor, can anybody explain it for me? Thanks.
Code snippet 1 works:
#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include <iterator>
#include <sstream>
#include <algorithm>
using namespace std;
int main()
{
string str = "The quick brown fox";
stringstream strstr(str);
istream_iterator<string> start(strstr);
vector<string> results(start, istream_iterator<string>());
ostream_iterator<string> oit(cout, "\n");
copy(results.begin(), results.end(), oit);
}
but code snippet 2 doesn't:
#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include <iterator>
#include <sstream>
#include <algorithm>
using namespace std;
int main()
{
string str = "The quick brown fox";
stringstream strstr(str);
vector<string> results(istream_iterator<string>(strstr), istream_iterator<string>());
ostream_iterator<string> oit(cout, "\n");
copy(results.begin(), results.end(), oit);
}
The only difference is the first parameter in the constructor of the vector.