简体   繁体   中英

vector iterator not dereferencable C++

I'm trying to set the value of a float by using (max_element-min_element) from a vector. 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. Some of the vectors I was using were empty, which caused the error.

EDIT2:

@WhozCraig so now I've done this:

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.

I was doing: cout << lowerBackYAmplitude<< endl (this was one of the values in 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. Either resize amplitudeStorage to match the size of vectorStorage at at the start, or use push_back . I prefer 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).

Note that this code is in C++11, since it uses a range-based for loop. You may need to tell your compiler to turn on support for C++11. There are lots of nice conveniences in 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]);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM