c++ - 删除向量的元素并在c ++中保留空白空间
解决方案
没有“空”之类的东西int
,因此连续的int
s 序列,例如 astd::vector<int>
不能有“空”元素。
您可以使用std::optional<int>
which 确实具有“没有价值”的概念
#include <iostream>
#include <vector>
#include <optional>
#include <algorithm>
template<typename T>
std::ostream& operator<<(std::ostream & os, const std::vector<std::optional<T>> & vec)
{
for (auto & el : vec)
{
if (el)
os << *el << ' ';
else
os << " ";
}
return os << '\n';
}
int main()
{
std::vector<std::optional<int>> vals = { 1, 2, 3, 4, 5, 6, 7 };
std::cout << vals;
*std::find(vals.begin(), vals.end(), 5) = std::nullopt;
*std::find(vals.begin(), vals.end(), 6) = std::nullopt;
std::cout << vals;
return 0;
}
推荐阅读
- python - 使用阈值的条件列
- mongodb - 从 WiredTiger 分片存档导入本地 mongodb 数据
- python - 如何使用 tkinter 上的图标图像制作主页按钮?
- python - Python pyplot colorbar和colormap有不同的颜色
- ansible - Openshift 3集群安装问题
- javascript - 让 Babel 识别 HTML 模板标签
- node.js - 收到此错误“引发了跨域错误。React 无权访问开发中的实际错误对象。”
- android - java.lang.RuntimeException:仅当 proguard 启用时存根 true
- debezium - 集群 JVM 中的 Debezium 方法
- c# - ASP Core 3.1 路由