c++11 使用 std::map 作为返回值

     2023-03-25     23

关键词:

【中文标题】c++11 使用 std::map 作为返回值【英文标题】:c++11 use std::map as return value 【发布时间】:2021-11-15 13:34:34 【问题描述】:

我通常返回 std::vector 或 std::map 的对象作为传入的参考参数(如下面的 funcVec2 和 funcMap2)。但是写代码的时候有点不方便。所以我想如果我可以在c ++ 11下使用返回值(如下面的funcVec1和funcMap1),因为它会调用移动构造函数而不是复制构造函数,所以它可能仍然只花费一个构造时间并且没有解构作为传入引用的形式参数。 但是我编写了下面的代码来验证它,结果发现 funcVec1 和 funcMap1 比 funcVec2 和 funcMap2 需要更多的时间。所以我现在很困惑,为什么 funcVec1 和 funcMap1 需要这么长时间?

#include <iostream>
#include <vector>
#include <map>
#include <chrono>
using namespace std;
vector<int> funcVec1() 
    vector<int >vec;
    for (int i = 0; i < 10; ++i) 
        vec.push_back(i);
    
    return vec;


void funcVec2(vector<int>&vec) 
    for (int i = 0; i < 10; ++i) 
        vec.push_back(i);
    
    return;


map<int, int> funcMap1() 
    map<int, int>tmpMap;
    for (int i = 0; i < 10; ++i) 
        tmpMap[i] = i;
    
    return tmpMap;


void funcMap2(map<int, int>&tmpMap) 
    for (int i = 0; i < 10; ++i) 
        tmpMap[i] = i;
    


int main()

    using namespace std::chrono;
    system_clock::time_point t1 = system_clock::now();
    for (int i = 0; i < 100000; ++i) 
        vector<int> vec1 = funcVec1();
    
    auto t2 = std::chrono::system_clock::now();
    cout << "return vec takes " << (t2 - t1).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t2 - t1).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    vector<int> vec2;
    for (int i = 0; i < 100000; ++i) 
        funcVec2(vec2);
    
    auto t3 = system_clock::now();
    cout << "reference vec takes " << (t3 - t2).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t3 - t2).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    for (int i = 0; i < 100000; ++i) 
        map<int, int> tmpMap1 = funcMap1();
    
    auto t4 = system_clock::now();
    cout << "return map takes " << (t4 - t3).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t4 - t3).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    map<int, int>tmpMap2;
    for (int i = 0; i < 100000; ++i) 
        funcMap2(tmpMap2);
    
    auto t5 = system_clock::now();
    cout << "reference map takes " << (t5 - t4).count() << " tick count" << endl;
    cout << duration_cast<milliseconds>(t5 - t4).count() << " milliseconds" << endl;
    cout << " --------------------------------" << endl;
    return 0;

【问题讨论】:

我没有玩过代码,但我猜这是因为快速示例只是将数据添加到单个向量/映射,而慢速示例为每个调用分配新的向量/映射。每次分配新容器时,内存分配的次数应该多得多。 通常的问题需要问。有没有开启优化?您使用的是什么编译器和标志? 只是一个带有 vs2017 的新控制台项目,没有任何其他额外的优化@Retired Ninja 【参考方案1】:

    您不仅要衡量您的操作时间,还包括打印输出。这是次优的。

    您应该在发布模式下衡量性能。请注意,您没有对对象做任何有用的事情,优化器可能会丢弃您想要测量的大部分代码。

    比较不“公平”。例如,在您的 map1 案例中,您正在构建一个空地图,填充它(内存分配发生在这里),然后你把它扔掉。在 map2 的情况下,您一遍又一遍地重用相同的地图对象。您不会一遍又一遍地分配内存。

【讨论】:

使用 SWIG 和 Python/C API 包装返回 std::map 的函数

】使用SWIG和Python/CAPI包装返回std::map的函数【英文标题】:UsingSWIGandthePython/CAPItowrapafunctionwhichreturnsastd::map【发布时间】:2014-10-1021:05:11【问题描述】:我想包装一个C++例程,它返回一个std::map整数和指向C++类实例的指针。我无法... 查看详情

计算向量的 std::map 的值作为键并作为值的两倍?

】计算向量的std::map的值作为键并作为值的两倍?【英文标题】:Computingthevaluesofastd::mapofvectorasthekeyanddoubleasthevalue?【发布时间】:2018-03-2905:58:46【问题描述】:std::map<std::vector<double>,double>MyMethod(std::map<std::vector<doubl 查看详情

使用 c++ 11 constexpr 进行 std::map 初始化

】使用c++11constexpr进行std::map初始化【英文标题】:usec++11constexprforstd::mapinitialization【发布时间】:2017-11-1211:12:31【问题描述】:我想用constexpr的键初始化一个std::map。考虑以下C++11MWE:#include<map>usingstd::map;constexprunsignedintstr2i... 查看详情

使用值对std::map进行排序(代码片段)

...reanyfunctionlikethistosortthemap?通用关联源(需要C++11)如果你使用template<typenameA,typenameB>std::pair<B,A>flip_pair(conststd::pair<A,B>&p)returnstd::pair<B,A>(p.second,p.first);template<typenameA,typenameB>std::multimap<B,A>flip_map(conststd:... 查看详情

使用类方法插入 std::map

】使用类方法插入std::map【英文标题】:insertintostd::mapusingclassmethod【发布时间】:2017-04-2716:39:07【问题描述】:我搜索了很多关于这个问题,但没有找到任何东西,如果重复,请见谅。我在使用返回*this的类方法插入std::map时遇... 查看详情

使用 std::string 作为 std::map 的键

】使用std::string作为std::map的键【英文标题】:Usinganstd::stringasakeyforanstd::map【发布时间】:2011-06-2318:39:08【问题描述】:我想要一个std::map(int.NET4.0)。我们当然知道地图是一棵树,并且需要一个operator错误24错误C2676:二进制\'所以... 查看详情

使用 protobuf 对象作为 std::map 中的键

】使用protobuf对象作为std::map中的键【英文标题】:Usingprotobufobjectsaskeyinthestd::map【发布时间】:2019-04-0619:34:01【问题描述】:我是协议缓冲区概念的新手,如果我使用protobuf对象作为std::map中的键,我手头有一个任务可以解决。... 查看详情

如何使用 arma::vec 作为 std::map 中的键

】如何使用arma::vec作为std::map中的键【英文标题】:Howtousearma::vecaskeyinstd::map【发布时间】:2020-02-2918:21:00【问题描述】:我已经实现了以下代码来更新马尔可夫模型,但编译器不允许我使用犰狳向量作为std::map中的键。有什么想... 查看详情

std::map 作为类成员

...述】:我有一个带有成员std::mapm_lookupTable的X类。我应该使用以下哪一项:ClassX...private:std::map<int,int>m_lookupTable;...或在类的析构函数中使用new和delete进行分配classXprivate:std::m 查看详情

使用带有结构的地图作为键 - 值不会保存[重复]

】使用带有结构的地图作为键-值不会保存[重复]【英文标题】:Usingmapwithstructureaskey-valuedoesn\'tsave[duplicate]【发布时间】:2014-04-0608:33:16【问题描述】:我对c++std::map容器和结构作为键没有什么问题。我想使用map作为ipv6查找表的... 查看详情

如何使用数组作为地图值?

】如何使用数组作为地图值?【英文标题】:HowcanIuseanarrayasmapvalue?【发布时间】:2010-04-0603:44:48【问题描述】:我正在尝试创建一个映射,其中键是int,值是一个数组,如下所示:intred[3]=1,0,0;intgreen[3]=0,1,0;intblue[3]=0,0,1;std::map<... 查看详情

为啥我可以在 std::map<std::string, int> 中使用 const char* 作为键

】为啥我可以在std::map<std::string,int>中使用constchar*作为键【英文标题】:WhyIcanuseconstchar*askeyinstd::map<std::string,int>为什么我可以在std::map<std::string,int>中使用constchar*作为键【发布时间】:2012-12-0514:52:25【问题描述】:... 查看详情

Pybind11:外部类型作为返回值

...peasreturnvalue【发布时间】:2019-11-0611:53:01【问题描述】:使用pybind11,我需要绑定一个函数,该函数返回一个对象类型,该对象类型的绑定已经可以从另一个扩展模块(也使用pybind11创建)获得。我知道这通常是可能的,因为thedo... 查看详情

使用 std::string 作为字典顺序的键对 unordered_map 进行排序

】使用std::string作为字典顺序的键对unordered_map进行排序【英文标题】:Sortingaunordered_mapwithastd::stringasakeyinlexicographicalorder【发布时间】:2017-10-2420:41:46【问题描述】:上周我参加了Catalysts编码竞赛,现在我正在尝试使用更多高级C... 查看详情

在编译时查找 std::map 的重复值

...是唯一的,以防止重复的std::string值在按值搜索查找期间返回 查看详情

c++11标准模板(stl)(std::unordered_map)(代码片段)

...<unordered_map>template<  classKey,  classT,  classHash=std::hash<Key>,  classKeyEqual=std::equal_to<Key>,  classAllocator=std::allocator<std::pair<constKey,T>>>classunordered_map;(1)(C++11起)namespacepmr  template<classKey,  ... 查看详情

是否可以使用 boost.any 作为 std::map 中的键(或类似的东西)?

】是否可以使用boost.any作为std::map中的键(或类似的东西)?【英文标题】:Isitpossibletouseboost.anyasakeyinastd::map(orsomethingsimiliar)?【发布时间】:2009-05-0509:01:20【问题描述】:std::map&lt;any,string&gt;不工作所以我想知道是否有另... 查看详情

qmap和map哪个效率高?

...map方案以替代std::map,但是由于开发环境不是Qt所以不想使用QMap,搜索时看到这个问题。。。。如果大家有什么好的解决方案,请麻烦告诉一下参考技术AQmap是目前支持矢量地图在互联网环境调用和显示速度最快的GIS引擎系统,... 查看详情