Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

Feeling dumb with iterators...

This topic is 4014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a vector of a vector of doubles. That is: std::vector< std::vector<double> > m_metData; I need to get at the doubles inside the nested vector. so I'm doing this:
for(std::vector< std::vector<double> >::iterator it = m_metData.begin(); it != m_metData.end(); it++)
	{
		std::vector<double>::iterator jt = *it;
                double myDouble = jt[0];
.......


1>        No constructor could take the source type, or constructor overload resolution was ambiguous
Feelin' dumb! Cheers

Share this post


Link to post
Share on other sites
Advertisement

for(std::vector< std::vector<double> >::iterator it = m_metData.begin(); it != m_metData.end(); it++)
{
for ( std::vector<double>::iterator jt = (*it).begin(); jt != (*it).end(); ++jt )
{
double myDouble = *jt;
}




but really for performance (you don't want to call .end() every iteration unless you're calling something like erase internal to the loop that changes the value of end() )


std::vector< std::vector<double> >::iterator ite = m_metData.end();
for(std::vector< std::vector<double> >::iterator it = m_metData.begin(); it != ite; it++)
{
std::vector<double>::iterator jte = (*it).end();
for ( std::vector<double>::iterator jt = (*it).begin(); jt != jte; ++jt )
{
double myDouble = *jt;
}



Share this post


Link to post
Share on other sites
Dereferencing it will produce a reference to a std::vector<double>, which is not an iterator. To get an iterator out of that, use .begin() again:

std::vector<double>::iterator jt = (*it).begin();
//or alternatively
std::vector<double>::iterator jt = it->begin();


However, you access jt as though it were a vector, not an iterator for a vector. If you want to keep jt as an iterator, then you'll need to access a particular element like so:

double myDouble = *(jt + 0);


The " + 0" part increments the iterator (well, a temporary iterator is incremented; jt remains the same) 0 times. Other numbers would increment it more, allowing you to access different elements. Then the *() dereferences this adjusted iterator.

However, if you want to keep the indexing syntax, what you need jt to be is a reference to a vector, not an iterator:

std::vector<double>& v = *it;
double myDouble = v[0];


(Or what Palidine said, depending on what you need to do.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!