1

I'm trying to compare two strings

One string is a normal string that I got from an istringstream:

string command;
iss >> command;

and the other is a string that I get from my map.

But it does not seem to be working.

The idea is that I have a map<string, string>, the value is to be an escape code to change the terminal output colour.

I have a map in my main that I put all the keys and values into (which works, I've tested that) so I pass the map into this function.

I also have defined colours at the top of my program:

#define BLACK "\033[22;30m"
#define RED "\033[22;31m"
#define GREEN "\033[22;32m"
#define BROWN "\033[22;33m"
#define BLUE "\033[22;34m"
#define PURPLE "\033[22;35m"
#define CYAN "\033[22;36m"
#define GREY "\033[22;37m"
#define DARK_GREY "\033[01;30m"
#define LIGHT_RED "\033[01;31m"
#define LIGHT_GREEN "\033[01;32m"
#define YELLOW "\033[01;33m"
#define LIGHT_BLUE "\033[01;34m"
#define LIGHT_PURPLE "\033[01;35m"
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"

This is the part that seems not to be working and confusing me:

string getColor(string command, map<string, string> &m)
{
  string color;
  if((m.find(command)->second).compare(RED) == 0)
  {
    color = RED;
    return color;
  }
  // ...and so on for all the other colors
}

I would have made this a switch, but C++ doesn't allow switches on strings.

So the problem is, even if the both strings are the same, it's not working like that.

I realize that m.find() is returning an iterator at the position of what we are looking for. But then doing m.find()->second should get to the value right? And the value is a string, isn't it?

So in short, I'm not sure why the comparison isn't working.

EDIT:

Ok, here is the config file I was talking about:

bold           \e[0;31m 
italic         \e[0;34m
underline      \e[0;32m 
default        \e[0;37m

so the user input would be something like:

(default this is a(bold simple) example.)

so when I get the command "default", I will search in my map for that string, and then get the colour code associated with it.

then I will change the output colour to the colour in the map associated with default. then I will change the output colour to the colour associated in the map with "bold" to print out the word "simple", and then I will go back to the previous colour to print out the rest of the sentence.

but because when I do this:

cout << config.find("bold")->second << " hi" << endl;

it doesn't change the colour, it prints out:

\e[0;31m hi

instead of changing the output colour :/

so I thought I'd do a comparison, because something like this:

cout << RED << "hi" << endl;

will print out hi in the colour red.

4

1 回答 1

0

听起来您正在尝试进行非常复杂的地图查找。这不行吗?

    const string & getColor(string command, const map<string, string> & m)
    {
        map<string,string>::const_iterator i = m.find(command);
        return i == m.end() ? BLACK : i->second;
    }

当然,这并没有解决所谓的相等字符串而不是比较相等的问题。我认为,您需要发布一个更完整的示例,以便我们能够回答这个问题。但是关于嵌入空字符的第一个答案是错误的。你所拥有的是嵌入的转义字符,这是完全正确的。我唯一不同的是使用 const char * RED = etc. 而不是#defines,因为变量比宏更容易处理。

编辑:似乎我在这里指的答案消失了。

Edit2:这与您尝试做的相比如何?它至少对我有用:

    #include <cstdio>
    #include <string>
    #include <iostream>
    #include <sstream>
    #include <map>
    using namespace std;

    #define BLACK "\033[22;30m"
    #define RED "\033[22;31m"
    #define GREEN "\033[22;32m"

    const string & getColor(const string & command, const map<string, string> & m)
    {
        map<string,string>::const_iterator i = m.find(command);
        return i == m.end() ? BLACK : i->second;
    }

    int main() {
        map<string,string> cols;
        cols["BLACK"] = BLACK;
        cols["RED"]   = RED;
        cols["GREEN"] = GREEN;

        string line, cmd, val;
        while(getline(cin, line))
        {
            stringstream ss(line);
            if(ss >> cmd >> val && cmd == "color:")
                cout << getColor(val, cols);
            else cout << line << endl;
        }
    }

输入

This text is default color
color: RED
This text is red
color: GREEN
This text is green

应产生指示的颜色。

Edit3:根据您的新信息,似乎真正的问题是您的配置文件包含不包含实际的转义字符,仅包含字符“\”和“e”。确保您的文件实际上包含真正的转义字符。如何生成这些因编辑器而异。在 vim 中,您可以通过 ctrl-V 后跟 escape 来输入转义。在最坏的情况下,您可以通过从您自己的 C 程序中打印它们来生成它 - 毕竟,您已经有了定义:fprintf(conffile, "bold %s\n", BOLD).

于 2012-10-28T23:19:30.713 回答