#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;
    }
}

Recommended Answers

All 4 Replies

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.

how can i correct it? still cannot run.

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";
}

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.