Cannot find an vector struct tutorial. Don't know how to access.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 5856 Views - Last Post: 11 July 2011 - 10:15 AM Rate Topic: -----

#1 grasmanek94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 11-July 11

Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 06:33 AM

Hello dear community,

I was stumbling across a problem when creating a plugin for a game. I already have made it now I want to add some features but the most efficient way would be vectors. unfortunately I am still at the beginner phase of learning c++ (or even lower).

Yet I created a Struct and a array, however resizing arrays is something dangerous to me so I want to use vectors instead. - I hope I made a good choice.

Okay so what I did:
//#define MAX_POINTS (1000)

struct BonusData
{
	float POSX;
	float POSY;
	float POSZ;
} 
//BonusPoint[MAX_POINTS]
;

vector <BonusData> BonusPoint;


(I have commented out the array - which works but resizing it is not really an option.)

now I want to set/get POSX for example on the 11th index:
float tempX = BonusPoint->POSX->at(11);


Ye I get an error on this line:
Error: expression must have a pointer type.



And I don't know how to add values to my vector and return the index...

What I am trying to make:
AddPoint(float x,float y,float z); which returns the index and RemovePoint(index); will remove an entry from AddPoints' vactor.

I searched severeal tuttorials but they do not cover the struct thingy...

Many thanks in advance :)

This post has been edited by grasmanek94: 11 July 2011 - 06:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot find an vector struct tutorial. Don't know how to access.

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 06:36 AM

std::vector<int> x;
// adding element to vector
x.push_back(1);
x.push_back(2);
// accessing element in vector
std::cout << x[1];


Was This Post Helpful? 0
  • +
  • -

#3 grasmanek94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 11-July 11

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 06:38 AM

View PostPlasticineGuy, on 11 July 2011 - 06:36 AM, said:

std::vector<int> x;
// adding element to vector
x.push_back(1);
x.push_back(2);
// accessing element in vector
std::cout << x[1];


I don't get it.. where do you use structs here?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 06:39 AM

There are probably several tutorials on this site for vectors but you can start with this page: Vector read the page and follow the links to the various methods and you should be able to start to grasp the vector class. The next link I would look at is the constructor. And then to insert elements into the vector look at push_back.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 06:41 AM

I don't; I was just using int as an easy example. It works for structs too:
struct 2nums {
    int a;
    int b;
    2nums (int n1, int n1): a(n1), b(n2) {}
    2nums (): a(0), b(0) {}
};
std::vector<2nums> x;
x.push_back(2nums(1, 2));
x.push_back(2nums(3, 4));
std::cout << x[0].a;

Was This Post Helpful? 1
  • +
  • -

#6 grasmanek94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 11-July 11

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 06:57 AM

Okay if I understood you my code should be this:
struct BonusData
{
	float xPOS;
	float yPOS;
	float zPOS;
	BonusData(float n1,float n2,float n3):xPOS(n1),yPOS(n2),zPOS(n3){}
};
vector <BonusData> BonusPoint;

//somewhere
	BonusPoint.push_back(BonusData(0.0,0.0,0.0));//ADDS a variable, how do I get the index?
//maybe>
int index = BonusPoint.push_back(BonusData(0.0,0.0,0.0));//??

	BonusPoint[index].xPOS = 1.1;//
	float tempx = BonusPoint[index].xPOS;


and how to remove an index from a vector?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:00 AM

Maybe the erase member function?

Jim
Was This Post Helpful? 0
  • +
  • -

#8 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:01 AM

To remove use erase:
std::vector<BonusData> BonusPoint;
// somewhere
BonusPoint.push_back(BonusData(0.0, 0.0, 0.0));
unsigned int index = BonusPoint.size() - 1; // get the index

// to remove:
BonusPoint.erase(BonusPoint.begin() + index); // use vector::begin() + index

Was This Post Helpful? 1
  • +
  • -

#9 grasmanek94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 11-July 11

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:03 AM

View Postjimblumberg, on 11 July 2011 - 07:00 AM, said:

Maybe the erase member function?

Jim

okay but does
int index = BonusPoint.push_back(BonusData(0.0,0.0,0.0));//??
get the position, and most of all, all positions are static? if I have position 1 2 and 3 and I delete 2, i will keep 1 and 3, aren't I right?

EDIT: read plasticines' post, thank you I understand it now, yet my last question about vectors for now:
all positions are static? if I have position 1 2 and 3 and I delete 2, i will keep 1 and 3, aren't I right?
if they are not static, how would I save them?

This post has been edited by grasmanek94: 11 July 2011 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:09 AM

Did you read the link provided?

Quote

Removes from the vector container either a single element (position) or a range of elements ([first,last)).

This effectively reduces the vector size by the number of elements removed, calling each element's destructor before.

Because vectors keep an array format, erasing on positions other than the vector end also moves all the elements after the segment erased to their new positions, which may not be a method as efficient as erasing in other kinds of sequence containers (deque, list).


Does this not answer your question?

Jim
Was This Post Helpful? 1
  • +
  • -

#11 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:09 AM

No, push_back returns nothing.
Positions are NOT static. If you need them to be, consider if you really need to erase positions.
Was This Post Helpful? 1
  • +
  • -

#12 grasmanek94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 11-July 11

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:28 AM

View PostPlasticineGuy, on 11 July 2011 - 07:09 AM, said:

No, push_back returns nothing.
Positions are NOT static. If you need them to be, consider if you really need to erase positions.


well I'm a PAWN scripter if you need to know and the functions I want to make:

native AddFlag(Float:X,Float:Y,Float:Z);
returns : The position in the vector for later use.

native DeleteFlag(position); //will remove a flag

That's what I want to acomplish, and if you do in PAWN:
new variable[2];
variable[0] = AddFlag(0.0,0.0,0.0);
variable[1] = AddFlag(1.0,1.0,2.0);
DeleteFlag(variable[0]);
DeleteFlag(variable[1]);//Exeption?



then variable[1] will be still pointing to position 1 instead 0, that's why I want it to be static.
And if there will be around 1000 points created, and if somebody delete's it I can make an additional vector variable like bool enabled; but if there will be 500 disabled points the loop will make 500 loops to much.. I need performance :(

This post has been edited by grasmanek94: 11 July 2011 - 07:29 AM

Was This Post Helpful? 0
  • +
  • -

#13 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:30 AM

If you need performance, think about if it is vital to use dynamic memory. If not, then you don't need to erase anything.
Was This Post Helpful? 0
  • +
  • -

#14 grasmanek94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 11-July 11

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:40 AM

View PostPlasticineGuy, on 11 July 2011 - 07:30 AM, said:

If you need performance, think about if it is vital to use dynamic memory. If not, then you don't need to erase anything.

the Plug In I am making is already very customizable by the end-users and I want to keep it very dynamic like turning all settings on off and creating deleting things...
okay well, I will make a 'enabled' variable, if a user creates to much and needs to REMOVE for performance... his problem lol (but I do care..).
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Cannot find an vector struct tutorial. Don't know how to access.

Posted 11 July 2011 - 07:57 AM

Instead of actually deleting the elements you could add a flag variable to your structure/class that indicates if the element is valid or "deleted". Then you just change the value of this flag to "delete" the element. You may also want to wrap this vector in another class that handles all the accessing of the vector and will automatically alter the flag when required. Then if the user adds too many items and wants to reduce the size you could provide a method that actually removes the "deleted" items.

Also the std::vector may not be the best container for this application. If you do not need random access to each element then maybe a std::list might be a better choice. Or if you can access the data based on a "key" maybe a map/multimap might be a better choice.

Jim
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2