Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#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