Avinash
Avinash

Reputation: 13257

Python and C++ code comparison

I have the following python code

for m,n in [(-1,1),(-1,0),(-1,-1)] if 0<=i+m<b and 0<=j+n<l and image[i+m][j+n] == '0']

image is array defined and i and j is also defined.

Following is how I have converted this into C++

std::vector<std::pair<int,int> > direction;
direction.push_back(std::make_pair(-1,1));
direction.push_back(std::make_pair(-1,0));
direction.push_back(std::make_pair(-1,-1));
for ( std::vector<std::pair<int,int> >::iterator itr = direction.begin(); 
                   itr != direction.end(); ++itr) {
    int m = (*itr).first;
    int n = (*itr).second;
   if ( (0 <= i + m && i + m < width ) && 
                   (0 <= j + n && j + n < width ) && 
                   image[i + m][j + n ] == 0) {
}

Is this conversion correct?

Upvotes: 2

Views: 1312

Answers (5)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

As another person remarked, the width used in two places is probably incorrect.

Assuming that, here's a comparision of direct translation from Python versus C++-like code:

#include <iostream>
#include <list>
#include <utility>
#include <vector>
using namespace std;

void likeCPlusPlus()
{
    int i = 666, j = 666, width = 666, height = 666, image[666][666];

    for( int dy = 1;  dy >= -1;  --dy )
    {
        int const   dx  = -1;
        int const   x   = i + dx;
        int const   y   = j + dy;

        if(
            0 <= x && x < width &&
            0 <= y && y < height &&
            image[x][y] == 0
            )
        {}
    }
}

void likePythonInCPlusPlus()
{
    int i = 666, j = 666, width = 666, image[666][666];

    std::vector<std::pair<int,int> > direction;
    direction.push_back(std::make_pair(-1,1));
    direction.push_back(std::make_pair(-1,0));
    direction.push_back(std::make_pair(-1,-1));
    for ( std::vector<std::pair<int,int> >::iterator itr = direction.begin(); 
                       itr != direction.end(); ++itr)
    {
        int m = (*itr).first;
        int n = (*itr).second;
        if ( (0 <= i + m && i + m < width ) && 
                       (0 <= j + n && j + n < width ) && 
                       image[i + m][j + n ] == 0)
        {}
    }
}

int main()
{}

Upvotes: 2

Ekalavya
Ekalavya

Reputation: 989

The following works under c++1z:

#include <vector>
using namespace std;
for( auto [m,n] : vector<tuple<int,int> >{{-1,1}, {-1,0}, {-1,-1}})
  if(0<=i+m<b and 0<=j+n<l and image[i+m][j+n] == '0'){}

Upvotes: 1

Kiril Kirov
Kiril Kirov

Reputation: 38173

Almost. You have two differences: in Python, you have i+m<b and j+n<l, which makes me think b!=l.

In your C++ code, you have i + m < width and j + n < width, where width is the same.

If width == b == l, then everything's fine.

Actually, depends on how image is defined. The image[i + m][j + n ] == 0 is what bothers me (the part with ==0)

As the @Avinash comment says, image is vector< vector< int > >, so the code is fine.

Upvotes: 2

Ferruccio
Ferruccio

Reputation: 100658

If you don't try to reproduce Python idioms in C++, the code can be simplified to:

for (int n = 1; n >= -1; --n) {
    const int m = -1;
    if (...

Upvotes: 1

Useless
Useless

Reputation: 67733

You don't need to build that vector at runtime, if it's really a hardcoded constant. Just do:

const std::pair<int,int> list[] = { {-1,1}, {-1,0}, {-1,-1} };
for (int index = 0; index < sizeof(list)/sizeof(*list); ++index)
{
    int m = list[index].first;
    int n = list[index].second;
    ...
}

if you're allowed C++0x, or

const struct { int first, second; } list[] = { {-1,1}, {-1,0}, {-1,-1} };
...

if not. Otherwise, the translation looks plausible.

Upvotes: 1

Related Questions