3 Replies - 962 Views - Last Post: 11 March 2012 - 11:31 AM Rate Topic: -----

#1 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Comparing Python and C++: The "for loop"

Posted 11 March 2012 - 08:53 AM

It occurred to me while I was learning Python that Python's "for" is slightly different from C++'s "for".

I noticed that they behave similarly in the way that...

# Python
for i in range(10):
     print x



is equivalent to

// C++
for(i = 0; i < 10; i++)
     cout << x << endl;



... but I was wondering how something like...

# Python
for line in infile.readlines():
     # [...]



... or...

# Python
import string
for numStr in string.split(inString):
     # [...]



... is possible. I'm having a hard time wrapping my head around this.

Is This A Good Question/Topic? 0
  • +

Replies To: Comparing Python and C++: The "for loop"

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: Comparing Python and C++: The "for loop"

Posted 11 March 2012 - 09:25 AM

Assuming infile.readline() were to return string::npos when there are no lines left, you might do something like this to get the same behavior:
string line;
while ((line = infile.readlines()) !== string::npos) {
    // [...]
}


You can of course use the for loop as well, if you prefer that syntax. I'm no expert on C++ syntax, but I suppose that could look a little bit like:
string line;
for (string line = infile.readlines(); line !== string::npos; line = infile.readlines()) {
    // [...]
}


Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: Comparing Python and C++: The "for loop"

Posted 11 March 2012 - 09:40 AM

What do you mean by "how is it possible"? Do you mean how would you implement it in C++? Well, python's for loop is of the form for var in collection: where collection must be iterable and var will be assigned to each element of the collection consecutively. So the C++ equivalent would simply be:

for(collection_type::iterator it = collection.begin(); it < collection.end(); it++) {
   T var = *it;
   // ...
}



Or in C++11:

for(T var: collection) {
    // ...
}


Was This Post Helpful? 1
  • +
  • -

#4 Lenn0s  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-March 12

Re: Comparing Python and C++: The "for loop"

Posted 11 March 2012 - 11:31 AM

View Postsepp2k, on 11 March 2012 - 09:40 AM, said:

What do you mean by "how is it possible"? Do you mean how would you implement it in C++?


Sorry, my question wasn't clear. I guess I was asking for a breakdown on how Python interprets the for loop, but the C++ equivalents that you both provided was adequate in helping me understand. =)

It also helped that you pointed out that "collection" in for var in collection: must be iterable. I was under the mindset that the conditions only worked with number variables. Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1