模式"allocate memory or use existing data"
Pattern "allocate memory or use existing data"
I have fieldstd::map<std::string, std::map<unsigned int, float>> widths;
我根据运行时标准将数据从另一个地图复制到宽度[键]或将自定义数据插入宽度[键]。但是,复制速度太慢。 我将使用指向 std::map 所以你想根据一些条件引用另一个容器中的 在这种情况下,您需要一个指针映射,例如: 使用std::map<unsigned int, float>
?std::map<std::string, std::map<unsigned int, float>*>
.这样,您将能够执行以下操作:if(criteria)
widths[key] = &targetMap;
std::map
对内容进行排序然后迭代并不是一个坏主意,但当然确实取决于真实的上下文。通过推测,我们只能说这是有史以来最好的方法,而永远不要使用其他方法,这是没有意义的
创建以下包装器:
class Widths
{
public:
Widths() : widths(new std::map<unsigned int, float>), to_be_deleted(true)
{
}
Widths(const std::map<unsigned int, float> *arg) noexcept : widths(const_cast<std::map<unsigned int, float>*>(arg)),
to_be_deleted(false)
{
}
~Widths() noexcept
{
if (to_be_deleted) delete widths;
}
Widths(Widths &&arg) : widths(arg.widths), to_be_deleted(arg.to_be_deleted)
{
arg.widths = nullptr;
arg.to_be_deleted = false;
}
Widths(const Widths &arg) = delete;
Widths& operator=(const Widths &arg) = delete;
const std::map<unsigned int, float>* operator->() const
{
return widths;
}
std::map<unsigned int, float>* operator->()
{
return widths;
}
private:
std::map<unsigned int, float> *widths;
bool to_be_deleted;
};
std::map<std::string, std::map<unsigned int, float>> widths;
更改为std::map<std::string, Widths> widths;
相关文章:
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 收到错误"invalid use of non-static data member 'stu::n' "
- C++ 初始化 .data 部分中的变量
- 模式"allocate memory or use existing data"
- boost::asio data owning `ConstBufferSequence`
- Python to C++ Data structure API
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- protobuf C++ SQLite handle blob data
- 错误:字段'dateOfBirth'的类型不完整'Poco::Data::Date'
- 为什么构造函数 Message(const T&data) 与 Message(T&& data) 冲突,当 T = int&时?
- "thread-safe data"与"thread-safe code/functions"的区别
- 如何找到目标文件 *.o 的 ram rom 使用情况(.bss .text .rodata .data)?
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- 使用 .data() 将字符数组转换为 std::string 不会转换整个数组
- *(int*)&data[18]在这段代码中实际上做了什么?
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 构造中错误:未在此范围中声明"data"
- 我可以在初始化之前使用 std::array 成员变量中的 data() 指针吗?发出警告
- 如何将data[i].int转换为vaible