我现在正在用C ++编写一个简单的文本编辑器。它使用std :: vector字符串让用户插入文本。行基本上是插入文本的新行,列是光标当前的位置。
当用户插入新行时,它也应该将其下面的任何文本向下移动一行。到目前为止,我可以将当前行上的文本向下移动一行,但下面的所有文本都会保留,直到我的光标到达该单行。这是当前执行的insertNewLine()代码。
void Editor::insertNewLine()
{
std::string currLine = lines[row];
size_t tempSize = currLine.size();
int lengthOffset = getSubstringOffset(tempSize, column);
std::string cutTemp = currLine.substr(column, lengthOffset); // our string for the new line
lines[row].erase(column); // erase the line we are shifting down
// after incrementing, row and amount of lines, initialize the new row
numberOfLines++;
lines.push_back("");
row++;
column = 1;
lines[row] += cutTemp; // insert substring into new line
}
在我的代码中,我的子字符串方法工作得很好,如果我想要插入的行中没有任何文本,方法本身就可以工作。但是,如果我决定插入一个带有文本的新行,那么就会发生这种情况(|表示光标):
Before
Line 1. Hello
Line 2. |w
Line 3. orld
After inserting a new line at line 2.
Line 1. Hello
Line 2.
Line 3. |orldw
Line 4.
以下是应该技术上发生的事情:
Before
Line 1. Hello
Line 2. |w
Line 3. orld
After inserting a new line at line 2
Line 1. Hello
Line 2.
Line 3. |w
Line 4. orld
是否有一些(简单/快速)方法将向量中的每个字符串元素向下移动一行?它也适用于空行吗?