字符串使用向量和正斜杠的算术

时间:2014-04-21 01:53:04

标签: c++ string vector

我遇到了一个简单的问题:

    string minEditOperations(vector<vector<int>> DPTable, string firstString, string secondString)
{
    //second string is what first string needs to be changed into, the DPTable function is    flipped
     int rowIterator = firstString.size() , columnIterator = secondString.size();

     string output;

while (rowIterator > 0 && columnIterator > 0)
{
    if (firstString[rowIterator - 1] == secondString[columnIterator - 1])
    {
            //Keep
            columnIterator--;
            rowIterator--;
            output = '^' + output;
    }
    else
    {
        //Calculate the different values
        int mincompare = min(DPTable[rowIterator - 1][columnIterator - 1], min(DPTable[rowIterator][columnIterator - 1], DPTable[rowIterator - 1][columnIterator]));

        if (DPTable[rowIterator - 1][columnIterator - 1] == mincompare)
        {
            output = "/" + firstString[rowIterator] + output;
        }
        else if (DPTable[rowIterator -1][columnIterator] == mincompare)
        {
            output = "-" + output;
        }
        else
        {
            //Right
            //Insert
            output = "+" + firstString[columnIterator] + output;
            columnIterator--;
        }

    }

}
return output;
}

我试图“prepend”到一个字符串,但是每当代码执行时,输出字符串都不会改变。

 output = "+" + firstString[columnIterator] + output; 

任何人都可以对此有所了解吗?

1 个答案:

答案 0 :(得分:2)

问题似乎来自于尝试将字符串添加到char值(因为firstString [rowIterator]是一个char)。

尝试这样的事情:

output = "/" + string(1, firstString[rowIterator]) + output;

这基本上是从char创建一个长度为1的字符串。