2017-10-05 51 views
2
struct Instrument 
{ 
    std::string make; 
    std::string brand; 
    double age; 
    Instrument(std::string const& f, std::string const& s, double l) { BLABLA } 
    bool operator<(Instrumentconst& rhs) { return BLABLABLA;} 
}; 
std::map<int,Instrument> instMap; 

Instrument myObj(); 
instMap.insert(pair<int,Instrument>(10,myObj)); 

如果我需要分别更新此映射中各个属性的值。 每当我们从地图中检索到它时,我们是否会创建工具副本?C++ - 更新地图上的值

it = instMap.lower_bound(10); 
if (it != m.end()) 
    *(it->second).brand = "Jonas";<= will there be a copy of Instrument object created here? 

这是最有效的内存(和时间)方式来更新值 - 如果这项操作必须在一百万个条目上完成?

我们正在使用C++ 98

+1

请给一些[MCVE](http://stackoverflow.com/help/mcve) –

+8

您的代码,如呈现甚至不会编译。当您尝试通过调用'new'来插入对象时,您的'std :: map'('std :: map instMap;')存储了对象的实例,它将返回一个指针。请提供[mcve]。 –

+0

忽略拼写错误,您可以为'const char *'调用'std :: string'构造函数并调用复制赋值操作符。在C++ 98中没有捷径/更快的方法。 –

回答

3

简单的答案是否定的,仪器对象将不会被复制。

std::string将被复制,至少在C++ 98中。