Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


#ActualKhatharr

Posted 16 January 2013 - 07:00 AM

You still have the problem of increasing your iterator twice when you erase an object.
 
Common ways are either if/else (if -> erase, else -> increment) or erasing a tmp iterator (auto tmp = iter++).

 
Ah, derp. So it'd have to be:
 

void VirtualKey::unbind(const Binding& binding) {
  for(auto iter = m_bindings.begin(), end = m_bindings.end(); iter != end; ) {
    if((iter->device == binding.device) && (iter->diKey == binding.diKey)) {
      iter = m_bindings.erase(iter);
    }
    else {
      ++iter;
    }
  }
}

 

So I still can't nice-looking syntax with erase. -.-

 

 

 

 

 

You could always use the Std lib Algorithm's to side step the mistakes smile.png

m_bindings.erase(std::remove_if(std::begin(m_bindings), std::end(m_bindings), [&binding](Binding &b) { return (b.device == binding.device) && (b.diKey == binding.diKey); })
		, std::end(m_bindings));

 

Can't bring myself to do it.


#3Khatharr

Posted 16 January 2013 - 06:59 AM

You still have the problem of increasing your iterator twice when you erase an object.
 
Common ways are either if/else (if -> erase, else -> increment) or erasing a tmp iterator (auto tmp = iter++).

 
Ah, derp. So it'd have to be:
 

void VirtualKey::unbind(const Binding& binding) {
  for(auto iter = m_bindings.begin(), end = m_bindings.end(); iter != end; ) {
    if((iter->device == binding.device) && (iter->diKey == binding.diKey)) {
      iter = m_bindings.erase(iter);
    }
    else {
      ++iter;
    }
  }
}

 

So I still can't nice-looking syntax with erase. -.-

You could always use the Std lib Algorithm's to side step the mistakes smile.png

m_bindings.erase(std::remove_if(std::begin(m_bindings), std::end(m_bindings), [&binding](Binding &b) { return (b.device == binding.device) && (b.diKey == binding.diKey); })
		, std::end(m_bindings));

 

Can't bring myself to do it.


#2Khatharr

Posted 16 January 2013 - 06:57 AM

You still have the problem of increasing your iterator twice when you erase an object.
 
Common ways are either if/else (if -> erase, else -> increment) or erasing a tmp iterator (auto tmp = iter++).

 
Ah, derp. So it'd have to be:
 

void VirtualKey::unbind(const Binding& binding) {
  for(auto iter = m_bindings.begin(), end = m_bindings.end(); iter != end; ) {
    if((iter->device == binding.device) && (iter->diKey == binding.diKey)) {
      iter = m_bindings.erase(iter);
      if(iter == end) {break;}
    }
    else {++iter;}
  }
}

 

So I still can't nice-looking syntax with erase. -.-

You could always use the Std lib Algorithm's to side step the mistakes smile.png

m_bindings.erase(std::remove_if(std::begin(m_bindings), std::end(m_bindings), [&binding](Binding &b) { return (b.device == binding.device) && (b.diKey == binding.diKey); })
		, std::end(m_bindings));

 

Can't bring myself to do it.


#1Khatharr

Posted 16 January 2013 - 06:51 AM

You still have the problem of increasing your iterator twice when you erase an object.

 

Common ways are either if/else (if -> erase, else -> increment) or erasing a tmp iterator (auto tmp = iter++).

 

What problem is this?

 

This is what I have right now. It seems to be working correctly:

void VirtualKey::unbind(const Binding& binding) {
  for(auto iter = m_bindings.begin(), end = m_bindings.end(); iter != end; ++iter) {
    if((iter->device == binding.device) && (iter->diKey == binding.diKey)) {
      iter = m_bindings.erase(iter);
      if(iter == end) {break;}
    }
  }
}

PARTNERS