C++ 使用类方法插入std::map

C++ 使用类方法插入std::map,c++,class,dictionary,stl,this,C++,Class,Dictionary,Stl,This,我搜索了很多这个问题,但没有找到任何东西,所以很抱歉,如果它是重复的 我在使用返回*this的class方法插入std::map时遇到问题。如果我尝试插入更多值,实际上只插入第一个值。让我向您展示我的代码: using namespace std; class test{ public: test(){} test Add(const int &a, const int &b); void print(){ for (auto it = map1.b

我搜索了很多这个问题,但没有找到任何东西,所以很抱歉,如果它是重复的

我在使用返回*this的class方法插入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;

}
仅将第一个值插入到贴图中。我应该更改什么以这样插入地图

非常感谢您的帮助。

您的错误是

test Add(const int &a, const  int &b);
按值返回。这意味着从
Add
返回的
test
与您启用的
test
不同,它是一个副本。这意味着当你做类似的事情时

a.Add(1,5) . Add( 4, 8);
。添加(4,8)
parts将项目添加到
a
的副本中,而不是
a
本身

解决此问题的方法是通过引用而不是通过值返回。当您通过引用返回时,您将使用您调用的
add
项目,而不是副本。这意味着您只需要将函数签名更改为

test& Add(const int &a, const  int &b)
对于声明和定义。

您的错误是

test Add(const int &a, const  int &b);
按值返回。这意味着从
Add
返回的
test
与您启用的
test
不同,它是一个副本。这意味着当你做类似的事情时

a.Add(1,5) . Add( 4, 8);
。添加(4,8)
parts将项目添加到
a
的副本中,而不是
a
本身

解决此问题的方法是通过引用而不是通过值返回。当您通过引用返回时,您将使用您调用的
add
项目,而不是副本。这意味着您只需要将函数签名更改为

test& Add(const int &a, const  int &b)

对于声明和定义。

插入一次的原因是您的
Add
方法按值返回,而您的第二个
方法按值返回。添加(4,8)最终插入到另一个地图中。若要解决此问题,您需要更改
添加
,以返回对
*此
的引用:

using namespace std;

class test
{
public:
    test() {}
    test& Add(const int &a, const  int &b); // < -- changed to return test&
    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) // < -- changed to return test&
{
    map1.insert(make_pair(a, b));
    return *this;
}

插入一次的原因是因为
Add
方法按值返回,第二个
方法返回。添加(4,8)最终插入到另一个地图中。若要解决此问题,您需要更改
添加
,以返回对
*此
的引用:

using namespace std;

class test
{
public:
    test() {}
    test& Add(const int &a, const  int &b); // < -- changed to return test&
    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) // < -- changed to return test&
{
    map1.insert(make_pair(a, b));
    return *this;
}

问题是
Add
函数返回的是当前对象的副本。要返回当前对象本身,需要将返回类型更改为引用:

test & test::Add(const int &a, const int &b) {
// ^^^^^
  map1.insert(make_pair(a,b));
  return *this;
}
当前代码将第二个
Add
应用到与第一个
Add
应用到的对象不同的对象,因此您永远看不到效果。但是,对于笑声和笑声,您也可以尝试:

a.Add(1,5) . Add( 4, 8) . print();

问题是
Add
函数返回的是当前对象的副本。要返回当前对象本身,需要将返回类型更改为引用:

test & test::Add(const int &a, const int &b) {
// ^^^^^
  map1.insert(make_pair(a,b));
  return *this;
}
当前代码将第二个
Add
应用到与第一个
Add
应用到的对象不同的对象,因此您永远看不到效果。但是,对于笑声和笑声,您也可以尝试:

a.Add(1,5) . Add( 4, 8) . print();

谢谢!它解决了这个问题。没问题。很高兴能帮忙。非常感谢!它解决了这个问题。没问题。很乐意帮忙。