gpt4 book ai didi

C++ 清除 wstring

转载 作者:行者123 更新时间:2023-11-28 07:12:44 25 4
gpt4 key购买 nike

我的问题是,以前用于字符串的方法都不能用于 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 cycles cTitle 被最后一个循环中的文本填满。

最佳答案

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链接到该代码。

关于C++ 清除 wstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com