0

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";
}
10
  • 1
    *it, an iterator itself is not true or false it points to an element of the container that may be true or false, also your iterator shouldnt be an int Commented Dec 2, 2021 at 14:26
  • int it = Verification.begin() should be a compile error too. Commented Dec 2, 2021 at 14:26
  • I don't think the error is the operator. You are declaring it as int but trying to put an iterator in it. Change int to vector<bool>::iterator. Although, personally I prefer this style- for(size_t i = 0; i < vector_name.size(); i++) Commented Dec 2, 2021 at 14:27
  • Aside: *it==true is the same as *it, is the same as (*it == true) == true. Just use the bool value directly Commented Dec 2, 2021 at 14:28
  • 1
    Read the introduction to iterators in your favourite C++ book. Commented Dec 2, 2021 at 14:36

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer works great, side question what does bool b : Verification do
@Omar click on that blue link range-for and everything will be clear
2

There are various ways to iterate over an std::vector

  1. Using iterator

Long example:

for( std::vector<bool>::iterator it = v.begin(); it != v.end(); ++it ) std::cout << *it;

or the same but shorter:

for( auto it = v.begin(); it != v.end(); ++it ) std::cout << *it;
  1. Using index

Here:

for( unsigned int i = 0; i != v.size(); ++i ) std::cout << v[i];
  1. Range loop

Here:

for( bool b : v ) std::cout << b;

(there are some more but we will omit them for clarity)

Looks like you mixed 1 and 2 hense you have compilation errors. Choose one.

Comments

1

The problem is that Verification.begin() gives you an iterator while it is an int.

To solve this you could modify your for loop to:

for (std::vector<bool>::iterator it = Verification.begin(); it != Verification.end(); it++) {   
                    if (*it==true) cout<<"true";
                    else if (*it == false) cout<<"false";
}

Note *it means we're dereferencing the iterator it and then comparing the result.

Also you don't need the else if because you can just use else.

Alternative solution

You can also use a range-base for loop as shown below:


for (bool element : Verification) 
{
    std::cout << std::boolalpha << element;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.