Read the sequence of hexadecimal values ​​from ifstream using std :: copy

Consider a file containing a sequence of integers expressed in hexadecimal notation. I can pass them as follows:

using namespace std;
ifstream infile(fname);
unsigned int i;
vector<unsigned int> vals;
while (infile >> std::hex >> i){
    vals.push_back(i);
}

What if I want to do the same with istream_iterator?

/// borks on hex:
copy(istream_iterator<unsigned int>(infile),
    istream_iterator<unsigned int>(), back_inserter(ref_data));

Is there any way to tell istream_iteratorhow to use hexadecimal notation?

+4
source share
1 answer

Same:

copy(istream_iterator<unsigned int>(infile >> std::hex),
     istream_iterator<unsigned int>(),
     back_inserter(ref_data));
+6
source

All Articles