Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过引用返回stl映射的正确方法/此行为是否定义良好?_C++_Stl_Reference - Fatal编程技术网

C++ 通过引用返回stl映射的正确方法/此行为是否定义良好?

C++ 通过引用返回stl映射的正确方法/此行为是否定义良好?,c++,stl,reference,C++,Stl,Reference,我有两个类,其中一个类有map我希望能够在其他类中使用它。这是我的密码: class a { map<string, vector<string> > m; public: const map<string, vector<string> > & get() { return m; } }; class b { a obj; public: void test

我有两个类,其中一个类有
map
我希望能够在其他类中使用它。这是我的密码:

class a
{
    map<string, vector<string> > m;
    public:
    const map<string, vector<string> > & get()
    {
        return m;
    }
};

class b
{
    a obj;
    public:
    void test()
    {
        map<string, vector<string> > m= obj.get();
        // and then print
    }
};

int main(int argc, char ** argv)
{
    b bobj;
    bobj.test();
    return 0;
}
a类
{
地图m;
公众:
const-map&get()
{
返回m;
}
};
b类
{
obj;
公众:
无效测试()
{
map m=obj.get();
//然后打印
}
};
int main(int argc,字符**argv)
{
b bobj;
bobj.test();
返回0;
}
我在
class a
中返回对map的引用的方式正确吗?它起作用了,但我只想确认它是否正确完成/我很幸运/对代码有任何其他评论


谢谢您的帮助。

如果您不想在
b::test()
中更改映射,则不应复制它:

const map<string, vector<string> >& m = obj.get(); // note the const &
  • 次要:我将使用
    typedef
    为地图类型创建别名

    typedef map<string, vector<string> > my_map_t;
    

    是的,这是返回对常量对象的引用的正确方法

    但是,在接收返回引用的
    测试
    函数中,左侧不是引用。这意味着您实际上要在这个函数中创建整个地图的副本。一个简单的更改将解决该问题,然后
    test
    函数将变为零拷贝:

    void test()
    {
        map< string, vector<string> > const & m = obj.get();
        // and then print
    }
    
    void测试()
    {
    mapconst&m=obj.get();
    //然后打印
    }
    
    这是正确的,但是您应该使用一个引用来存储前面提到的
    obj.get()
    的结果。如果你做了这样的事情,那就不正确了:

    const map< string, vector<string> >& foo()
    {
        map< string, vector<string> > m;
        // ...
        return m;
    }
    
    const-map&foo()
    {
    mapm;
    // ...
    返回m;
    }
    

    因为,在这种情况下,
    m
    foo()
    完成执行后将不再存在。

    让我给你看一个非常简单的例子,而不是直接回答你的问题。考虑一个简单的类,它有一个双份作为它的私有成员。< /P>
    class A{
    
    private:
    
      double a;
    
    public:
    
      A(double _a);
    
      double
      get_a();
    
      double&
      get_aref();
    
      const double&
      get_aconstref();
    
    };
    
    该类的实现。很简单

    A::A(double _a): a(_a){
    
    };
    
    // Getters
    double
    A::get_a() {
      return a;
    }
    
    double&
    A::get_aref(){
      return a;
    }
    
    const double&
    A::get_aconstref(){
      return a;
    }
    
    现在让我们进入使用类A的主程序

    int main(){
    
      A ainst(6.0);
    
      double a = ainst.get_a();
      std::cout << "a = " << a << std::endl;
      a++;
      std::cout << "a+1 = " << a << std::endl;
      std::cout << "Meanwhile, in class A, value of a = " << ainst.get_a() << std::endl;
    
      double& aref = ainst.get_aref();
      std::cout << "aref = " << aref << std::endl;
      aref++;
      std::cout << "aref+1 = " << aref << std::endl;
    
      std::cout << "Meanwhile, in class A, value of a = " << ainst.get_a() << std::endl;
    
      const double& aconstref = ainst.get_aconstref();
      std::cout << "aconstref = " << aconstref << std::endl;
    
    //  aconstref++;
    //  std::cout << "aconstref+1 = " << aconstref << std::endl;
    
      std::cout << "Meanwhile, in class A, value of a = " << ainst.get_a() << std::endl;
    
      return 0;
    }
    
    intmain(){
    aIST(6.0);
    double a=ainst.get_a();
    
    STD::我会在地图上重复重复你有什么代码< B/<代码>?这是C++,你可以有免费的功能!@新手,谢谢你!休息一切都好了。SBI,你说得对,但是它只是我正在工作的程序的一部分。休息好了吗?细节:你应该把函数签名改为<代码> const map和GET()const
    使其成为const-friendly.Correct,如果我可以补充一句:如果你真的想复制,按值返回可能会更快。@daramarak:以什么方式返回会更快?所有这一切只会迫使调用方复制。如果他想要复制,按const引用返回仍然是最好的。但正如大家所暗示的,我怀疑他是否真的需要ants想复制一份。@tenour:daramarak可能是对的:@sbi:在最好的情况下,它的速度是相同的,而不是更快。谢谢。我现在就得到了。它不应该是-const my_map\u t&m=obj.get()(在测试函数中?@iammilind:谢谢你的修复。我真的应该试着编译它。
    :(
    对不起,@Ian。
    A::A(double _a): a(_a){
    
    };
    
    // Getters
    double
    A::get_a() {
      return a;
    }
    
    double&
    A::get_aref(){
      return a;
    }
    
    const double&
    A::get_aconstref(){
      return a;
    }
    
    int main(){
    
      A ainst(6.0);
    
      double a = ainst.get_a();
      std::cout << "a = " << a << std::endl;
      a++;
      std::cout << "a+1 = " << a << std::endl;
      std::cout << "Meanwhile, in class A, value of a = " << ainst.get_a() << std::endl;
    
      double& aref = ainst.get_aref();
      std::cout << "aref = " << aref << std::endl;
      aref++;
      std::cout << "aref+1 = " << aref << std::endl;
    
      std::cout << "Meanwhile, in class A, value of a = " << ainst.get_a() << std::endl;
    
      const double& aconstref = ainst.get_aconstref();
      std::cout << "aconstref = " << aconstref << std::endl;
    
    //  aconstref++;
    //  std::cout << "aconstref+1 = " << aconstref << std::endl;
    
      std::cout << "Meanwhile, in class A, value of a = " << ainst.get_a() << std::endl;
    
      return 0;
    }