- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
StockAccount::StockAccount() {
vector<string> temp;
string line;
std::ifstream stockfile("Results.txt");
if (stockfile.is_open()) {
while (stockfile.good()) {
getline(stockfile, line);
istringstream ss(line);
string token;
while (std::getline(ss, token, ',')) {
temp.push_back(token);
}
addStock(temp.at(0), temp.at(1), temp.at(2));
temp.clear();
}
stockfile.close();
} else {
cout << "Unable to open file" << std::endl << std::endl;
}
}
我知道它效率不高,这就是我要解决的问题。它应该做的是:
我正在使用该 vector temp 来存储值,将它们添加到函数中,然后将其清除,以便它可以为空并再次用于存储下一个值......等等。
我尝试在 temp.clear() 之前打印出每个值,它们都打印出来,然后我得到了错误。所以我知道 temp.clear() 是问题所在。也许我使用了错误的方法,或者有更好的方法。
我想尝试,如果可能的话不使用 boost。
这是我遇到的错误:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector
Results.txt 是一个看起来像的文件。
goog,525,0MSFT,34,10
等等。
最佳答案
while (stockfile.good())
是错误的,会导致您阅读额外的、不存在的一行。
那是因为您在尝试读取新行之前检查流有效性;如果没有要读取的新行,只有在 调用 getline
之后,此条件才会评估为 false
但到那时为时已晚您正在尝试处理这条不存在的线路。
那条不存在的行上没有三个标记,但您没有对标记化执行错误检查,也没有验证 vector temp
的大小。
因此,当您尝试访问这三个不存在的 vector 元素时,会抛出异常。
你的循环应该是这样的:
while (getline(stockfile, line)) {
istringstream ss(line);
string token;
// ...
}
请注意我是如何直接检查循环条件是否成功的,如果实际的 getline
失败,这将阻止执行循环体。
关于c++ - 获取 out_of_range : vector error for c++ but can't figure out why,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20406242/
这段代码是关于将字符串转换为替代密码(将特定字母替换为另一个特定字母,例如:用'E'代替'a')当我输入字符串时,它可以正常工作。但是,当我输入一个句子时,它说“在抛出一瞬间'std::out_of_
我编写了一个对字符串进行百分号编码的函数,如下所示: string percent_encode(string str) { string reserved = // gen-delims
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有一个非常简单的程序,可以将十六进制字符串转换为它的 int 值。代码看起来不错,但会引发运行时错误: terminate called after throwing an instance of
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试制作一个基本的计算器,用户可以在其中输入 2 个数字和一个运算符,然后计算就可以进行了。我不明白为什么我会收到这个错误,我也不完全确定这个错误是什么意思,因为我是 c++ 的新手,只有一些
给定一个包含 ASCII 码和相应数值的全局 vector list 以及一个字符串,例如 000.00-000.0.0.0,此函数接受一个输入 token strings 2-char or 3-c
嗨,我正在点击 std::out_of_range: basic_string::substr在下面的代码中。一直在努力修复它,但无法完成。 std::string orig_str = "1-1,2
目前在 uni 做一个项目,起初我需要取消字符串的连字符,看起来很简单但是当我运行程序时它有一个错误 WeirdPuncProgram.exe: Microsoft C++ exception: st
下面是发生这个错误的函数: std::string DataTranslation::getMeshName(std::string meshLink) { File input(this->
我正在处理一个包含行链接列表的文件,每个节点如下所示: struct TextLine{ //The actual text string text; //The line n
我在 IDE 输出日志中收到此异常消息,即使我有一个代码可以捕获它,如果它发生的话。 这是代码本身: t_ptr obj_ptr; try { obj_ptr = _objects.at(fi
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有以下代码来读取文本文件。 const string FILENAME = PACKAGES_DIR + pname; //the arguments to ifstream is a cstr
这个问题在这里已经有了答案: Why does std::getline() skip input after a formatted extraction? (5 个答案) 关闭 6 年前。 in
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在 Linux Ubuntu 的 opencv 中编写以下代码。x_captured 和 y_captured 是“int”类型的 vector 。两个 vector 的大小都是 18。 for
我正在尝试编写一个简单的链表,并在用户需要超出范围的节点索引时尝试抛出 out_of_range 异常。但是,当我编译源文件时,出现错误“‘out_of_range’不是‘std’的成员”。 我的理解
考虑以下无法编译的代码: #include //#include // uncommenting this works int main() { std::map test; t
// The following code works fine, throwing a std::out_of_range exception: std::vector vd{ 1.
我是一名优秀的程序员,十分优秀!