简体   繁体   中英

How to initialize a vector<bool> with values 01 repeating?

Can I initialize a vector<bool> to start with a sequence of 010101...etc? Or do I have to just initialize it with 0's and loop thru and change every other value to 1?

To expand on @zmbq's answer, use std::generate_n like this:

std::vector<bool> v;
v.reserve(desired_size);

bool b = true;
std::generate_n(std::back_inserter(v), desired_size, [&b]() { return (b = !b); });

You would use std::generate if the vector already had a size.

If you don't have a C++11 compiler...

std::vector<bool> v;
v.reserve(desired_size);

struct GenFn
{
   GenFn(bool b = true)
      : b(b)
   {
   }

   bool operator()() const
   {
      return (b = !b);
   }

private:
   bool b;
};

std::generate_n(std::back_inserter(v), desired_size, GenFn());

看一下generate

inspired by zmbq, I wrote this and it works:

#include <algorithm>
#include <vector>

bool fill01()
{
    static int val=1;
    val=++val%2;
    return val==0?false:true;
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<bool> ve(100);
    std::generate(ve.begin(), ve.end(), fill01);
    return 0;
}

Another possibility (code not tested, might be buggy). It's basically the same as mos's generate_n , but by a different route. It's probably obvious that by comparison to generate_n , this is only worth doing if you can get some other use out of flipflop_iterator :

struct flipflop_iterator : public std::iterator<std::forward_iterator_tag, bool> {
    bool offset; // make sure the first value is false
    size_t remaining;
    flipflop_iterator(size_t length) : offset(length % 2), remaining(length) {}
    flipflop_iterator &operator++() {
        --remaining;
        return *this;
    }
    flipflop_iterator operator++(int) {
        flipflop_iterator tmp(*this); 
        ++(*this); 
        return tmp; 
    }
    bool operator*() const { 
        return (remaining + offset) % 2;
    }
    bool operator==(const flipflop_iterator &rhs) const {
        return remaining == rhs.remaining;
    }
    bool operator!=(const flipflop_iterator &rhs) const {
        return !(*this == rhs);
    }
};

std::vector<bool> v(flipflop_iterator(100), flipflop_iterator(0));

You could get a performance improvement by making flipflop_iterator a random access iterator instead of a forward iterator, but I can't be bothered to write out all those member functions. In that case it would probably be better to build it out of a boost::counting_iterator and a boost::transform_iterator .

You can try something like this

#include<vector>
#include<iostream>
using namespace std;

int main()
{
    bool bool_array[] = {true,false,true,false,true,false,true,false};
    vector<bool> bVec;
    bVec.insert(bVec.begin(),bool_array,&bool_array[sizeof(bool_array)/ sizeof(*bool_array)]);
    for(int i=0;i<bVec.size();++i){
        cout<<"At pos :"<<i<<" Val is:"<<bVec[i]<<endl;
    }
    return 0;
}

And the output is

At pos :0 Val is:1
At pos :1 Val is:0
At pos :2 Val is:1
At pos :3 Val is:0
At pos :4 Val is:1
At pos :5 Val is:0
At pos :6 Val is:1
At pos :7 Val is:0

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