Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL vector

Started by MagTDK May 22, 2002 at 6:11 PM 6 replies 250+ views
Original Post
MagTDK
MagTDK
How do I test if an STL vector contains nothing? I have a function that needs to test if an element already exists in my vector. The problem is, there''s nothing in it the first time through so I get an error. Here is some source:
  
// This is declared in my class header

std::vector<CTexture> STLVector;

void CTexture::Load (std::string file)
{

	std::vector<CTexture>::iterator Iterator;

	for (Iterator = STLVector.begin(); Iterator != STLVector.end(); Iterator++) {

		// Test if element exists and if it does, return from this function


	}

	// Otherwise continue

	CTexture Temp;

	Temp.filename = file;
	
	STLVector.push_back(Temp);

}
  
Bloodshot Eye
Bloodshot Eye
STLVector.empty() will tell you if the vector is empty. Though I''m not an expert I would have thought that if the vector was empty then STLVector.begin() would return STLVector.end() and your loop should exit right away.

Cheers,

Ryan.
When things are dim give ''em VIM
DerekSaw
DerekSaw
Another thing... it is a little faster to use ++Iterator than of Iterator++.

[edited by - DerekSaw on May 22, 2002 8:25:13 PM]
"after many years of singularity, i'm still searching n the event horizon" o
MagTDK
MagTDK

I''ve never used the find keyword, what library does it belong too?

I''m getting this error: ''find'' : undeclared identifier
Fruny
Fruny
#include<algorithms>

find is in the std::namespace.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Shannon Barber
Shannon Barber
find is NOT a keyword
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Bloodshot Eye
Bloodshot Eye
MagTDK, I don''t think you''ve been given the full story on find. If you use find with your vector of CTexture then its going to be using the == operator to compare each element with the third argument you pass to find, which in this case is going to be the file parameter to your Load member function. i.e.

find(STLVector.begin(), STLVector.end(), file);

The problem with this is that comparing a CTexture to a string wont go down well with your compiler. To fix that your going to need to write an operator== member function for your CTexture class.

Something like this:

  
bool CTexture::operator==(const std::string& fileName) const
{
return texFile == fileName;
}


Were texFile is the file name member of the CTexture class.

Cheers,

Ryan.
When things are dim give ''em VIM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.