如何使用 C++ std 计算整数集合的前导零和尾随零

How to use the C++ std to count leading and trailing zeros of a collection of integers

本文关键字:集合 C++ 何使用 std 计算 整数      更新时间:2023-10-16

我们可以通过调用标准库来替换循环来计算整数集合中的前导零吗?

我正在学习 std,但在这种情况下找不到使用 count 或 count_if 之类的方法,因为我需要知道前一个元素。

int collection[] = { 0,0,0,0,6,3,1,3,5,0,0 };
auto collectionSize = sizeof(collection) / sizeof(collection[0]);
auto countLeadingZeros = 0;
for (auto idx = 0; idx < collectionSize; idx++)
{
if (collection[idx] == 0)
countLeadingZeros++;
else
break;
}
// leading zeros: 4*0
cout << "leading zeros: " << countLeadingZeros << "*0" << endl;

我有一个类似的案例来计算同一集合中的尾随零。

auto countTrailingZeros = 0;
for (auto idx = collectionSize - 1; idx >= 0; idx--)
{
if (collection[idx] == 0)
countTrailingZeros++;
else
break;
}
// trailing zeros: 2*0
cout << "trailing zeros: " << countTrailingZeros << "*0" << endl;

下面是一个完整的构建示例。

#include <iostream>
using namespace std;
int main()
{
int collection[] = { 0,0,0,0,6,3,1,3,5,0,0 };
auto collectionSize = sizeof(collection) / sizeof(collection[0]);
auto countLeadingZeros = 0;
for (auto idx = 0; idx < collectionSize; idx++)
{
if (collection[idx] == 0)
countLeadingZeros++;
else
break;
}
cout << "leading zeros: " << countLeadingZeros << "*0" << endl;
auto countTrailingZeros = 0;
for (auto idx = collectionSize - 1; idx >= 0; idx--)
{
if (collection[idx] == 0)
countTrailingZeros++;
else
break;
}
cout << "trailing zeros: " << countTrailingZeros << "*0" << endl;

return 0;
}

一种方法是使用std::find_if

auto countLeadingZeros = std::find_if(
std::begin(collection), std::end(collection),
[](int x) { return x != 0; }) - std::begin(collection);
auto countTrailingZeros = std::find_if(
std::rbegin(collection), std::rend(collection),
[](int x) { return x != 0; }) - std::rbegin(collection);

您可以使用std::find_if和反向迭代器

#include <iostream>
#include <algorithm>
#include <vector>
using std::cout;
using std::endl;
int main() {
auto vec = std::vector<int>{0, 0, 1, 0};
auto first_non_zero = std::find_if(vec.cbegin(), vec.cend(),
[](auto integer) {
return integer != 0;
});
auto first_non_zero_end = std::find_if(vec.crbegin(), vec.crend(),
[](auto integer) {
return integer != 0;
});
auto leading_zeros = std::distance(vec.cbegin(), first_non_zero);
auto trailing_zeros = std::distance(vec.crbegin(), first_non_zero_end);
cout << "leading zeros " << leading_zeros << endl;
cout << "trailing zeros " << trailing_zeros << endl;
}

您可以使用标准算法std::find_if或标准算法std::find_if_not

这是一个使用标准算法的演示程序std::find_if_not.

#include <iostream>
#include <functional>
#include <iterator>
#include <algorithm>
int main()
{
int collection[] = { 0, 0, 0, 0, 6, 3, 1, 3, 5, 0, 0 };
std::size_t n = 0;
auto first = std::find_if_not(std::begin(collection),
std::end(collection), 
std::bind2nd(std::equal_to<int>(), 0));
n += std::distance(std::begin(collection), first);
auto last = std::find_if_not(std::rbegin(collection),
std::reverse_iterator<int *>( first ),
std::bind2nd(std::equal_to<int>(), 0));
n += std::distance(std::rbegin(collection), last );
std::cout << n << std::endl;
return 0;
}

程序输出为

6