C++ 我需要有人给我解释这些代码
我需要有人逐行给我解释这个代码。我特别不懂这句话:C++ 我需要有人给我解释这些代码,c++,stl,C++,Stl,我需要有人逐行给我解释这个代码。我特别不懂这句话: operator std::map<T, U>() 操作符std::map() 多谢各位 template <typename T, typename U> class create_map { std::map<T, U> m_map; public: create_map(const T& key, const U& val) { m_
operator std::map<T, U>()
操作符std::map()
多谢各位
template <typename T, typename U>
class create_map
{
std::map<T, U> m_map;
public:
create_map(const T& key, const U& val)
{
m_map[key] = val;
}
create_map<T, U>& operator()(const T& key, const U& val)
{
m_map[key] = val;
return *this;
}
operator std::map<T, U>()
{
return m_map;
}
};
模板
类创建映射
{
std::map mu map;
公众:
创建地图(常数T和键、常数U和值)
{
m_map[key]=val;
}
创建映射和运算符()
{
m_map[key]=val;
归还*这个;
}
运算符std::map()
{
返回m_图;
}
};
我还建议您进一步探索std::map
,尤其是std::map
中重载的操作符[]
我还建议您进一步探索std::map
,尤其是std::map
行中重载的操作符[]
operator std::map<T, U>()
操作符std::map()
定义一个函数,当您的create_map对象像代码中的std::map一样使用时,将调用该函数
一个更简单的例子是:
class A
{
public:
operator int()
{
return 3;
}
};
int main()
{
A a;
cout << a << endl;
}
A类
{
公众:
运算符int()
{
返回3;
}
};
int main()
{
A A;
不能
operator std::map<T, U>()
操作符std::map()
定义一个函数,当您的create_map对象像代码中的std::map一样使用时,将调用该函数
一个更简单的例子是:
class A
{
public:
operator int()
{
return 3;
}
};
int main()
{
A a;
cout << a << endl;
}
A类
{
公众:
运算符int()
{
返回3;
}
};
int main()
{
A A;
cout关于它没有多少需要了解的。操作符std::map()
重写类的转换操作符(不带参数)提供类型为std::map
的对象实例std::map
是用于关联键->值存储的STL标准类。在您的示例中,它从类型为T
的键映射到类型为U
的值T
和U
到目前为止尚未定义(您编写了模板类
,但模板参数在哪里?)
转换操作符允许使用类实例来代替操作符提供转换的类型,如下所示
class foo {
operator char const *() {
return "foo instance as char const *";
}
};
// ...
void bar(foo &f)
{
// here the class instance is used as if it were a char const *
cout << f << endl;
}
class-foo{
运算符char const*(){
返回“foo instance as char const*”;
}
};
// ...
空栏(foo&f)
{
//在这里,类实例被当作一个char const来使用*
cout关于它没有多少需要了解的。操作符std::map()
重写类的转换操作符(不带参数)提供类型为std::map
的对象实例std::map
是用于关联键->值存储的STL标准类。在您的示例中,它从类型为T
的键映射到类型为U
的值T
和U
到目前为止尚未定义(您编写了模板类
,但模板参数在哪里?)
转换操作符允许使用类实例来代替操作符提供转换的类型,如下所示
class foo {
operator char const *() {
return "foo instance as char const *";
}
};
// ...
void bar(foo &f)
{
// here the class instance is used as if it were a char const *
cout << f << endl;
}
class-foo{
运算符char const*(){
返回“foo instance as char const*”;
}
};
// ...
空栏(foo&f)
{
//在这里,类实例被当作一个char const来使用*
cout代码:
由于该类没有默认构造函数,因此无法使用它创建空映射。这可能是设计造成的,也可能是设计错误
最后的const
允许将create\u map
声明为const
并使用
在转换为引用时,存在与使用引用参数相同的问题,即理论上可能出现别名,保留对const
引用的代码不准备处理引用对象的更改。但实际上这不是问题。例如,作为形式参数,只需编写std::string const&s
(而不仅仅是std::string s
)这是理所当然的事,如果由此导致任何错误的话,我几乎不会遇到任何问题
干杯&hth.,代码:
由于该类没有默认构造函数,因此无法使用它创建空映射。这可能是设计造成的,也可能是设计错误
最后的const
允许将create\u map
声明为const
并使用
在转换为引用时,存在与使用引用参数相同的问题,即理论上可能出现别名,保留对const
引用的代码不准备处理引用对象的更改。但实际上这不是问题。例如,作为形式参数,只需编写std::string const&s
(而不仅仅是std::string s
)这是理所当然的事,如果由此导致任何错误的话,我几乎不会遇到任何问题
干杯&hth.,请正确重新格式化代码(在编辑器中选择代码并单击“代码”工具按钮)请正确重新格式化代码(在编辑器中选择代码并单击“代码”工具按钮)对不起,我真的不明白。我真的需要一行一行的解释,这非常重要,谢谢。所以它只是在必要时将create_map对象转换为map对象?请你解释一下另一个操作符重载?它是如何使用的?对不起,我有点不明白。非常感谢。@jose:一般来说,不用麻烦了告诉我们这“很重要”。这对我们来说并不重要。我们尽力帮助别人是因为我们喜欢帮助别人,但你的紧迫感对我们来说并没有什么影响。充其量,我们会像对待其他人一样对待你的问题,在最坏的情况下,你会把你的紧迫感推到那些试图帮助你的人身上,从而激怒他们。:)对不起,我真的不明白。我真的需要一行一行的解释,这非常重要,谢谢。所以它只是在必要时将创建映射对象转换为映射对象?并且
class foo {
operator char const *() {
return "foo instance as char const *";
}
};
// ...
void bar(foo &f)
{
// here the class instance is used as if it were a char const *
cout << f << endl;
}
template <typename T, typename U>
class create_map
{
std::map<T, U> m_map;
public:
create_map(const T& key, const U& val)
{
m_map[key] = val;
}
create_map<T, U>& operator()(const T& key, const U& val)
{
m_map[key] = val;
return *this;
}
operator std::map<T, U>()
{
return m_map;
}
};
create_map<int, std::string>( 1, "blah" )( 2, "hah" )( 3, "doh" )
operator std::map<T, U>()
{
return m_map;
}
operator std::map<T, U> const& () const
{
return m_map;
}