如何使用 C++ std 计算整数集合的前导零和尾随零
How to use the C++ std to count leading and trailing zeros of a collection of integers
我们可以通过调用标准库来替换循环来计算整数集合中的前导零吗?
我正在学习 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
相关文章:
- 处理多个异常集合的C++方法
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- 如何将ampl中的集合表示为c++中的向量
- 检查值是否在集合p1和p2中,但不在p3中
- 带过滤器的现代迭代c++集合
- 如何将结构插入到集合中并打印集合的成员
- 集合上的输出迭代器:assign和increment迭代器
- 如何将函数集合传递给客户端类,以便将它们当作客户端类本身的成员使用
- 实现一个在集合上迭代的模板函数
- 尝试使用集合函数时出现分段错误
- 互斥指针的集合
- c++找不到具有相同哈希的无序集合元素
- python集合的C++等价物是什么.计数器
- 如何在C++中递归地按相反顺序打印集合
- Python 集合.计数器,如何避免重复查找
- 如何写向量的无序向量集,即unordered_set<向量<向量<int>>集合?
- 从具有自定义排序的向量创建集合
- 显示字符串的集合和子集
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 有没有办法在 c++ 中按更大的 int 和更小的对对 int 和对的集合进行排序?