Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 332 333 [334] 335 336 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 883315 times)

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4995 on: September 21, 2013, 06:21:34 pm »

You can use an iterator for that, see the following example: http://www.cplusplus.com/reference/map/map/begin/
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4996 on: September 21, 2013, 07:50:57 pm »

Try this:
Code: [Select]
for (auto entry : data) {
  string key = entry.first;
  int value = entry.second;
  // your code here
}
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4997 on: September 21, 2013, 09:03:13 pm »

Don't you need 3 conditions for a for loop or something? This isn't working.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4998 on: September 21, 2013, 10:11:26 pm »

Don't you need 3 conditions for a for loop or something? This isn't working.

That's a range-based for loop. It requires C++11 support to be used.

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4999 on: September 21, 2013, 10:13:26 pm »

So how would I do what I want to do with an earlier version of C++?
Logged

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5000 on: September 21, 2013, 10:23:08 pm »

Not certain this code is correct syntax and I have no compiler installed on this computer, but something like this should work:

Code: [Select]
map<string, int>::iterator it;

for (it = data.begin(); it != data.end(); it++) {
    // Do whatever you need to do with the data value.  it->first is the key, it->second is the value
    cout << it->second;
}

It's been a little while since I've worked with iterators, but I'm pretty sure it should fit that pattern pretty closely.

To explain the code: iterators are a class that abstract accessing elements of STL containers like maps and vectors.  They use operator overloading to make it possible to advance them by the ++ operator, so you can treat them almost like an integer in that respect.  The containers have methods for getting iterators for the beginning and end of the container (begin() and end()).
« Last Edit: September 21, 2013, 10:26:51 pm by Telgin »
Logged
Through pain, I find wisdom.

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5001 on: September 21, 2013, 11:05:28 pm »

Thank you very much.

So, I've written the basic skeleton of the program http://pastebin.com/bTnBHej4, but when I try to compile I get the error "debug assertion failed... vector subscript out of range". Does anyone know what that's about?

Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5002 on: September 21, 2013, 11:35:23 pm »

It means you are trying to access a vector element that doesn't exist. Eg, for a vector of length 10 you attempt to access the 11th element.

Also: try searching the internet with the error message. Nine out of ten Stack Overflow has the answer. XD
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5003 on: September 22, 2013, 04:49:28 am »

So, I've written the basic skeleton of the program http://pastebin.com/bTnBHej4, but when I try to compile I get the error "debug assertion failed... vector subscript out of range". Does anyone know what that's about?
Did that error come with a line number?
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5004 on: September 22, 2013, 05:02:00 am »

Data is c++ <string, int> map.
Assuming data is a dictionary can't you just do: (python code, but that's basically pseudo-code so you'll understand :P)
Code: [Select]
for key in data:
    print(key, data[key])
Try this:
Code: [Select]
for (auto entry : data) {
  string key = entry.first;
  int value = entry.second;
  // your code here
}

It might just be MSVS, but
Code: [Select]
for each (auto entry in data)
{
  string key = entry.first;
  int value = entry.second;
  // your code here
}

seems to work. I've used it extensively in my AI's code, and it's a lot more intuitive than for ( x : y)... but that just might be me.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5005 on: September 22, 2013, 05:19:31 am »

Looks like that's not part of the C++11 standard.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5006 on: September 22, 2013, 05:41:38 am »

That makes sense. It's code from C# that happens to work exactly the same in C++. I imagine the msvc compiler replaces all for each with for (x : y).
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #5007 on: September 22, 2013, 09:03:32 am »

Yeah, don't do that. Ever. Non-standard extensions are evil.

Seriously, you'll be kicking yourself if you ever want to compile that with GCC or Clang.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5008 on: September 22, 2013, 09:16:32 am »

Yeah, don't do that. Ever. Non-standard extensions are evil.

Seriously, you'll be kicking yourself if you ever want to compile that with GCC or Clang.

Well, that instance is really easy and trivial to fix. Just replace all for each with ( x : y) when I want to compile it on gcc.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5009 on: September 22, 2013, 12:03:39 pm »

So, I've written the basic skeleton of the program http://pastebin.com/bTnBHej4, but when I try to compile I get the error "debug assertion failed... vector subscript out of range". Does anyone know what that's about?
Did that error come with a line number?
Line:932
Logged
Pages: 1 ... 332 333 [334] 335 336 ... 796