#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
    string title, nameX, nameY;
    const int maxData = 20;
    int data, maxValue;
    int count = 0;
    vector<int> v;
     maxValue = *(max_element(v.begin(), v.end()));
    for(int a = maxValue; a > 0; a++)
    {
        if (a < 10)
        {
            cout << a << "  |";
        }
        else
        {
            cout << a << " |";
        }
        for(int b = 0; b < v.size()-1; b++)
        {
            int temp = v[b];
            if (temp >= maxValue)
            {
                if (b < 10)
                {
                    cout << " * ";
                }
                else
                {
                    cout << "  * ";
                }
            }
            else
            {
                if (b < 10)
                {
                    cout << "   ";
                }
                else
                {
                    cout << "    ";
                }
            }
        }
        maxValue--;
        cout << endl;
    }
}

Line 15.

The vector v has no contents. It is of size zero. max_element returns the iterator that is v.end(). You then try to dereference this iterator, which is forbidden.

What moschops is saying is that you need to check for an empty vector or end iterator OR make sure that vector v is not empty.

std::vector<int>::iterator it = (max_element(v.begin(), v.end()));
if (it != v.end())
{
    maxValue = *it;
    //all other code (that for loop) goes here..
}
else
{
    std::cout<<"Vector v is empty!!\n";
}

Edited 2 Years Ago by triumphost

Given that you create the vector empty, and never put anything in it, what exactly is it for? Did you mean to put something in it?

This article has been dead for over six months. Start a new discussion instead.