C ++清除wstring

时间:2013-12-22 10:50:38

标签: c++ wstring

我的问题是,没有任何用于字符串的方法适用于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会完全填满来自最后一个周期的文本。

1 个答案:

答案 0 :(得分:1)

std::wstring::clear应该有用,因为它和std::string都是std::basic_string。如果您遇到问题,请查看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链接。