C ++ 14在方法的定义中使用自动关键字

C ++ 14在方法的定义中使用自动关键字

问题描述:

我有几个std::unordered_maps.它们的键名和数据均不同,均带有std::string.我想从给定地图的键中创建一个csv字符串,因为该数据需要通过电线发送到连接的客户端.目前,我对每个单独的地图都有一个方法.我想使它通用,并提出了以下建议:

I have several std::unordered_maps. They all have an std::string as their key and their data differs. I want to make a csv string from a given map's keys because that data needs to be sent over the wire to a connected client. At the moment I have a method for each individual map. I wanted to make this generic and I came up with the following :

std::string myClass::getCollection(auto& myMap) {
    std::vector <std::string> tmpVec;
    for ( auto& elem : myMap) {
        tmpVec.push_back(elem.first);
    }
    std::stringstream ss;
    for ( auto& elem : tmpVec ) {
        ss << elem <<',';
    }
    std::string result=ss.str();
    result.pop_back(); //remove the last ','
    return result;
}

我使用eclipse使用gcc 6.1.0和-std = c ++ 14进行编译,并且可以编译但不链接. 链接器抱怨未定义引用std::__cxx11::getCollection(someMap);

I compile with gcc 6.1.0 and -std=c++14 using eclipse and it compiles but it doesn't link. The linker complains about undefined reference to std::__cxx11::getCollection(someMap);

不管地图数据和我如何称呼它,它总是告诉我:

Regardless of the map data and the way I call it, it always tells me :

Invalid arguments ' Candidates are: std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>> getCollection() '

我该如何解决?

正如在C ++ 14中,auto参数仅在lambda中允许使用(根据@ildjarn的评论),您可以开发一个函数模板,以地图类型为模板,例如:

As in C++14 auto parameters are only allowed in lambdas (as per @ildjarn's comment), you can just develop a function template, templateized on the map type, e.g.:

#include <sstream>
#include <string>
#include <vector>

class myClass {
...

template <typename MapType>
std::string getCollection(const MapType& myMap) {
    std::vector <std::string> tmpVec;
    for ( const auto& elem : myMap) {
        tmpVec.push_back(elem.first);
    }
    std::stringstream ss;
    for ( const auto& elem : tmpVec ) {
        ss << elem <<',';
    }
    std::string result=ss.str();
    result.pop_back(); //remove the last ','
    return result;
}

还请注意,出于某些 const -正确性,添加了const.

Note also the addition of const for some const-correctness.

此外,为什么不直接使用字符串流对象直接构建输出字符串,而不填充 intermediate vector<string>(这是更多的代码,更多的潜在错误,更多的开销,更低的效率) ?

Moreover, why not just building the output string directly using the string stream object, without populating an intermediate vector<string> (which is more code, more potential for bugs, more overhead, less efficiency)?

而且,由于您只是想将字符串流用作 output 流,因此使用ostringstream而不是stringstream会更好,因为它更有效并且可以更好地传达您的意图.

And, since you are just interested in using the string stream as an output stream, using ostringstream instead of stringstream is better as it's more efficient and communicates your intent better.

#include <sstream>  // for std::ostringstream
#include <string>   // for std::string
...

template <typename MapType>
std::string getCollection(const MapType& myMap) {
    std::ostringstream ss;
    for (const auto& elem : myMap) {
        ss << elem.first << ',';
    }
    std::string result = ss.str();
    result.pop_back(); // remove the last ','
    return result;
}