gpt4 book ai didi

C++ SetConsoleTextAttribute 更改字符串中的字符

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

我有一个包含一些字符的 C++ 字符串。如果遇到某些字符,如何更改字符颜色?下面是示例代码:

#include <iostream>
#include "windows.h"
using namespace std;
int main()
{
HANDLE h;
h = GetStdHandle(STD_OUTPUT_HANDLE);
string str = "my name is meow.";
for(int i=0; i<str.length(); i++)
{
if(str[i] == 'm')
{
//change the char 'm' to red color..
}

cout<<str[i];
}
return 0;
}

最佳答案

 if(str[i] == 'm')
{
SetConsoleTextAttribute(h, FOREGROUND_RED);
cout<<str[i];
}
else
{
SetConsoleTextAttribute(h, 15);
cout<<str[i];
}

也许这就是你想做的?

关于C++ SetConsoleTextAttribute 更改字符串中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17571900/

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