c++ - Operator >> doesn't like vector<bool>? -
i have following code takes vector , writes file, , opens file , writes content different vector.
#include <fstream> #include <iostream> #include <vector> using namespace std; int main() { vector<bool> q, p; // ^^^^ q.resize(5, 0); q[0] = 1; q[2] = 1; q[4] = 1; ofstream ofile("file.log"); (int = 0; i<5; i++) ofile <<q[i]<<" "; ofile.close(); ifstream ifile("file.log"); p.resize(5); int = 0; // vvvvvvvvvvvv while(ifile>> p[i]) { cout <<i<<"\t"<<p[i]<<endl; i++; } ifile.close(); return 0; }
what noticed code compiles , runs no problem when vector contains double, int, , long data types, produces error if changed bool. error message get:
../src/timeexample.cpp:31: error: no match ‘operator>>’ in ‘ifile >> p.std::vector<bool, _alloc>::operator[] [with _alloc = std::allocator<bool>](((long unsigned int)i))’
so, know why happens?
thank you
std::vector<bool>
specialized have space efficiency. operator[]
wouldn't able return addressable variable, returns std::vector<bool>::reference
proxy object instead. input temporary variable , transfer it:
bool b; while (ifile >> b) { p[i] = b; cout <<i<<"\t"<<b<<endl; i++; }
Comments
Post a Comment