2

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?

4
  • 1
    you can use itr->first syntax instead of (*itr).first, although it's just a matter of taste. Commented Dec 1, 2011 at 17:42
  • image is defined as ['10001','10100','00000','00000','00111','00100','00100'] Commented Dec 1, 2011 at 17:46
  • In the C++ code? What kind of data is that? 2D array with chars ? Commented Dec 1, 2011 at 17:48
  • I am using int and not char like std::vector<std::vector<int> > image; Commented Dec 1, 2011 at 17:54

5 Answers 5

2

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()
{}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

and they are the same as width? Plus, in Python, you compare the image with chars (== '0'), and here with ints ( == 0 ). If this is correct, everything else is fine.
additionally the python snippet is a list comprehension so OP should probably push the m and n into a list if the condition of the if is true
@soulcheck - probably yes, but we don't know what's after the if's condition.
@KirilKirov aye, though we know that the python snippet ends with ] and has list comprehension syntax so whatever he does with m and n will get pushed some list
1

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.

4 Comments

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int for list declaration
Which version, (I posted two), which line, and with or without C++0x support? NB, I just edited to qualify std::pair, since I omitted using namespace std; along with the includes etc.
following is the real error : error C2552: 'list' : non-aggregates cannot be initialized with initializer list
The first version, which specifically requires C++0x support for extended initializer lists? Try the second version of the declaration instead then, it's marginally more verbose but doesn't require C++0x support
1

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 (...

Comments

1

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'){}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.