[英]vector iterator not dereferencable C++
I'm trying to set the value of a float by using (max_element-min_element) from a vector.我试图通过使用向量中的 (max_element-min_element) 来设置浮点数的值。 I'm looping over several of these, hence the vector of floats and vector of vectors.我正在循环其中的几个,因此是浮点数向量和向量向量。
I'm getting the following error:我收到以下错误:
Expression: Vector iterator not dereferencable
vector<float> amplitudeStorage;
vector<vector<float>> vectorStorage;
int main(){
for (int i = 0; i < amplitudeStorage.size(); i++)
{
AssignWaveAmplitude(amplitudeStorage[i], vectorStorage[i]);
}
}
It happens on the function call.它发生在函数调用上。 The function looks like this:该函数如下所示:
void AssignWaveAmplitude(float amplitudeVariable, vector<float> dataVectorr)
{
amplitudeVariable = (*max_element(begin(dataVectorr), end(dataVectorr))) - (*min_element(begin(dataVectorr), end(dataVectorr)));
}
Does anyone know how to fix this?有谁知道如何解决这一问题?
Thanks very much.非常感谢。
EDIT1:The solution to this problem is the first comment to this question. EDIT1:这个问题的解决方案是这个问题的第一个评论。 Some of the vectors I was using were empty, which caused the error.我使用的一些向量是空的,这导致了错误。
EDIT2:编辑2:
@WhozCraig so now I've done this: @WhozCraig 所以现在我已经做到了:
for (int i = 0; i < amplitudeStorage.size(); i++) {
amplitudeStorage[i] =AssignWaveAmplitude(vectorStorage[i]);
}
and this:和这个:
float AssignWaveAmplitude( vector<float> dataVectorr) {
return (*max_element(begin(dataVectorr), end(dataVectorr))) - (*min_element(begin(dataVectorr), end(dataVectorr)));
}
but the floats all still come out to the same number.但浮点数仍然是相同的数字。 any idea why?知道为什么吗?
EDIT3: It turns out the reason as to why the floats were coming out wrong was because I was outputting them wrong. EDIT3:事实证明,浮点数出现错误的原因是我输出错误。
I was doing: cout << lowerBackYAmplitude<< endl
(this was one of the values in amplitudeStorage)我在做: cout << lowerBackYAmplitude<< endl
(这是amplitudeStorage 中的值之一)
I should have been doing:我应该做的:
for (int i = 0; i < amplitudeStorage.size(); i++)
{
cout << amplitudeStorage[i] << endl;
}
Your compiler is probably trying to warn you that you cannot correctly refer to amplitudeStorage[i] for any value of i, when the amplitudeStorage vector is simply empty.您的编译器可能试图警告您,当ampampionStorage 向量为空时,您无法正确引用amplitudeStorage[i] 的任何i 值。 Either resize amplitudeStorage to match the size of vectorStorage at at the start, or use push_back
.要么在开始时调整amplitudeStorage 的大小以匹配vectorStorage 的大小,要么使用push_back
。 I prefer push_back
.我更喜欢push_back
。 Here's a complete example (which also avoids passing vectors by value and corrects the problem in your code of using int
to index a data structure whose size itself can grow to more than the maximum value an int
can hold).这是一个完整的示例(它也避免了按值传递向量,并纠正了代码中使用int
索引数据结构的问题,该数据结构的大小本身可以增长到超过int
可以容纳的最大值)。
Note that this code is in C++11, since it uses a range-based for loop.请注意,此代码使用 C++11,因为它使用基于范围的 for 循环。 You may need to tell your compiler to turn on support for C++11.您可能需要告诉编译器打开对 C++11 的支持。 There are lots of nice conveniences in C++11. C++11 中有很多很好的便利。
#include <vector>
#include <algorithm>
using namespace std;
float WaveAmplitude(const vector<float>& dataVectorr) {
return (*max_element(begin(dataVectorr), end(dataVectorr)))
- (*min_element(begin(dataVectorr), end(dataVectorr)));
}
vector<float> amplitudeStorage;
vector<vector<float>> vectorStorage;
int main(void) {
// Populate vectorStorage somehow here, replacing this comment.
amplitudeStorage.clear();
for (const auto& wave : vectorStorage)
{
amplitudeStorage.push_back(WaveAmplitude(wave));
}
return 0;
}
我使用的一些向量是空的,这导致了错误。
See full worked example.请参阅完整的工作示例。 I try co compile and there is no problems.我尝试共同编译,没有问题。
#include <vector>
#include <algorithm>
using namespace std;
vector<float> amplitudeStorage;
vector< vector<float> > vectorStorage;
void AssignWaveAmplitude(float& amplitudeVariable, const vector<float>& dataVectorr) {
if( dataVectorr.size() ) {
amplitudeVariable = 0;
return;
}
amplitudeVariable = (*max_element(dataVectorr.begin(), dataVectorr.end())) -
(*min_element(dataVectorr.begin(), dataVectorr.end()));
}
main(){
for (int i = 0; i < amplitudeStorage.size(); i++) {
AssignWaveAmplitude(amplitudeStorage[i], vectorStorage[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.