Pointer to vector element
I cannot figure out how to get a pointer to an element through an iterator,
indexing works but since I'm using an iterator that is out. The vector holds structures by value and the class that it is contained in has a method to get an iterator to an item by looking up it's name. The return of the Find method is something like this: std::vector< mystruct* >::iterator. Since Find is a private method I have also added a public Lookup method to return a pointer to the element in the array. I don't use Vector or the STL that often so I'm not quite sure how to do this. Any ideas? |
Re: Pointer to vector element
Well, I guess posting no code isn't very smart of me.
Here is what I have but it really needs to be cleaned up and possibly rewritten. Code:
#ifndef USERMESSAGE_H |
Re: Pointer to vector element
wow, C++.
I suck at C++. Personally I would get rid of the STL and use my own struct for that. Then using an array like this: Code:
struct usermessage_t messages[MAX_USERMESSAGES]; Adding a pointer to the callback function in the network message structure is a smart idea, I think I will borrow this one ! :) What's the advantage of using the STL by the way ? Auto-sorting by name or by ID ? This can be done using a binary tree and a linked list instead of an array for sorting by name; and sorting by ID can be done just by inserting the messages at their right position in the array since there's 256 registerable user messages at max in the HL engine. |
Re: Pointer to vector element
To be honest, if I could write this stuff in a pure C compiler I would but this seemed to be the easiest way to do things. Much less complicated that way.
The problem with allocating a fixed array of 256 user message objects is that any given dll only sees from 60 and above wasting quite a bit of space. Useless addition that has nothing to do with this post: Hehe - whenever my fan goes faster or slower the usage light on my ups goes up or down a notch. |
Re: Pointer to vector element
STL is quite fine since the performance is ok, and you dont have to care about implementing your own lists, etc. and the vector class is as fast as an array as long as you dont look at adding elements ...
already tried &(*iterator) for getting a pointer to an element ? |
Re: Pointer to vector element
Yeah, but I think it's a pointer to the iterator not the element.
|
Re: Pointer to vector element
Quote:
|
Re: Pointer to vector element
Well, ignore my comment earlier I misread @$.1415rin's post.
A small test shows that &(*iterator) gives a pointer to the element. Thanks to all who posted here. @PMB: Then there are more steps to do, its a choice between wasting memory or extra lookup steps. I looked at this problem and saw an object-oriented solution though those methods you mentioned probably work great aswell. vbmenu_register("postmenu_8440", true); |
Re: Pointer to vector element
or you can use my code you know, :) in rcbot I use similar structures for catching network messages (without STL but my own generic structures... ho hum)
oh .. and it should be easy to understand, that's what I've been trying to do with most of my code (...heh without comments *cough*) |
Re: Pointer to vector element
Well, since you must write all half-life code in C++ there was no sense in ignoring the STL. It was easy to use and didn't require alot of maintenance.
When I get around to finishing my C sdk for half-life I'll start using it instead of C++. |
All times are GMT +2. The time now is 10:22. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.