I don't know why there is no display to screen with the number one .
The iterator is equal to one, and 1 is > that 0. Please Explain it to me.
int main()
{
bool hola;
vector<bool> v;
vector<bool>:: iterator it = v.begin();
v.push_back (hola);
cout << "numero tamano: " << int(v.size()) << endl;
int i = int(v.size());
if(i>0)
}
cout << *it << " this";
}
cout << "fin\n";
return 0;
}
it only output by screen is this:
numero tamano: 1
You initialize your iterator before your push back. That way your iterator is pointing to somewhere you don't have access Thats why you get a segmentation You will have to initialize the iterator after pushback. It will work fine.
bool hola;
vector<bool> v;
v.push_back (hola);
vector<bool>:: iterator it = v.begin();
cout << "numero tamano: " << int(v.size()) << endl;
int i = int(v.size());
while(it!=v.end())
{
cout << *it << " this";
it++;
}
You save an iterator in the line
vector<bool>:: iterator it = v.begin();
Then you perform an operation that (potentially (probably)) invalidates iterators (may not if you had previously called .reserve
- but you didn't)
v.push_back (hola);
Then you use the stored (now potentially (probably) invalid) iterator
cout << *it << " this";
Don't do that. Accessing an invalidated iterator is Undefined Behaviour and your entire program now has no meaning.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.