本文介绍了C ++ newbie问题:设计一个函数来返回一个字符串或双重重载或模板的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数,在文本文件中搜索名称。它返回向量,其中向量的每个元素是一个不同的名称。

I have written a function that searches a text file for names. It returns vector, where each element of the vector is a different name.

现在我想搜索相同的文本文件的数字,并返回向量中的数字。

Now I would like to search the same text file for numbers and return the numbers in a vector.

这可能是一个愚蠢的问题,但我想知道什么是最好的方法。通过写一个第二个函数来重载函数,该函数返回一个向量,或者通过用向量T代替类型来转换我已经写入模板的函数。

This is probably a dumb question, but I'm wondering what the best approach would be. Overload the function by writing a second function that returns a vector or turning the function I have already written into a template by replacing the type with T, as in vector.

我对模板选项感到困惑的原因是我不确定字符串和数字类型(如double和int)是否在模板中兼容。任何提示将不胜感激!感谢。

The reason I'm confused about the template option is that I'm not sure if strings and numerical types like double and int are compatible in a template. Any tips would be appreciated! Thanks.

推荐答案

由于函数签名不包括其返回类型,因此不能仅在其返回类型。

As a function signature does not include its return type, you can't overload a function only on its return type.

另外,由于两个函数返回不同类型的数据(一个可以返回人名,另一个可以返回人的年龄),具有相同的名称一个语义错误。

Also, as the two functions returns different kind of data (one could return person names, the other could return the persons ages), having the same name for both seems a semantic error.

但无论如何...

void retrieveData(std::vector<std::string> & data) ;
void retrieveData(std::vector<double>      & data) ;

void foo()
{
   std::vector<std::string> strings ;
   std::vector<double> doubles ;

   // retrieve the strings
   retrieveData(strings) ;
   // retrieve the numbers
   retrieveData(doubles) ;
}

这个解决方案最好是因为重载工作as is它避免了一个向量的副本(我使用C ++ 03,在这里...在C ++ 0x,将使用移动语义避免潜在的额外副本)。

This solution is best both because the overload work "as is", and because it avoids a copy of the vector (I am using C++03, here... In C++0x, one would use move semantics to avoid the potential extra copy).

std::vector<std::string> retrieveData(std::string * dummy) ;
std::vector<double>      retrieveData(double      * dummy) ;

void foo()
{
   // retrieve the strings
   std::vector<std::string> strings = retrieveData((std::string *) NULL) ;
   // retrieve the numbers
   std::vector<double> doubles = retrieveData((double *) NULL) ;
}

这里,虚拟参数的指针值不在函数体中使用。它是用来使编译器找到正确的重载。

Here, the dummy parameter's pointer value is not used in the function body. It's use is to enable the compiler to find the right overload.

这是丑陋的(没有提到通过复制一个向量在C ++ 03返回的部分,但这是

This is ugly (not mentioning the part on returning by copy a vector in C++03 but this is out of topic), but it does the work and is a viable answer to your question.

// declared, but not defined
template<typename T>
std::vector<T> retrieveData() ;
// defined
template<>
std::vector<std::string> retrieveData<std::string>() ;
// defined
template<>
std::vector<double>      retrieveData<double>() ;

void foo()
{
   // retrieve the strings
   std::vector<std::string> strings = retrieveData<std::string>() ;
   // retrieve the numbers
   std::vector<double> doubles = retrieveData<double>() ;
}

这里,模板参数由用户给出,信息来选择正确的重载

Here, the template parameter is given by the user, thus giving the compiler enough information to choose the right overload

PS:这个答案与我在这里给出的答案非常相似:

P.S.: This answer is quite similar to the one I gave here: Puzzle: Overload a C++ function according to the return value

这篇关于C ++ newbie问题:设计一个函数来返回一个字符串或双重重载或模板的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:15