简体   繁体   中英

No type named iterator_category in std::vector

This code is giving an error :

vector<vector<bool> > revealed(vector<bool>(10, false), vector<bool>(10,false));

I am trying to define a 2D boolean array that has all elements as false by default.

The error is

.\stl_iterator_base_types.h|165|error: no type named 'iterator_category' in 'class std::vector<bool>'|

The error refers to line 165 of stl_iterator_base_types.h

162 template<typename _Iterator>
163 struct iterator_traits
164 {
165     typedef typename _Iterator::iterator_category iterator_category;
166     typedef typename _Iterator::value_type        value_type;
167     typedef typename _Iterator::difference_type   difference_type;
168     typedef typename _Iterator::pointer           pointer;
169     typedef typename _Iterator::reference         reference;
170 };

By the way, I am using Code::Blocks(with MinGW) as my IDE

You would initialize such a vector as follows

std::vector<std::vector<bool>> revealed(10, std::vector<bool>(10, false));

The reason is that the constructor overload you are trying to use for std::vector is

vector(size_type count, const T& value);

So you can see the first argument is the count, the second is the value. By this notion, you want the outer vector to be

vector(10, "vectors_of_length_10_wlth_all_false_values")
           ^

The way you signify the second argument is

std::vector<bool>(10, false)

The first argument for the outer vector is simply 10 because you want it to contain 10 of these vectors that have 10 false values.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM