How to Work with std::string and std::vector in Dear ImGui: A Complete Guide

Include misc/cpp/imgui_stdlib.h to enable native std::string and std::vector<std::string> support in Dear ImGui, allowing direct binding to InputText, Combo, and ListBox widgets.

Dear ImGui’s core API is designed around C-style character buffers (char*), but the ocornut/imgui repository provides official C++ Standard Library helpers that eliminate manual string management. By including a single header, you can bind modern C++ containers directly to ImGui widgets while maintaining the library’s immediate-mode efficiency. This guide covers the exact implementation details and usage patterns for integrating std::string and std::vector in Dear ImGui.

Enabling C++ Standard Library Support

To use std::string and std::vector seamlessly, you must include the optional utility header located in the repository’s misc/cpp/ directory. This file is not part of the core compilation unit and must be added explicitly to your project.

#include "imgui.h"
#include "misc/cpp/imgui_stdlib.h"  // Required for std::string overloads

Without this include, Dear ImGui falls back to its original C-style API, requiring you to manage buffer sizing and null termination manually. The overloads in imgui_stdlib.h are header-only implementations that wrap the core functionality in imgui.cpp.

Binding std::string to Input Fields

Once imgui_stdlib.h is included, you can pass std::string pointers directly to text input widgets. The implementation automatically handles buffer reallocation through an internal ImGuiTextBuffer when the user exceeds the current capacity.

Single-Line Input with InputText

static std::string player_name = "Player 1";
ImGui::InputText("Player Name", &player_name);

In misc/cpp/imgui_stdlib.h, this overload creates a temporary editing buffer that resizes the underlying std::string if the user types beyond the current length. You do not need to pre-allocate a fixed-size character array.

Multi-Line Text Editors

The same pattern applies to multiline editors:

static std::string document_content;
ImGui::InputTextMultiline("Document", &document_content, ImVec2(0, 300));

The widget handles newline characters and buffer growth automatically, converting between the internal char* representation and your std::string instance on every frame.

Working with std::vectorstd::string for Selection Widgets

The standard library helper provides convenient iteration utilities for std::vector<std::string>, eliminating the need to construct C-style string arrays or manual index tracking callbacks.

Combo Boxes from String Vectors

static std::vector<std::string> options = {"Low", "Medium", "High", "Ultra"};
static int selected_quality = 0;

ImGui::Combo("Quality", &selected_quality, options);

Internally, this overload iterates over the vector and presents each element as a selectable item. The selected_quality integer receives the index of the chosen element.

ListBox Implementation

For scrollable lists with multiple visible items:

static std::vector<std::string> inventory = {"Sword", "Shield", "Potion", "Key"};
static int selected_item = -1;

ImGui::ListBox("Inventory", &selected_item, inventory, 4);  // Display 4 items at once

The fourth parameter specifies the height in items, while the vector provides the string content dynamically.

How the Internal Buffer Management Works

According to the ocornut/imgui source code, the imgui_stdlib.h implementation creates a temporary ImGuiTextBuffer during the InputText call. When the user modifies the text, the helper compares the new length against the std::string capacity and calls std::string::resize() if necessary before copying the edited content back to your instance.

This approach maintains compatibility with the core API in imgui.h while providing zero-overhead abstractions for C++ projects. The vector helpers similarly adapt the underlying const char* expectations of Combo and ListBox by accessing std::vector::data() and iterating through elements.

Summary

  • Include misc/cpp/imgui_stdlib.h to enable modern C++ container support alongside imgui.h.
  • std::string overloads for InputText and InputTextMultiline automatically resize buffers during editing.
  • std::vector<std::string> helpers simplify Combo and ListBox population without manual C-array conversion.
  • These utilities are optional headers that wrap the core char* API in imgui.cpp, maintaining backward compatibility.

Frequently Asked Questions

Do I have to use imgui_stdlib.h to use std::string with Dear ImGui?

No, but without imgui_stdlib.h, you must manually manage char arrays and buffer sizes. The core API in imgui.h only accepts C-style strings, requiring you to handle memory allocation and null termination yourself.

How does Dear ImGui handle text resizing when using std::string?

The imgui_stdlib.h implementation creates a temporary editing buffer that monitors text length during the input frame. If the user input exceeds the current std::string capacity, the helper automatically calls resize() on your string instance before copying the final value back.

Can I use std::vectorstd::string with other widgets besides Combo and ListBox?

The standard helper specifically targets Combo and ListBox in imgui_stdlib.h. For other widgets requiring string lists, you can manually iterate over the vector and use the standard C++ indexing approach, or extend the helper pattern with custom callbacks.

Is imgui_stdlib.h officially maintained as part of Dear ImGui?

Yes, misc/cpp/imgui_stdlib.h is an official file in the ocornut/imgui repository located under the misc/cpp/ directory. It is maintained alongside the core library but kept separate to preserve C++ standard library independence for the main API.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →