can't deference out of range vector iterator

Mistake: can't deference out of range vector iterator.

file1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using namespace std::chrono;

int RandomNumber2() { return (std::rand() % 100); }
void experiment::FillVector(std::vector<int>& a) {
    std::srand(unsigned(std::time(0)));
    std::generate(a.begin(), a.end(), RandomNumber2);
    std::cout << "\n\nVector: ";
    for (std::vector<int>::iterator it = a.begin(); it != a.end(); it++) std::cout << *it << " ";
}

void experiment::StoogeSort(std::vector<int>& a, int start, int end) {
    int temp{0};
    if (end - start + 1 > 2) {
        temp = (end - start + 1) / 3;
        StoogeSort(a, start, end - temp);
        StoogeSort(a, start + temp, end);
        StoogeSort(a, start, end - temp);
    }
    if (a[end] < a[start]) std::swap(a[end], a[start]);
}

double experiment::measureTime(std::vector<int>& a, int n) {
    steady_clock::time_point t1 = steady_clock::now();
    StoogeSort(a, 0, n-1);
    steady_clock::time_point t2 = steady_clock::now();
    duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
    double mTime = time_span.count();
    return mTime;
}


void experiment::Experiment(std::vector<int> &a, int n, int start, int end) {
    FillVector(a);
    StoogeSort(a, 0, n - 1);
    std::cout << "\n\na. Sorted data: ";
    for (int i{ 0 }; i < n; i++) std::cout << a[i] <<  ' ';

    statistics stat;

    double* times = new double[10];
    double* means = new double[10];
    double* stds = new double[10];

    std::cout << "\n";
    for (int k{0}; k<=53; k++) std::cout << "-";
    std::cout << "\nArgument" << " | " << "Average time"<< " | " << "Standard deviation of time |" << std::endl;
    for (int k{0}; k<=53; k++) std::cout << "-";

    for (n=100; n < 1000; n+=100) {
        std::cout << "\n[" << n << "]" << " ";
        for (int j{1}; j < 10; j+=1) {
            times[j] = measureTime(a, n);
        }
        means[n] = stat.meanTime(times, n);
        stds[n] = stat.stdTime(times, means[n]);
    }
}


file2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "statistics.h"
#include "cmath"
#include <iostream>

//Измерение среднего времени для пунктов a, b и c
double statistics::meanTime(double* t, int n) {
    double s{0};
    for (int j{1}; j < 10; j += 1) s += t[j];
    double aTime = s / 10;
    std::cout << "\t   [" << aTime << "]    \t";
    return aTime;
}

//Измерение std для пунктов a, b и c
double statistics::stdTime(double* t, double mean) {
    double s{0};
    for (int j{1}; j < 10; j += 1) s += (t[j] - mean) * (t[j] - mean);
    double sTime = sqrt(s / 10);
    std::cout << "[" << sTime << "]";
    return  sTime;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<vector>
#include "experiment.h"
#include <chrono>

int main() {
   

    experiment ex;
    int n{1000};
    std::vector<int> a(n);
    ex.Experiment(a, n, 0, n - 1);
   
    return 0;
}
Last edited on
main line 11: What do you think the value of n is? It's not 1000. It is the address of the array n. You can't dimension a vector with the address of something.

Misread the braces as brackets.

Last edited on
onetwo12, from what I can tell your issue is not in the sorting part of the code, rather it is due to a misuse of your variables. In your Experiment function, you are using 'n' for two different purposes, and this is confusing you.
- First, you use n to mean the size of the vector.
- Later, you use n as an iteration variable.

Line 41: 'means' points to a dynamic array of length 10.
Line 54: means[n] is accessed, where n >= 100.

You are going way outside the bounds of your array.
(same thing with stds array)
Last edited on
You are going way outside the bounds of your array.


Yes, thanks
Topic archived. No new replies allowed.