C++ 后退一行

标签 c++ c++11

我正在编写一个多行系统,如下所示:

string readLines(string x)
{
    string temp = "a";
    vector<string> lines(0);
    string result;

    while (1)
    {
        cout << x;
        getline(cin, temp)

        if(temp != "")
        {
            result = result + "\n" + temp;
            lines.push_back(temp);
        }
        else
            break;
    }
    return result;
}

工作正常,但我希望能够编辑上一行,例如,我正在输入如下内容:

Helo,
World

我想回到 helo 并修复我的拼写错误。我该怎么做?

最佳答案

在 C++ 中没有返回一行的可移植方法。

您可以通过打印 \r 转到行首, 但移动到上一行需要依赖于平台的代码。

如果不想使用像Curses 这样的库, 你可以试试ANSI escape codes .取决于终端,cout << "\033[F"会将光标向上移动一行。

在 Windows 上,还有 SetConsoleCursorPosition API。

关于C++ 后退一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45134217/

相关文章:

c++ - 可排列的最长链条

c++ - 如何使用 cmake 设置 GDB 调试标志?

c++ - Unordered_map 产生二级键

c++ - 可变参数宏 : Reuse variadic arguments (Boost. fusion )

c++ - boost::bind 与模板仿函数

c++ - mingw 4.7.1 上的 stoi 和 std::to_string

c++ - VS2010 中的标准智能感知

c++ - vector::insert 重载之间的 libc++ 区别

c++ - 移动语义 C++11(Bjarne Stroustrup 书,第 75 页)

c++ - 在 C++ 模板类中存在循环依赖时修复包含顺序