# Vectors and Windows Programming

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-24-2008
strokebow
Vectors and Windows Programming
Hi,

Is it possible for me to make a vector of POINT's

Something like:

Code:

`vector<POINT>    name;`

Thanks
• 11-24-2008
matsp
I don't see any reason that wouldn't work. POINT is a simple struct, so that should work just fine in a vector.

--
Mats
• 11-24-2008
strokebow
Quote:

Originally Posted by matsp
I don't see any reason that wouldn't work. POINT is a simple struct, so that should work just fine in a vector.

--
Mats

Thats what I thought but I'm getting 2 errors about that line.

Error1: error C2143: syntax error : missing ';' before '<'
Error2: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

:-S

FYI:

Code:

```#include <windows.h> #include <vector>```
are also present.

Any ideas?
• 11-24-2008
matsp
Are you by any chance missing a "std::" in front of "vector"?

--
Mats
• 11-24-2008
strokebow
:):)
Legend

Nice 1
• 11-24-2008
strokebow
How do I write to a field of the struct?

eg)
Code:

`name.x.push_back(23)`
??

How do you access a vector op and a struct field at the same time??

Thanks
• 11-24-2008
matsp
No, you'd push back a POINT, e.g.
Code:

```POINT p; p.x = 23; p.y = 42; v.push_back(p);```
After all, you have a vector of points, not a vector of x's and another vector of y's.

--
Mats
• 11-24-2008
strokebow
Quote:

Originally Posted by matsp
No, you'd push back a POINT, e.g.
Code:

```POINT p; p.x = 23; p.y = 42; v.push_back(p);```
After all, you have a vector of points, not a vector of x's and another vector of y's.

--
Mats

Thanks

wuldnt it be alot better for me to create an array of points then which I can acvcess much easier:

Code:

`name[0].x = 23`
What do you think?
• 11-24-2008
matsp
You can do that with vector too: Use the "presize" by construction:
Code:

`std::vector<T> name(size);`
Or, you can resize the vector to the size you want:
Code:

`  name.resize(size);`
--
Mats
• 11-24-2008
strokebow
Quote:

Originally Posted by matsp
You can do that with vector too: Use the "presize" by construction:
Code:

`std::vector<T> name(size);`
Or, you can resize the vector to the size you want:
Code:

`  name.resize(size);`
--
Mats

ahh! excellent
• 11-24-2008
strokebow
Quote:

Originally Posted by matsp
No, you'd push back a POINT, e.g.
Code:

```POINT p; p.x = 23; p.y = 42; v.push_back(p);```
After all, you have a vector of points, not a vector of x's and another vector of y's.

--
Mats

So if i wanna add extra data as and when its needed I am going to have to follow the protocol (above)?

Because that is painful
• 11-24-2008
matsp
Quote:

Originally Posted by strokebow
So if i wanna add extra data as and when its needed I am going to have to follow the protocol (above)?

Because that is painful

You can of course resize as you need it too. So if you know you need 10 more, you do:
Code:

```size_t size = v.Size(); size_t newsize = size + 10; v.resize(newsize); for(i = size; i < newsize; i++) {   v[i].x = i;   v[i].y = i * 2; }```
It's even more painful if you have to do this without vector (normal C style arrays and/or dynamic memory allocation).

--
Mats
• 11-24-2008
Thantos
Does POINT have any constructors? Would it allow you to do this?
Code:

`v.push_back(POINT(23,42));`
• 11-24-2008
matsp
Quote:

Originally Posted by Thantos
Does POINT have any constructors? Would it allow you to do this?
Code:

`v.push_back(POINT(23,42));`

No, POINT is a standard C type, something like this:
Code:

```typedef struct _POINT {   int x;   int y; } POINT;```
It may use a different integral type (guessing DWORD), but in essence it doesn't have any C++ features.

I guess we could make a proxy object that pretends to be a POINT.

--
Mats
• 11-24-2008
strokebow
Quote:

Originally Posted by Thantos
Does POINT have any constructors? Would it allow you to do this?
Code:

`v.push_back(POINT(23,42));`

Nice thought. But,

error C2661: 'tagPOINT::tagPOINT' : no overloaded function takes 2 arguments

POINT is just a structure
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last