STL Vector Insert

Started by
8 comments, last by ZoomBoy 19 years, 10 months ago
I'm a STL newb and I've been doing some Googling but haven't come up with anything specific with respect to vector::insert I've a vector of tiles and What I want to do is access the Tile by [] or at(). push_back doesn't give me the exact thing I need because I'm not guaranteed the exact [] access. I'll be storing the Tilenumber on my map and need an exact query to vectTiles.at(tilenum).
 
class TileManager 
{ 
... 
	std::vector <Tile *> vectTiles; 
} 

TileManager::TileManager() 
{ 
... 
	for (int tilenum = TileStartNumber; tilenum < TileStartNumber + TileEndNumber; tilenum++) 
		{ 
		SetRect(&rcTSource, tilenum); 
		Tile *pNewTile = new Tile(&rcTSource, tsiLoadSheet.pTileSurface, tsiLoadSheet.Sheet)); 
		vectTiles.insert(&vectTiles.at(tilenum), pNewTile); 
		} 
} 
I use to have vectTiles.insert(tilenum, pNewTile) but recognized I need an iterator to insert rather than just tilenum. When I run this, it looks like the nothing is allocated as yet in the Vector. Is there a preliminary step before using Insert? ZoomBoy Developing a iso-tile 2D RPG with skills, weapons, and adventure. See my old Hex-Tile RPG GAME, character editor, diary, 3D Art resources at Check out my web-site
Advertisement
First, insert() puts the element to be inserted *before* the iterator that you pass in. So to insert something at the end of a vector you would do container.insert(continer.end(), value).

In your situation, the easiest method would probably be to use container.insert(container.begin() + tilenum, value). What this does is creates an iterator pointing to the beginning of the vector (with begin()) and then moves it forward to point to the element at tilenum.

Also, this will work fine when you have nothing in the vector.
What is the reason for using vector's insert() instead of push_back()?

Kuphryn
Quote:Original post by kuphryn
What is the reason for using vector's insert() instead of push_back()?

Kuphryn


I'd push_back() X to X + Nth for the 1st input. But as the Editor is going to allow dynamically loading of tilenumber sequences by other users, push_back doesn't allow for absolute access.

For example: push_back() X to X + Nth and then push_back()Y to Y + Nth
If Y is less than X or slightly out of sequence, I'll get a incorrect answer with vectTile[123]
But this is as far as I know. And as Error-checking I also want to check for Tilenumber collision.
As I'm a STL newb this is the best answer I have so far.
It sounds like your problem is that you're trying to create a vector with uninitialized elements in different spots. This is not kosher. If you don't want contiguous storage then you probably need to use a different container class. Try std::map, or if your library supports it, one of the hash_map extension classes.
Quote:Original post by SiCrane
It sounds like your problem is that you're trying to create a vector with uninitialized elements in different spots. This is not kosher. If you don't want contiguous storage then you probably need to use a different container class. Try std::map, or if your library supports it, one of the hash_map extension classes.
Yes, my mind does bounce back to that but I've tended to think of that as being used to map strings to objects. Mapping to a integer would be simpler - I'll take a look at that - especially collision.
I've decided to stick with the vector container. After some more research I might implement the STL map.

As for out_of_range activities, I've wrapped the retreival of the Tile info in the Tile Manager

Tile* TileManager::GetTile(int CurrentTile)
{
try {
return vectTiles.at(CurrentTile); // at() - throws an out_of_range exception
}
catch (...)
{
return NULL;
}
}
just out of curiosity, what kind of exception does an at() call throw? i mean, what paremters should my catch block recieve? a char * ?
FTA, my 2D futuristic action MMORPG
std::vector::at() should throw std::out_of_range.
It looks like an argument string
class out_of_range : public logic_error {public:     out_of_range (const string& what_arg);};out_of_range::out_of_range (const string& what_arg);Constructs an object of class out_of_range.


I use the generic (...) because that's sufficient to know that there's an error.

This topic is closed to new replies.

Advertisement