我有一个包含一些字符的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;
}
答案 0 :(得分:1)
if(str[i] == 'm')
{
SetConsoleTextAttribute(h, FOREGROUND_RED);
cout<<str[i];
}
else
{
SetConsoleTextAttribute(h, 15);
cout<<str[i];
}
也许这就是你想做的事情?