Jump to content

  • Log In with Google      Sign In   
  • Create Account

STL Vector Insert


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
9 replies to this topic

#1   Members   -  Reputation: 162

Like
0Likes
Like

Posted 21 June 2004 - 05:05 AM

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

#2   Members   -  Reputation: 175

Like
0Likes
Like

Posted 21 June 2004 - 06:27 AM

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.

#3   Members   -  Reputation: 210

Like
0Likes
Like

Posted 21 June 2004 - 06:29 AM

What is the reason for using vector's insert() instead of push_back()?

Kuphryn

#4   Members   -  Reputation: 162

Like
0Likes
Like

Posted 21 June 2004 - 07:34 AM

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.


#5   Moderators   -  Reputation: 11362

Like
0Likes
Like

Posted 21 June 2004 - 08:48 AM

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.

#6   Members   -  Reputation: 162

Like
0Likes
Like

Posted 21 June 2004 - 09:53 AM

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.

#7   Members   -  Reputation: 162

Like
0Likes
Like

Posted 22 June 2004 - 11:01 AM

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;
}
}

#8   Members   -  Reputation: 583

Like
0Likes
Like

Posted 22 June 2004 - 11:09 AM

just out of curiosity, what kind of exception does an at() call throw? i mean, what paremters should my catch block recieve? a char * ?

#9   Moderators   -  Reputation: 11362

Like
0Likes
Like

Posted 22 June 2004 - 11:24 AM

std::vector::at() should throw std::out_of_range.

#10   Members   -  Reputation: 162

Like
0Likes
Like

Posted 22 June 2004 - 11:28 AM

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.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS