【发布时间】:2017-04-27 16:39:07
【问题描述】:
我搜索了很多关于这个问题,但没有找到任何东西,如果重复,请见谅。
我在使用返回 *this 的类方法插入 std::map 时遇到问题。如果我尝试插入更多值,则实际上只插入第一个值。让我给你看我的代码:
using namespace std;
class test{
public:
test(){}
test Add(const int &a, const int &b);
void print(){
for (auto it = map1.begin(); it != map1.end(); ++it) {
cout << it->first << " " << it->second << endl;
}
}
private:
map<int,int> map1;
};
test test::Add(const int &a, const int &b) {
map1.insert(make_pair(a,b));
return *this;
}
但是当我尝试这样的事情时:
int main ( void ) {
test a;
a.Add(1,5) . Add( 4, 8);
a.print();
return 0;
}
只有第一个值被插入到地图中。我应该改变什么才能以这种方式插入地图?
非常感谢您的帮助。
【问题讨论】:
标签: c++ class dictionary stl this