0

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

1 回答 1

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

也许这就是你想做的?

于 2013-07-10T13:32:22.783 回答