push_back和emplace_back哪个效率更高?

C++ linux :错误:‘move’不是‘std’的成员该怎么解决呢?

使用高版本的gcc就可以了。

source /opt/rh/devtoolset-8/enable

gcc -lstdc++ -o test test.cpp   //如果没有写makefile,一定要连接stdc++,不然还是编译不过的。

push_back和emplace_back哪个效率更高?

最近学习一下C++新特性,std::move 一直不理解,所以写了一个函数来测试。

#pragma pack(4)

#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <iostream>
#include <utility>
#include <memory>
#include <cstring>

using namespace std;

class Person
{
    public:
    Person(int _age,const char* _name):age(_age),name(NULL)
    {
        std::cout<<"Person() construct" << std::endl;
        name = new char( sizeof(_name));
        if(name != NULL)
        {
            memcpy(name,_name,sizeof(_name));
        }

    }

    ~Person()
    {
        if(name != NULL)
        {
            delete[] name;
            name = NULL;
        }
    }
    Person(const Person& rhs)
    {
        std::cout<<"Person() copy construct" << std::endl;
        age = rhs.age;
        name = new char( sizeof(rhs.name));
        if(name != NULL)
        {
            memcpy(name,rhs.name,sizeof(rhs.name));
        }
    }
     Person( Person&& rhs)
    {
        std::cout<<"Person() move construct" << std::endl;
        age = rhs.age;
        name = rhs.name;
    }

    void printMe()
    {
        if(name == NULL)
        {
            cout <<" this name is null." << endl;
        }
        else
        {
            cout <<" age = " << age << "   name = " << name << endl;
        }
       
    }

    private:
    int age;
    char *name;
};

void  test02()
{
 cout << "__cpplus = " << __cplusplus << endl;    
Person p1(18, "123");
    Person p2(58, "890");
    Person p3(68, "1000");
    Person p4(78, "1001");

    std::vector<Person> vp;
    std::cout << "\n call push_back  move \n"; 
    vp.push_back(std::move(p1));
    p1.printMe();

    std::cout << "\n call emplace_back  move \n"; 
    vp.emplace_back(std::move(p2));
    p2.printMe();

    std::cout << "\n  call emplace_back  \n"; 
    vp.emplace_back(p3);
    p3.printMe();

    std::cout << "\n call push_back :\n "; 
    vp.push_back(p4);
    p4.printMe() ; 

    std::cout << "\n call emplace_back :\n "; 
    vp.emplace_back(99,"testemplace");

    std::cout << "\n just test move :\n "; 
    Person p5(std::move(p4)); //p3不能用了
    p4.printMe();
    p5.printMe();
}

int main()
{
    test02();
    return 0;
}

运行结果:

__cpplus = 201402
Person() construct
Person() construct
Person() construct
Person() construct

 call push_back  move
Person() move construct  
 age = 18   name = 123

 call emplace_back  move
Person() move construct
Person() copy construct
 age = 58   name = 890

  call emplace_back
Person() copy construct
Person() copy construct
Person() copy construct
 age = 68   name = 1000

 call push_back :
 Person() copy construct
 age = 78   name = 1001

 call emplace_back :
 Person() construct
Person() copy construct
Person() copy construct
Person() copy construct
Person() copy construct

 just test move :
 Person() move construct
 age = 78   name = 1001
 age = 78   name = 1001

这个好迷惑,这么看push_back的效率更高一些呢。为什么?大侠们请赐教。

用的是最新版本的gcc,__cpplus = 201402 我的代码库里面的push_back和emplace_back的实现如下:按这个实现如果C++版本比较高的化,至少两个效率应该是一样的呢。

#if __cplusplus >= 201103L
      template<typename _Up = _Tp>
        typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
					void>::__type
        push_back(_Tp&& __x)
	{ emplace_back(std::move(__x)); } //像是全部调用的这个。

      template<typename... _Args>
        void
        emplace_back(_Args&&... __args)
	{
	  bool __realloc = _M_requires_reallocation(this->size() + 1);
	  _Base::emplace_back(std::forward<_Args>(__args)...);
	  if (__realloc)
	    this->_M_invalidate_all();
	  _M_update_guaranteed_capacity();
	}
#endif

终于找到原因了。原因是vector是变长的,如果只定义不给长度,那么可能他的长度可能是最小值,每插入一个,长度就增加一倍,这样每次就看到调用了好几次拷贝,然后开始怀疑自己。所以如果时C++14 以后的版本,一定要在初始化的时候给个初始长度。或者用 reserve(10);设置一下长度。用空间换时间。然后我增加了一行,

然后输出就变了。这么看push_back和emplace_back效率就一样了。

__cpplus = 201402
Person() construct
Person() construct
Person() construct
Person() construct

 call push_back  move
Person() move construct
 age = 18   name = 123

 call emplace_back  move
Person() move construct
 age = 58   name = 890

  call emplace_back
Person() copy construct
 age = 68   name = 1000

 call push_back :
 Person() copy construct
 age = 78   name = 1001

 call emplace_back :
 Person() construct

 just test move :
 Person() move construct
 age = 78   name = 1001
 age = 78   name = 1001

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值