Array and Vectors

Hi!
Im using imGui library and one object takes as an argument an array bool b [] but in order to use in a interactive way i need that array to be fized size according a vector. so i need that bool b [] size be equal to vector size. its possible ?

bool b [] = {false, false}}; // need this fixed size

        for(int i = 0; i <vector.size; i ++){
            if (i % 5 != 0) ImGui::SameLine();
            
            ImGui::PushID(i);
            if(ImGui::Checkbox("##", &b[i])){
            }
            ImGui::PopID();
        }

Hi, usually you can easily convert convert from a vector to an array just by pointing to the vector’s first element, but bool vectors are a specialized type that optimizes space making it impossible to transform these into arrays these without copying the data.

although you can do it like this

vector<bool> vb; // your vector
auto arr = std::unique_ptr<bool[]>(new bool[vb.size()]);
std::copy(std::begin(vb), std::end(vb), arr.get());

arr.get() will return the actuall array.

Hello! thanks for answer! i really dont know too much about all that kind of stuff about c++.
but i think i explained wrong.

the ImGui::Checkbock object takes as second argument an array of boolean.


for(int i = 0; i < vector.size(); i++){
           ImGui::PushID(i);
            if(ImGui::Checkbox("##", &b[i])){
            }
}

so i need define b as

bool b [] = {0,0....etc}

the perfect way to fixed my problem will be if i be able to put a vector instead of an array. but this isnt possible.

So i`ve a vector of objects (lets say shapes) and i need for each one make a new CheckBox. cos i dont know how size this vector can be… im try to make a " [] fixed size"… maybe im wrong. .

also, how can i access to arr.size to iterate? i cant acces others [] size…

thanks a lot

EDIT:: finally i could make it work.
i made an extra array to get the boolean values that i need, so then i copy to arr.

Right, that’s what I meant. you need to make an intermediate array just for passing it to the CheckBox function.
I dont use ImGui much so I might be wrong, but the ImGui::Checkbox function expects just a pointer to a bool rather than a pointer to an array. Maybe I am wrong. Because from the code you’ve posted at each iteration you are passing the ponter to each member of the vector, so it wont be necesary to convert into an array.