Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read data from a Vector

How can I use the following vector to read true/false from using a while or for loop.
With this implemtation of the loop I get an error for the oprator !=

no operator "!=" matches these operands

vector<bool> Verification;

Verification.push_back(true);
Verification.push_back(false);
Verification.push_back(true);
Verification.push_back(false);
Verification.push_back(true);

for (int it = Verification.begin(); it != Verification.end(); it++) {   
                    if (it==true) cout<<"true";
                    else if (it == false) cout<<"false";
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are declaring it as the wrong type. The result of Verification.begin() is a std::vector<bool>::iterator. But you don’t need to specify that.

Use a range-for loop instead

for (bool b : Verification) 
{
    std::cout << std::boolalpha << b;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading