Class 的容器 get() 成员函数使用 vs 复制
Class's container get() member function usage vs copying
我创建了一个 config
class,它从配置 YAML
加载配置。
我为每种类型创建了矢量容器
// Pseudo Code
class config
{
private:
std::vector<std::string> c_name;
public:
config(yaml_file_path)
{
// Handles reading from yaml and loading data to c_name container.
load(yaml_file_path);
}
std::vector<std::string> get_name()
{
return c_name;
}
};
我在其他class中使用此对象来获取名称配置。
class loadConfig
{
config cfg(yaml_file_path);
std::vector<std::string> name = cfg.get_name();
// Further use of vector name like checks.
}
问题 : 什么会更好?(因为代码练习和执行时间and/or内存space)
- 在代码的各个地方使用
get_name()
函数。或者
- 像我一样在容器中复制值?
What would be better?( as code practice and execution time and/or memory space)
您的 get_name()
函数会在每次调用时复制容器。这非常昂贵并且不需要,只要您不想在 class.
之外修改它
我建议改用一个/两个重载,以便编译器可以选择您调用的(非 const/const)对象:
// for the non-const `config` objects call
std::vector<std::string>& get_name() /* noexcept */ {
return c_name;
}
// for the const `config` objects
const std::vector<std::string>& get_name() const /* noexcept */ {
return c_name;
}
现在来电者可以
auto& name = cfg.get_name(); // non-const ref for further modifications
或
const auto& name = cfg.get_name(); // const-ref for read only purposes.
在这两种情况下,您都不会复制容器。
也就是说,对于 class 类,例如 config
,它只有一个容器作为内部存储,我个人最喜欢的是通过提供begin
和 end
重载:
class config
{
std::vector<std::string> c_name;
public:
auto begin() noexcept { return c_name.begin(); }
auto cbegin() const noexcept { return c_name.cbegin(); }
auto end() noexcept { return c_name.end(); }
auto cend() noexcept { return c_name.cend(); }
};
这使得你编写的代码如>
config names;
for (auto& name : names) // or for (auto const& name : names)
{
// ...do something with names
}
我创建了一个 config
class,它从配置 YAML
加载配置。
我为每种类型创建了矢量容器
// Pseudo Code
class config
{
private:
std::vector<std::string> c_name;
public:
config(yaml_file_path)
{
// Handles reading from yaml and loading data to c_name container.
load(yaml_file_path);
}
std::vector<std::string> get_name()
{
return c_name;
}
};
我在其他class中使用此对象来获取名称配置。
class loadConfig
{
config cfg(yaml_file_path);
std::vector<std::string> name = cfg.get_name();
// Further use of vector name like checks.
}
问题 : 什么会更好?(因为代码练习和执行时间and/or内存space)
- 在代码的各个地方使用
get_name()
函数。或者 - 像我一样在容器中复制值?
What would be better?( as code practice and execution time and/or memory space)
您的 get_name()
函数会在每次调用时复制容器。这非常昂贵并且不需要,只要您不想在 class.
我建议改用一个/两个重载,以便编译器可以选择您调用的(非 const/const)对象:
// for the non-const `config` objects call
std::vector<std::string>& get_name() /* noexcept */ {
return c_name;
}
// for the const `config` objects
const std::vector<std::string>& get_name() const /* noexcept */ {
return c_name;
}
现在来电者可以
auto& name = cfg.get_name(); // non-const ref for further modifications
或
const auto& name = cfg.get_name(); // const-ref for read only purposes.
在这两种情况下,您都不会复制容器。
也就是说,对于 class 类,例如 config
,它只有一个容器作为内部存储,我个人最喜欢的是通过提供begin
和 end
重载:
class config
{
std::vector<std::string> c_name;
public:
auto begin() noexcept { return c_name.begin(); }
auto cbegin() const noexcept { return c_name.cbegin(); }
auto end() noexcept { return c_name.end(); }
auto cend() noexcept { return c_name.cend(); }
};
这使得你编写的代码如>
config names;
for (auto& name : names) // or for (auto const& name : names)
{
// ...do something with names
}