【发布时间】:2014-06-21 20:06:54
【问题描述】:
void Display::displayText(const char* text) {
using std::string;
using std::vector;
string line = string(text);
vector<string> temp(1);
if (Display::startLine < 0) Display::startLine = 0;
bool cont = true;
int lastRegex = 0;
int regex = 1;
string tmp = string(" ");
for (int i=0; i<line.length(); i++) {
if(line.at(i)=='\n') {
tmp = line.substr(lastRegex, i-lastRegex);
tmp.erase(tmp.find("\n"), tmp.find("\n"));
lastRegex = i+1;
regex++;
temp.resize(regex);
temp[regex-1] = tmp;
}
if (i - lastRegex == COLS-3) {
bool b = true;
int j = i;
while (b) {
if (line.at(i) == ' ') {
b = false;
tmp = line.substr(lastRegex, j-lastRegex);
lastRegex = j+1;
regex++;
temp.resize(regex);
temp[regex-1] = tmp;
}
j--;
}
}
}
regex++;
temp.resize(regex);
temp[regex-1] = line.substr(lastRegex, string::npos);
if (Display::startLine+1 > temp.size()) Display::startLine = temp.size()-1;
for (int i=0; i<temp[startLine].length(); i++) Window::draw(i+1, 1, temp[Display::startLine].at(i));
if (Display::startLine+1 <= temp.size()-1)
for (int i=0; i<temp[Display::startLine+1].length(); i++) Window::draw(i+1, 2, temp[Display::startLine].at(i));
if (Display::startLine > 0) Window::draw(COLS-2, 1, '^');
if (Display::startLine + 1 < temp.size()-1) Window::draw(COLS-2, 2, 'v');
}
}
此代码编译正确。但是,当我运行它时,我收到 std::basic_string::at 的 std::out_of_range 错误。
我尝试添加检查行是否为空,并将 for 循环更改为 .length()-1,但两者都产生相同的结果。
这个函数应该接收文本,并将其显示在窗口的顶部两行(因此是 COLS 变量和Window::draw),如果文本扩展过去,则在行尾添加箭头两条线。我当前输入的引发错误的文本是“Hello World!”。
如果我使用Window::draw 手动显示相同的文本,则绘图功能没有问题。 (此方法专门用于自动将文本环绕在屏幕周围并在两行处加盖)
【问题讨论】:
-
我们不能替代您的调试器:/
-
std::out_of_range - 这意味着您使用
.at()访问不存在的数组元素 -
与其使用
for (int i=0; i<line.length(); i++),不如使用for(auto ch; line)。 -
@RSahu 我需要使用 i 来获取正确的子字符串
-
这种情况下,可以在循环前定义并初始化
i,使用for ( auto it = line.begin(); it != line.end(); ++it, ++i )。
标签: c++ string c++11 stdstring