iterator

Ruby's && operator

天涯浪子 提交于 2021-02-07 17:47:30
问题 In PHP, this evaluates to true : $a = 1 $b = 2 var_dump($a && $b); // true In ruby, this evaluates to 2 : a = 1 b = 2 p a && b # 2 Why does ruby return the value of the last statement (when the first is true and the second one is also true) and does not return a boolean? I have two arrays and I iterate them with an external iterator: a = [1,2,3].to_enum b = [5,6,7].to_enum c = [] begin while a_next = a.next && b_next = b.next result = a_next + b_next p "a[x] + b[x] = c[x] = #{a_next} + #{b

Ruby's && operator

*爱你&永不变心* 提交于 2021-02-07 17:47:19
问题 In PHP, this evaluates to true : $a = 1 $b = 2 var_dump($a && $b); // true In ruby, this evaluates to 2 : a = 1 b = 2 p a && b # 2 Why does ruby return the value of the last statement (when the first is true and the second one is also true) and does not return a boolean? I have two arrays and I iterate them with an external iterator: a = [1,2,3].to_enum b = [5,6,7].to_enum c = [] begin while a_next = a.next && b_next = b.next result = a_next + b_next p "a[x] + b[x] = c[x] = #{a_next} + #{b

Why can't I delete last element of vector

家住魔仙堡 提交于 2021-02-07 14:26:51
问题 I have stl vector consisting of several elements. I need to iterate through this vector and delete elements which meets some criteria. So I wrote this code for (int j = imageDataVector.size()-1; j >= 0; j--) { if(imageDataVector[i] < threshold) imageDataVector.erase(imageDataVector.end() - j); } This code works fine for almost all cases, however if all elements of vector meets the criteria I get an error: vector erase iterator outside the range This error occurs if I have only one element

Why can't I delete last element of vector

和自甴很熟 提交于 2021-02-07 14:24:27
问题 I have stl vector consisting of several elements. I need to iterate through this vector and delete elements which meets some criteria. So I wrote this code for (int j = imageDataVector.size()-1; j >= 0; j--) { if(imageDataVector[i] < threshold) imageDataVector.erase(imageDataVector.end() - j); } This code works fine for almost all cases, however if all elements of vector meets the criteria I get an error: vector erase iterator outside the range This error occurs if I have only one element

C++ recursive_directory_iterator miss some files

做~自己de王妃 提交于 2021-02-07 10:48:15
问题 I'm trying to get all files in directory through c++17 on my visual studio 2017 but I've just encountered a really weird problem. If I specify directory like this I can get all files without any problem: for (auto& p : std::filesystem::recursive_directory_iterator("C:\\Users\\r00t\\AppData\\Roaming\\Mozilla")) { if (std::filesystem::is_regular_file(p.path())) { std::cout << p.path() << std::endl; } } But I need all file list on APPDATA, and I'm trying to get path with getenv() function and

Passing a List Iterator to multiple Threads in Java

允我心安 提交于 2021-02-07 03:08:46
问题 I have a list that contains roughly 200K elements. Am I able to pass the iterator for this list to multiple threads and have them iterate over the whole lot, without any of them accessing the same elements? This is what I am thinking of at the moment. Main: public static void main(String[] args) { // Imagine this list has the 200,000 elements. ArrayList<Integer> list = new ArrayList<Integer>(); // Get the iterator for the list. Iterator<Integer> i = list.iterator(); // Create MyThread,

Passing a List Iterator to multiple Threads in Java

若如初见. 提交于 2021-02-07 03:08:24
问题 I have a list that contains roughly 200K elements. Am I able to pass the iterator for this list to multiple threads and have them iterate over the whole lot, without any of them accessing the same elements? This is what I am thinking of at the moment. Main: public static void main(String[] args) { // Imagine this list has the 200,000 elements. ArrayList<Integer> list = new ArrayList<Integer>(); // Get the iterator for the list. Iterator<Integer> i = list.iterator(); // Create MyThread,

Pythonic way to iterate over bits of integer

十年热恋 提交于 2021-02-05 20:21:30
问题 Let's a=109 or 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1] 回答1: There's a trick for just getting the 1's out of the binary representation without having to iterate over all the intervening 0's: def bits(n): while n: b = n & (~n+1) yield b n ^= b >>> for b in bits(109): print(b) 1 4 8 32 64 回答2: My approach: def bits(number): bit = 1 while number >= bit: if number & bit: yield bit bit <<= 1 I don't think there is a builtin function for it. I also wonder

Pythonic way to iterate over bits of integer

旧城冷巷雨未停 提交于 2021-02-05 20:19:39
问题 Let's a=109 or 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1] 回答1: There's a trick for just getting the 1's out of the binary representation without having to iterate over all the intervening 0's: def bits(n): while n: b = n & (~n+1) yield b n ^= b >>> for b in bits(109): print(b) 1 4 8 32 64 回答2: My approach: def bits(number): bit = 1 while number >= bit: if number & bit: yield bit bit <<= 1 I don't think there is a builtin function for it. I also wonder

Pythonic way to iterate over bits of integer

大城市里の小女人 提交于 2021-02-05 20:19:06
问题 Let's a=109 or 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1] 回答1: There's a trick for just getting the 1's out of the binary representation without having to iterate over all the intervening 0's: def bits(n): while n: b = n & (~n+1) yield b n ^= b >>> for b in bits(109): print(b) 1 4 8 32 64 回答2: My approach: def bits(number): bit = 1 while number >= bit: if number & bit: yield bit bit <<= 1 I don't think there is a builtin function for it. I also wonder