将矢量的元素向左推
Pushing elements of the vector to the left
本文关键字:元素 更新时间:2023-10-16
31 04 00 08
假设这是输入向量文件:
我该如何生成?
31 48 00 00
我想将所有非零数字从"04"推到左边。请帮忙!!
您可以使用以下内容在通过向量进行单次传递中执行此操作:
vector<int> v = {3, 1, 0, 4, 0, 0, 0, 8};
auto zero_iter = find(v.begin(), v.end(), 0);
for (auto curr_iter = zero_iter; curr_iter != v.end(); curr_iter++) {
if (*curr_iter != 0) {
swap(*curr_iter, *zero_iter);
zero_iter++;
}
}
这使用<algorithm>
中的std::find
和std::swap
。
这里的想法是跟踪第一个可用的零位置的位置,并在此之后找到非零位置。每次找到非零数时,您都会将其交换到第一个可用的零位置,并递增零位置以指向下一个位置。
至少在我看来
,到目前为止给出的解决方案有些不理想。
标准库提供了专门为手头的任务设计的算法。您正在尝试将输入划分为非零数字,后跟零。为此,std::stable_partition
将正常工作:
std::string input = "31040008";
std::stable_partition(input.begin(), input.end(),
[](auto c) { return c != '0'; });
结果:
31480000
如果您不关心非零数的顺序(只是它们都在零之前(,那么使用std::partition
可能会获得一点速度。
您可以使用
std::remove
删除所有 0。事实上,它只会将所有不为零的元素向左推,并返回要擦除的部分的开头。但是,您可以用 0 填充它,而不是擦除它:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = { 3, 1, 0, 4, 0, 0, 0, 8 };
std::fill(std::remove(v.begin(), v.end(), 0), v.end(), 0);
//test
for (auto d : v)
std::cout << d << " ";
std::cout << std::endl;
return 0;
}
指纹:
3 1 4 8 0 0 0 0
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 从控制台中删除最后打印的元素
- 擦除while循环中迭代的元素