我有一个包含一些字符的 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/