Vector arrays

I made some code using vector arrays. However i have some errors in my code. Here it is:

int _tmain(int argc, _TCHAR* argv[])
{
const int people = 10;
vector <int> pancake[people];
int npan;

for (int i = 0; i < people; i++)
{
cout << "Pancakes person " << i + 1 << " had: ";
cin >> npan;
pancake[i] = npan;
}

sort(pancake.begin(), pancake.end());

return 0;
}

In the for loop pancake[i] = npan; i get "no operator "=" matches these operands.

And in the sort function, pancake.begin() and .end() are underlined red with message "expression must have class type".

I have googled several times for an explanation but i dont understand what these messages mean.
Line 4: This is declaring an array of 10 vectors, not a vector of 10 elements.
Use the following to create a vector of 10 elements:
 
  vector <int> pancake (people);


You need the <algorithm> header to use sort.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Oh awesome respond. I tried using the <> formatting button but when I did it wouldnt allow me to post my question. I got an error window several times after I tried using <> formatting button. When I didnt, I could post... thats why :)
I fixed the issue with the vector but how do i cout the end position of the vector?

i tried:

1
2
 cout << "most pancakes eaten: " << *pancake.end() << "! \n";


I get a debugging error after I enter the 10 different amounts of pancakes.

(this time the <> formatting button worked!)
Last edited on
end() points PAST the last entry in the vector.

Since you're sorting the vector, the largest number of pancakes would be in pancake[9] after the sort.


Oh found my mistake. Idk why i tried referencing it as a pointer. But i tried many other things besides this one. What would work is:

1
2
cout << pancake[9] // which is the last place in the vector, and thanks to the sort function it
                               // should contain the highest value entered. 


I was putting pancake[10] which doesnt exist and thats why i was getting the error i believe.
Any suggestions or comments would be really appreciated! Please respond with some constructive comments. Trying to get good at c++
Here is my final code. It looks short and amazing. Vectors really are incredible. I have never use them before.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{   
	const int people = 10;
	vector <int> pancake (people);
	int npan;
	
	for (int i = 0; i < people; i++)
	{
		cout << "Pancakes person " << i + 1 << " had: ";
		cin >> npan;
		pancake[i] = npan;
	}

	sort(pancake.begin(), pancake.end());

	cout << "Most pancakes eaten: " <<pancake[9] << "! \n";
	return 0;
}


Sort function really awesome too
Well I have one more question about this exercise. I want to display which person ate the most pancakes. I current code looks like this:

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
#include "stdafx.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{   
	const int people = 10;
	vector <int> pancake (people);
	int npan;
	
	for (int i = 0; i < people; i++)
	{
		cout << "Pancakes person " << i + 1 << " had: ";
		cin >> npan;
		pancake[i] = npan;
	}

	sort(pancake.begin(), pancake.end());

	cout << "Most pancakes eaten: " << pancake[9] << "! \n";
	cout << "Least pancakes eaten: " << pancake[0] << "! \n\n";

	cout << "\tList of pancakes eaten! \n\n";

	for (int j = 0; j < people; j++)
	{
		cout << pancake[j] << endl;
	}

	return 0;
}


How could I store the following chart into an array or vector so when i sort it the # of pancakes stays associated with the person #?

Person | #Pancakes
-------------------------
p1 | 20
p2 | 100
p1 | 3


You guys dont have to write the code if you dont want to. I would appreciate something like "Here, read this about vectors" or "Look at this link, arrays have this property and such".
The more complications, the more i learn right?



There are a couple of ways you could do it easily. You could create a small struct and store that. Alternatively, you could have a vector of pairs.

I've written a working example using both here: https://ideone.com/gWX2rm

Note that this add a little complexity to your sorting. The struct needs to overload the < operator, so that the sort operation knows how to compare two PancakeStats.

The pair method also needs this, but passes a lambda function as a predicate to std::sort.

A couple of std::vector notes that may have been missed
I noticed a couple of things in your vector usage.

First off, it's worth noting that unlike arrays you don't need to know the size of the vector when you declare it. For example, you could create one without passing the people variable in your code. Vectors are expandable by their very nature. You can add to them by using the push_back() method.

A vector will be initially allocated some size. This isn't in the standard and is implementation specific. When the memory reserved by a vector is exceeded, it'll resize, reserving double the size, and copy the data to the new location in memory. Worth noting that this can be expensive, so reserving size can bear a performance benefit.

Vectors have methods for accessing the first and last elements. They are front() and back() respectively.

When retrieving elements by index, it's possible to access an out of bounds index. If you use the [] operator, you'll end up with undefined behaviour. A safer method is to use the at(), which will throw std::out_of_range if you access an out of bounds index.
Last edited on
Topic archived. No new replies allowed.