简体   繁体   English

C ++字符比较不起作用

[英]C++ char comparison not working

It is probably better to say I am doing something wrong rather then say the comparison isn't working. 最好说我做错了,然后说比较不起作用。 But, I have been going over this code for some time now. 但是,我已经研究这段代码已有一段时间了。

I have a recursive function. 我有一个递归函数。 Most of it is working well, so I will only put the part that isn't working: 大部分都运行良好,因此我只介绍不起作用的部分:

//In main
string C[] = {"S=>bS",
                    "S=>aaT",
                    "T=>aT",
                    "T=>bU",
                    "U=>Ua",
                    "U=>aa"};
CFG CFG1(C);

...

string *code;
char startNT;
//The CFG constructor 
CFG::CFG(string C[])
{
    code = C;
    startNT = code[0][0];
}

...

//Inside of processData recursive function
for(unsigned int i = 0; i < code->size(); i++)
{
    if(code[i][0] == startNT)
    {
        string newStr = code[i].substr(code[i].find(">")+1);
        string old = wkString;
        //This is the recursive call
        if(processData(inString, wkString.replace(wkString.find_first_of(startNT), wkString.find_first_of(startNT)+1, newStr)))
        {
            return true;
        }
        cout << wkString << endl;
        wkString = old;

    }
}

The comparison that isn't working is code[i][0] == startNT . 比较失败的代码是code [i] [0] == startNT。 Well... I should say, isn't working 100% of the time. 好吧...我应该说,不是100%的时间都在工作。 It works great until half way through the recursive function, code[i][0] becomes 'S' and startNT becomes 'T' (after already proving that it can compare 'S' and 'T' properly somewhere during the call), and it still evaluates to true, which causes the wkString.replace() to break since it can't find the 'T'. 在递归函数执行到一半之前,它一直有效,代码[i] [0]变为“ S”,startNT变为“ T”(已经证明它可以在调用过程中的某个位置正确比较“ S”和“ T”),并且它仍然评估为true,这导致wkString.replace()中断,因为找不到'T'。

It has been awhile since I have used C++, so I am probably making a stupid mistake. 自从我使用C ++已经有一段时间了,所以我可能犯了一个愚蠢的错误。 Thanks for any help. 谢谢你的帮助。

code is a pointer to the first string in an array of strings. code是指向字符串数组中第一个字符串的指针。 So when you say code->size() , that's the size(number of characters) of the first string(5 in the example you gave). 因此,当您说code->size() ,这就是第一个字符串的大小(字符数)(在您给出的示例中为5)。 I'm pretty sure you're trying to iterate over the string array, not the characters in the first string. 我确定您要遍历字符串数组,而不是第一个字符串中的字符。 So that's wrong. 这是错误的。

Unfortunately, since you store a pointer in the class, and not an array, the size of the array is unknown to the class. 不幸的是,由于您将指针存储在类中,而不是数组中,因此该类不知道数组的大小。 So you can't iterate over it properly. 因此,您无法对其进行适当的迭代。 You'll need to restructure your code somehow. 您需要以某种方式重组代码。 Without being able to see it more fully, I cannot make any concrete suggestions. 如果无法更全面地了解它,我将无法提出任何具体建议。

What you probably want to do is store a collection of strings in the actual class. 您可能想要做的是在实际的类中存储字符串集合。 By default, I recommend a vector. 默认情况下,我建议使用向量。 Your code might look something like this then: 然后,您的代码可能看起来像这样:

// in class CFG
std::vector<std::string> code;
char startNT;

CFG(const string * C, int N)
    :code(C, C+N),
    startNT(code[0][0]) // strong assumption made here
{}
...
// processData
for(unsigned int i = 0; i < code.size(); i++) // note, dot instead of arrow
{
...
// in main
std::string C[] = {
    "S=>bS",
    "S=>aaT",
    "T=>aT",
    "T=>bU",
    "U=>Ua",
    "U=>aa"
};

CFG CFG1(C, sizeof(C)/sizeof(*C));

You're using,' i < code->size(); 您正在使用,'i <code-> size(); i++', to control the loop, but code is a pointer to a string, so I'm thinking it's telling you the size of the first element of code, not the number of code strings. i ++”来控制循环,但是代码是指向字符串的指针,所以我认为它是在告诉您代码第一个元素的大小,而不是代码字符串的数目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM