向 C++ 向量添加元素

Adding elements to C++ vector

我想学习更多关于 C++ 编码的知识,尤其是设计和创建桌面应用程序。

首先,我想创建一个通知应用程序,我可以在其中通过为任务指定名称和内容来创建任务。为此,我使用了 2 个向量(namecontent)。

我这样初始化向量:

std::vector <LPWSTR> NameTask; 
std::vector <LPWSTR> ContentTask; 

之后,我简单地创建了一个按钮来将 namecontent 添加到向量中:

if (wmId == ID_BUTTON) {
    //local variables (only for the Button event)
    int len_name = GetWindowTextLength(TextBox_Name) + 1; //give the lenght value of the text in the textbox
    int len_content = GetWindowTextLength(TextBox_content) + 1; //give the lenght value of the text in the textbox
    wchar_t Text_name[100] = L""; //Wchar is compatible with LPWSTR
    wchar_t Text_content[100] = L""; //Wchar is compatible with LPWSTR
            
    // get the text of the Text edit and put it in local variable name and content
    GetWindowText(TextBox_Name, Text_name, len_name);
    GetWindowText(TextBox_content, Text_content, len_content);
    //verify if the texts are empty
    if (wcslen(Text_name) == 0 || wcslen(Text_content) == 0) {
        MessageBox(hWnd, L"name and content can't be empty", L"MessageBox", MB_OK);
    }
    else {
        NameTask.push_back(Text_name); // set name of task in vector of NameTask
        ContentTask.push_back (Text_name); // set content of task in vector of ContentTask
        //ComboBox_AddString(Combobox, Text_name); // add the title of the task in the combotext
        //SetWindowText(TextBox_Name, L""); //empty the textbox
        SetWindowText(TextBox_content, NameTask.at(0)); // visualize first element of vector name
    }           
}

我遇到的问题是,当我向矢量添加一个新元素,然后将其可视化时,它总是显示最后添加的元素。即使我使用 SetWindowText(TextBox_content, NameTask.at(0));

当我使用另一个按钮可视化 namecontent 时,它给出了一个奇怪的输出:

我不知道你到底想做什么。首先,我会将 wchar_t 更改为 std::wstring。这样你就不需要指定大小。但是,这里我有一个建议

if (wmId == ID_BUTTON) {
        //local variables (only for the Button event)
        int len_name = GetWindowTextLength(TextBox_Name) + 1; //give the lenght value of the text in the textbox
        int len_content = GetWindowTextLength(TextBox_content) + 1; //give the lenght value of the text in the textbox
        std::wstring Text_name = L""; //Wchar is compatible with LPWSTR
        std::wstring Text_content = L""; //Wchar is compatible with LPWSTR
        
        // get the text of the Text edit and put it in local variable name and content
        GetWindowText(TextBox_Name, Text_name, len_name);
        GetWindowText(TextBox_content, Text_content, len_content);
        //verify if the texts are empty
        if (wcslen(Text_name) == 0 || wcslen(Text_content) == 0) {
            MessageBox(hWnd, L"name and content can't be empty", L"MessageBox", MB_OK);
        }
        else {
            NameTask.push_back(Text_name); // set name of task in vector of NameTask
            ContentTask.push_back (Text_name); // set content of task in vector of ContentTask
            //ComboBox_AddString(Combobox, Text_name); // add the title of the task in the combotext
            //SetWindowText(TextBox_Name, L""); //empty the textbox
            SetWindowText(TextBox_content, NameTask.at(0)); // visualize first element of vector name
        }           
    }

问题是您在向量中存储了悬挂指针。您的 if 块正在分配本地数组,然后将指向这些数组的指针存储到向量中。当 if 块退出时,数组被销毁,但向量仍然指向它们。

您需要对从 UI 中检索的文本数据进行 复制。您可以使用 std::wstring 来处理。

试试像这样的东西:

struct Task
{
    std::wstring Name; 
    std::wstring Content; 
};

std::vector<Task> Tasks;

...

if (wmId == ID_BUTTON) {
    int len_name = GetWindowTextLength(TextBox_Name);
    int len_content = GetWindowTextLength(TextBox_content);

    if (len_name == 0 || len_content == 0) {
        MessageBox(hWnd, L"name and content can't be empty", L"MessageBox", MB_OK);
    }
    else {
        ++len_name;
        ++len_content;

        std::wstring Text_name(L'[=10=]', len_name);
        std::wstring Text_content(L'[=10=]', len_content);
            
        len_name = GetWindowText(TextBox_Name, &Text_name[0], len_name);
        Text_name.resize(len_name);

        len_content = GetWindowText(TextBox_content, &Text_content[0], len_content);
        Text_content.resize(len_content);

        Task task;
        task.Name = Text_name;
        task.Content = Text_content;
        Tasks.push_back(task);

        //ComboBox_AddString(Combobox, Text_name.c_str());
        //SetWindowText(TextBox_Name, L"");
        SetWindowText(TextBox_content, Text_name.c_str());
    }
}

...

// use Tasks[index].Name(.c_str()) and Tasks[index].Content(.c_str()) as needed...

我改变了它,它起作用了。这可能不是理想和正确的方式,但我会顺其自然。

if (wmId == ID_BUTTON) {
            //local variables (only for the Button event)
            int len_name = GetWindowTextLength(TextBox_Name)+1; //give the lenght value of the text in the textbox
            int len_content = GetWindowTextLength(TextBox_content)+1; //give the lenght value of the text in the textbox
            wchar_t Text_name[len] = L""; //Wchar is compatible with LPWSTR
            wchar_t Text_content[len] = L""; //Wchar is compatible with LPWSTR
            
            // get the text of the Text edit and put it in local variable name and content
            GetWindowText(TextBox_Name,  (LPWSTR) Text_name, len_name);
            GetWindowText(TextBox_content, (LPWSTR)Text_content, len_content);
            //verify if the texts are empty
            if (len_name == 1 || len_content == 1) {
                MessageBox(hWnd, L"name and content can't be empty", L"MessageBox", MB_OK); 
            }
            else {
                NameTask.push_back(Text_name); // set name of task in vector of NameTask
                ContentTask.push_back(Text_content); // set content of task in vector of ContentTask
                ComboBox_AddString(Combobox,Text_name); //set name in combobox
                SetWindowText(TextBox_Name, L""); //empty the textbox
                SetWindowText(TextBox_content, L""); //empty the textbox
                
                
                
            }

        }