How to make a dynamic ListBox

Hi , I have an issue with some code of mine, I am not sure what’s wrong but I think it has more to do with C++ then ImGui.
Just trying to build a dynamic LisBox().

std::array<const char*, 2> names_array;
for (int i = 0; i < names_array.size(); i++)
{
	std::string name = "Object ";
	name += std::to_string(i);		
	names_array[i] = name.c_str();
            /// from here names_array[i] gives "Object 0", then "Object 1" , logical.
}

std::cout<< "------- " << names_array[0] << "\n"; /// this prints "Object 1" weird !

ImGui::ListBox("listbox\n(single select)", &listbox_item_current, names_array.data(), 2, 4);

Two elements appear in the listBox , both of them are named “Object 1”.
I don’t get it.
Is there a prefered way to do this kind of thing with imGUI ?

You should not be using this signature of ListBox() if your data is not already formatted for it. Even if it will work with the right code, it’s still too much code and would be way too inefficient to recreate std::string every frame.

Use ListBoxHeader / ListBoxFooter.

I finally got it to work with ListBox() alone , but yes you’re right it’s much easier using ListBoxHeader() and ListBoxFooter()

Thanks a lot.
and Merry Christams to you btw :slight_smile: