[英]Python and C++ code comparison
I have the following python
code我有以下
python
代码
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. image
是数组定义的,也定义了i
和j
。
Following is how I have converted this into C++
以下是我如何将其转换为
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?这个转换正确吗?
Almost.几乎。 You have two differences: in
Python
, you have i+m<b
and j+n<l
, which makes me think b!=l
.你有两个区别:在
Python
,你有i+m<b
和j+n<l
,这让我觉得b!=l
。
In your C++
code, you have i + m < width
and j + n < width
, where width
is the same.在您的
C++
代码中,您有i + m < width
和j + n < width
,其中width
相同。
If width == b == l
, then everything's fine.如果
width == b == l
,那么一切都很好。
Actually, depends on how image
is defined.实际上,取决于
image
的定义方式。 The image[i + m][j + n ] == 0
is what bothers me (the part with ==0
) image[i + m][j + n ] == 0
是困扰我的东西(带有==0
的部分)
As the @Avinash comment says, image is vector< vector< int > >
, so the code is fine.正如@Avinash 评论所说,图像是
vector< vector< int > >
,所以代码很好。
As another person remarked, the width
used in two places is probably incorrect.正如另一个人所说,两个地方使用的
width
可能不正确。
Assuming that, here's a comparision of direct translation from Python versus C++-like code:假设,这是从 Python 直接翻译与类似 C++ 的代码的比较:
#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()
{}
The following works under c++1z:以下在 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'){}
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如果你被允许使用 C++0x,或者
const struct { int first, second; } list[] = { {-1,1}, {-1,0}, {-1,-1} };
...
if not.如果不。 Otherwise, the translation looks plausible.
否则,翻译看起来是合理的。
If you don't try to reproduce Python idioms in C++, the code can be simplified to:如果您不尝试在 C++ 中重现 Python 习语,则可以将代码简化为:
for (int n = 1; n >= -1; --n) {
const int m = -1;
if (...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.