[英]C++ Clearing 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;
}
}
基本上,當我按y
或Y
,它會循環cTitle
,當我看到正確的cTitle
時,按此cTitle
, 大約20個周期后,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鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.