﻿ 数组-如何通过过滤快速就地改变整数数组 - 代码日志

数组-如何通过过滤快速就地改变整数数组

``````var numbers = Array(1...1000000)
numbers = numbers.filter( { return \$0 % 2 == 0  } )
``````

number.MutablefilterOperation({return \$0％2 == 0})

``````std::vector<int> originalNumbers(1000000);
std::vector<int> newNumbers;
std::copy_if (originalNumbers.begin(), originalNumbers.end(), std::back_inserter(newNumbers), [](int i) { return i % 2 == 0 } );
``````

``````std::vector<int> originalNumbers(1000000);
auto pos = std::remove_if(originalNumbers.begin(), originalNumbers.end(), [](int x) { return x % 2 == 0; });
originalNumbers.erase(pos, originalNumbers.end());
``````

Structures and Enumerations Are Value Types

``````extension Array {
// the mutating function should be named "filter" but
// this is already taken by the non-mutating function
mutating func filtering(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element] {
try self = self.filter(isIncluded)
return self
}
}

var numbers = Array(1...10)
numbers.filtering( { return \$0 % 2 == 0  } )
print(numbers) // "[2, 4, 6, 8, 10]\n"
``````