简体   繁体   English

如何获取 C++ 中字符串大小的 const 值?

[英]How can I get a const value for the size of a string in C++?

I need to be able to use ImGui textboxes, however they don't take const char* or std::string so I need to convert a string to a char array.我需要能够使用ImGui文本框,但是它们不使用const char*std::string所以我需要将字符串转换为char数组。 The problem with this is, however, that I need my char array to be the same size as the string (+1).但是,问题在于我需要我的char数组与字符串 (+1) 的大小相同。 I get an error saying it needs to be constant value in declaration but I need to be able to access the string's size and make a variable that holds that value as constant.我收到一个错误,说它在声明中需要是常量值,但我需要能够访问字符串的大小并创建一个将该值保持为常量的变量。 Is this possible?这可能吗? Here is the code:这是代码:

static std::string text = "";
static bool read_only = false;
char txt[text.size() + 1] = text;
            

ImGui::Begin("Window");

ImGui::InputTextMultiline("Textbox", txt, IM_ARRAYSIZE(txt), ImVec2(-1.0f, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_AllowTabInput | (read_only ? ImGuiInputTextFlags_ReadOnly : 0));

ImGui::End();
         

The format for the ImGui::InputTextMultiline is this: ImGui::InputTextMultiline 的格式是这样的:

bool InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0,0), ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL)

Edit: The textbox needs to be arbitrary size and not limited by a static const value at compile time, rather a dynamic size such that strings are aswell.编辑:文本框需要是任意大小,并且在编译时不受 static 常量值的限制,而是动态大小,以便字符串也是如此。

Use a local char buffer to accomplish what you want.使用本地char缓冲区来完成您想要的。 There's no OS call to allocate memory, and you should have an idea of what you want your maximum allowable input to be.没有操作系统调用来分配 memory,您应该知道您希望最大允许输入是什么。

This function doesn't really do anything.这个 function 并没有真正做任何事情。 After you get the input, you'll need to copy the data into a std::string or something else to do stuff with it.获得输入后,您需要将数据复制到std::string或其他东西来处理它。

std::string get_text_input(std::size_t arbitrary_size) {
    char* buf = new char[arbitrary_size];

    ImGui::Begin("Window");

    ImGui::InputTextMultiline("Textbox", buf, arbitrary_size, ImVec2(-1.0f, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_AllowTabInput | (read_only ? ImGuiInputTextFlags_ReadOnly : 0));

    ImGui::End();

    std::string ret(buf);

    delete[] buf;

    return ret;
}

Disregard the above.以上无视。 You should use this function signature: https://github.com/ocornut/imgui/blob/01cc6660395032714e7a991eba679a9c69b00c5b/misc/cpp/imgui_stdlib.cpp#L54您应该使用这个 function 签名: https://github.com/ocornut/imgui/blob/01cc6660395032714e7a991eba679a9c69b00stdlib/cpp#L54b0_c5b.m

bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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