C++ 清除绳子

C++ Clearing wstring

本文关键字:清除 C++      更新时间:2023-10-16

我的问题是,过去处理字符串的方法不适用于 wstring。所以我问我如何才能轻松地清除wstring以达到美学目的。

我现在的代码:

    while (!foundRightOne)
    {
        wstring cTitle;
        ForegroundWindow = GetForegroundWindow();
        cout << "FRGW  " << ForegroundWindow << endl;
        int len = GetWindowTextLengthW(ForegroundWindow) + 1;
        wchar_t * windowTitle = new wchar_t[len];
        GetWindowTextW(ForegroundWindow, windowTitle, len);
        title += windowTitle;
        // OUTPUT
        cTitle = L"Title: ";
        cTitle += title;
        wcout << cTitle << endl;
        cTitle = ' ';
        //OUTPUT
        keyPress = getchar();
        system("CLS");
        if (keyPress == 'y' || keyPress == 'Y')
        {
            foundRightOne = true;
        }
    }

基本上,当我按yY时它会循环,当我看到正确的cTitle时按下,并且在~20个周期后,cTitle被上一个周期的文本完全填满。

std::wstring::clear应该可以工作,因为它和std::string都是std::basic_string s。 如果您遇到问题,请查看 std::basic_string 文档。

#include <iostream>
int main()
{
    std::string regularString("regular string!");
    std::wstring wideString(L"wide string!");
    std::cout << regularString << std::endl << "size: " << regularString.size() << std::endl;
    std::wcout << wideString << std::endl << "size: " << wideString.size() << std::endl;
    regularString.clear();
    wideString.clear();
    std::cout << regularString << std::endl << "size: " << regularString.size() << std::endl;
    std::wcout << wideString << std::endl << "size: " << wideString.size() << std::endl;
}

输出:

regular string!
size: 15
wide string!
size: 12
size: 0
size: 0

下面是指向该代码的 ideone 链接。