简体   繁体   English

将Python转换为C ++-Lambda中的索引

[英]Converting Python to C++ - index in lambda

I am new to python, and I am trying to convert some python code to C++. 我是python的新手,我正在尝试将一些python代码转换为C ++。

path is a string, WORDS is a list of strings. path是一个字符串,WORDS是一个字符串列表。

python snippit: python snippit:

suggestions = filter(lambda x: x[0] == path[0] and x[-1] == path[-1], WORDS)

Because I do not know python at all, I have trouble understanding the notion of 0 and -1. 因为我根本不了解python,所以我很难理解0和-1的概念。 Do they represent indexes of these arrays, like i and i-1 would in C++? 它们是否代表这些数组的索引,就像ii-1在C ++中一样?

Here is my attempt to convert: 这是我的转换尝试:

  vector<string>suggestions; 
int len = path.length();

  for (string &x : dictionary) //for each word in some dictionary
      for (int i = 0; i < len; i++)
        {
          if (path[i] == x[i] && path[i-1] ==x[i-1])
            suggestions.push_back(x);
            break;
        }

Even though I think it should be right, this code isn't doing what I want it to do. 即使我认为应该是对的,该代码也没有按照我想要的去做。 The filtered list from the python code is much shorter than the vector from the C++ code. python代码中的过滤列表比C ++代码中的向量短得多。

A string is a sequence in python, so the indicies you're asking about are referring to specific characters. 字符串是python中的一个序列,因此您要查询的标记是指特定字符。 Index 0 means the same thing in Python as C++. 索引0在Python中的含义与C ++相同。 Negative indexes in python count backwards from the end of the sequence, so Index -1 is the last character in the string. python中的负索引从序列末尾开始倒数,因此索引-1是字符串中的最后一个字符。

vector<string> suggestions; 
int len = path.length();

for (string &x : dictionary) //for each word in some dictionary
  if(path[0] == x.front() && path[len-1] == x.back())
    suggestions.push_back(x);

The lambda creates a function that takes a string and checks the first and last characters. Lambda创建一个接受字符串并检查第一个和最后一个字符的函数。 That function is then passed into filter , which calls it on each element of WORDS . 然后将该函数传递到filter ,该filterWORDS每个元素上调用它。 filter returns an iterable of all the words for which the lambda returned true. filter返回lambda返回true的所有单词的可迭代值。

I have trouble understanding the notion of 0 and -1. 我很难理解0和-1的概念。

See [ Negative index to Python list ] and [ Explain Python's slice notation ]. 请参见[ Python列表的负索引 ]和[ 解释Python的切片符号 ]。

Negative numbers mean that you count from the right instead of the left. 负数表示您是从右数而不是从左数。 So, list[-1] refers to the last element, list[-2] is the second-last, and so on. 因此,list [-1]指向最后一个元素,list [-2]指向倒数第二个,依此类推。

Index 0 is the first element, and index -1 is the last element. 索引0是第一个元素,索引-1是最后一个元素。 If foo contains [4, 6, 8] , foo[0] would be 4 and foo[-1] would be 8 . 如果foo包含[4, 6, 8] foo [4, 6, 8] ,则foo[0]将为4foo[-1]将为8

In the case of a string, they are the first and last character respectively. 对于字符串,它们分别是第一个和最后一个字符。 So, string::front() and string::back() in C++11, otherwise access with 0 and length-1 as normal. 因此,在C ++ 11中为string::front()string::back() ,否则正常使用0和length-1进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM