using victors

Write your question here.
I'm trying to print out the last number of a victor so I used an int var called counter. How come it doesn't work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 int main()
{
	int counter=0;
	int num1;
	int num2;
	vector<int>numbers;

	while(cin>>num1>>num2)
	{
		numbers.push_back(num1);
		numbers.push_back(num2);
		sort(numbers.begin(),numbers.end());
}
		for(int i=0;i<numbers.size();++i)
		{
		cout<<numbers[i]<<" ";
		counter++;
		}
	
		cout<<endl<<"The Highest number is "<<numbers[counter]<<endl;
}
The counter increases as many times as the loop on lines 14-18 executes, i.e. numbers.size() times. numbers[numbers.size()] is not a valid element.

numbers.back() is a valid element, unless numbers is empty.
Topic archived. No new replies allowed.