C++ 在给定列和行的字符数组中查找索引?

C++ 在给定列和行的字符数组中查找索引?,c++,algorithm,C++,Algorithm,这是我的情况 我有一个换行算法,它可以在宽度过大时生成文本行,或者在文本中找到\n或“-” 因此,它不将“\n”包含在文本行本身中。这对我来说是个挑战 我正在尝试做一个算法,在给定行和列的情况下,返回字符数组中的索引 因此,如果第1行的长度是20,第2行的长度是10,那么如果第1行最初有一个“\n”,那么应该考虑到这一点 例: 案文如下: Hello blue sky\nworld 然后我们得到两行: Hello blue sky world 现在,如果我的插入符号由“|”表示,我将其放在这

这是我的情况

我有一个换行算法,它可以在宽度过大时生成文本行,或者在文本中找到\n或“-”

因此,它不将“\n”包含在文本行本身中。这对我来说是个挑战

我正在尝试做一个算法,在给定行和列的情况下,返回字符数组中的索引

因此,如果第1行的长度是20,第2行的长度是10,那么如果第1行最初有一个“\n”,那么应该考虑到这一点

例:

案文如下:

Hello blue sky\nworld
然后我们得到两行:

Hello blue sky
world
现在,如果我的插入符号由“|”表示,我将其放在这里:

  Hello blue sky
  |world
结果不是第14个字符,而是第15个字符,因为必须考虑不可见的“\n”

棘手的部分是换行符是否决定生成“\n”

例如:

在这种情况下,结果应该仍然是15,因为wrap推送了一个新行,而不是因为文本中有“\n”

谢谢

Here is my mess so far:

int AguiTextBox::indexFromColumnRow( int column, int row )
{
    int len = 0;
    int charCount = 0;
    std::string curChar;

    int curLen = 0;
    int bytesSkipped = 0;
    std::string::const_iterator it = getText().begin();
    std::string::const_iterator end = getText().end();

    if(textRows.size() == 0)
    {
        return -1;
    }
    for(int i = 0; i < row; ++i)
    {
        len = _unicodeFunctions.getUtf8StringLength(textRows[i]);

        for(int j = 0; j < len; ++j)
        {
            //skip characters that would not have passed the newline test
            do
            {
                curLen = _unicodeFunctions.bringToNextUnichar(it,end);
                curChar = getText().substr(bytesSkipped,curLen);
                bytesSkipped += curLen;
                charCount++;
            }
            while(curChar[0] < ' ');
        }

    }

    len = len = _unicodeFunctions.getUtf8StringLength(textRows[row]);


    if(column == 0 && charCount + 1 < getTextLength())
    {
        curChar = _unicodeFunctions.getUtf8SubStr(getText(),charCount,1);

        while(charCount < getTextLength() - 1)
        {
            if(curChar[0] < ' ' && curChar[0] != '\n')
            {
                charCount++;
                curLen = _unicodeFunctions.bringToNextUnichar(it,end);
                curChar = getText().substr(bytesSkipped,curLen);
                bytesSkipped += curLen;
            }
            else
            {
                break;
            }
        }
        if(curChar[0] == '\n')
        {
            charCount++;
            curLen = _unicodeFunctions.bringToNextUnichar(it,end);
            curChar = getText().substr(bytesSkipped,curLen);
            bytesSkipped += curLen;
        }
    }
    for (int i = 0; i < column; ++i)
    {
        do
        {
            curLen = _unicodeFunctions.bringToNextUnichar(it,end);
            curChar = getText().substr(bytesSkipped,curLen);
            bytesSkipped += curLen;
            charCount++;
        }
        while(curChar[0] < ' ');
    }

    return charCount - 1;
}

//and the opposite
AguiPoint AguiTextBox::columnRowFromIndex( int index )
{
    std::string::const_iterator it = getText().begin();
    std::string::const_iterator end = getText().end();

    std::string curChar;
    int charCount = 0;
    int len = 0;
    int byteCount = 0;
    int curLen = 0;
    for(int i = 0; i < (int)textRows.size(); ++i)
    {
        len = _unicodeFunctions.getUtf8StringLength(textRows[i]);

        for(int j = 0; j < len; ++j)
        {

            //skip characters if needed
            curLen = _unicodeFunctions.bringToNextUnichar(it,end);
            curChar = getText().substr(byteCount,curLen);
            byteCount += curLen;

            while (curChar[0] < ' ' && curChar[0] != '\n')
            {
                curLen = _unicodeFunctions.bringToNextUnichar(it,end);
                curChar = getText().substr(byteCount,curLen);
                byteCount += curLen;
            }
            if(curChar[0] == '\n')
            {
                charCount++;
                if(charCount == index)
                {
                    return AguiPoint(j,i);
                }

            }
            charCount++;
            if(charCount == index)
            {
                return AguiPoint(j,i);
            }



        }
    }
    return AguiPoint(len,textRows.size() - 1);
}
这是我目前的困境:
int AguiTextBox::indexFromColumnRow(int列,int行)
{
int len=0;
int charCount=0;
std::字符串curChar;
int-curLen=0;
int bytesSkipped=0;
std::string::const_迭代器it=getText().begin();
std::string::const_迭代器end=getText().end();
if(textRows.size()==0)
{
返回-1;
}
对于(int i=0;i
我不清楚您需要如何处理这些数据。你到底是想写一个编辑还是什么

您可以做的一件事是构建一个指向每行开头的指针数组,也许还有每行的长度

这样,您就可以表示所有行,而无需修改基础数据


但是,您应该更好地准确描述转换数据的要求。

向我们展示您的代码。到目前为止,您有什么问题?哪里出了问题?@Jonathan Grynspan我发布了我的代码,但它运行得不太好。正如您所知:假设
getText()
按值返回
std::string
,您应该只调用一次。否则,迭代器将无法正确匹配(尽管我看不到您在任何地方实际使用它们。)我正在制作一个支持WordWrap的文本框,我需要在插入符号位置删除此功能。我猜这不适用于Windows?我的建议行不通吗?
Here is my mess so far:

int AguiTextBox::indexFromColumnRow( int column, int row )
{
    int len = 0;
    int charCount = 0;
    std::string curChar;

    int curLen = 0;
    int bytesSkipped = 0;
    std::string::const_iterator it = getText().begin();
    std::string::const_iterator end = getText().end();

    if(textRows.size() == 0)
    {
        return -1;
    }
    for(int i = 0; i < row; ++i)
    {
        len = _unicodeFunctions.getUtf8StringLength(textRows[i]);

        for(int j = 0; j < len; ++j)
        {
            //skip characters that would not have passed the newline test
            do
            {
                curLen = _unicodeFunctions.bringToNextUnichar(it,end);
                curChar = getText().substr(bytesSkipped,curLen);
                bytesSkipped += curLen;
                charCount++;
            }
            while(curChar[0] < ' ');
        }

    }

    len = len = _unicodeFunctions.getUtf8StringLength(textRows[row]);


    if(column == 0 && charCount + 1 < getTextLength())
    {
        curChar = _unicodeFunctions.getUtf8SubStr(getText(),charCount,1);

        while(charCount < getTextLength() - 1)
        {
            if(curChar[0] < ' ' && curChar[0] != '\n')
            {
                charCount++;
                curLen = _unicodeFunctions.bringToNextUnichar(it,end);
                curChar = getText().substr(bytesSkipped,curLen);
                bytesSkipped += curLen;
            }
            else
            {
                break;
            }
        }
        if(curChar[0] == '\n')
        {
            charCount++;
            curLen = _unicodeFunctions.bringToNextUnichar(it,end);
            curChar = getText().substr(bytesSkipped,curLen);
            bytesSkipped += curLen;
        }
    }
    for (int i = 0; i < column; ++i)
    {
        do
        {
            curLen = _unicodeFunctions.bringToNextUnichar(it,end);
            curChar = getText().substr(bytesSkipped,curLen);
            bytesSkipped += curLen;
            charCount++;
        }
        while(curChar[0] < ' ');
    }

    return charCount - 1;
}

//and the opposite
AguiPoint AguiTextBox::columnRowFromIndex( int index )
{
    std::string::const_iterator it = getText().begin();
    std::string::const_iterator end = getText().end();

    std::string curChar;
    int charCount = 0;
    int len = 0;
    int byteCount = 0;
    int curLen = 0;
    for(int i = 0; i < (int)textRows.size(); ++i)
    {
        len = _unicodeFunctions.getUtf8StringLength(textRows[i]);

        for(int j = 0; j < len; ++j)
        {

            //skip characters if needed
            curLen = _unicodeFunctions.bringToNextUnichar(it,end);
            curChar = getText().substr(byteCount,curLen);
            byteCount += curLen;

            while (curChar[0] < ' ' && curChar[0] != '\n')
            {
                curLen = _unicodeFunctions.bringToNextUnichar(it,end);
                curChar = getText().substr(byteCount,curLen);
                byteCount += curLen;
            }
            if(curChar[0] == '\n')
            {
                charCount++;
                if(charCount == index)
                {
                    return AguiPoint(j,i);
                }

            }
            charCount++;
            if(charCount == index)
            {
                return AguiPoint(j,i);
            }



        }
    }
    return AguiPoint(len,textRows.size() - 1);
}