从模板函数将迭代器返回到STL容器

时间:2014-10-17 21:20:25

标签: c++ stl iterator return-type template-function

我试图将迭代器返回到模板函数的向量(还不是模板类成员 - 我还在编写它)。编译器一直给我错误(下面复制以便于谷歌搜索)。我基本上知道问题是什么,但确切的语法是难以捉摸的。

我在网上搜索了搜索结果,包括Where and why do I have to put the "template" and "typename" keywords?,但没有找到有效的答案。我想我应该问这个问题并在这里自己回答。

(简称)原始代码如下:

#include <vector>
#include <algorithm>  // std::lower_bound

template<typename T> std::vector<T>::iterator
              insertIntoVector(std::vector<T>& vec, const T& val)
{  itr = [some std::vector<T> iterator];
   return itr;  // return iterator to val in vec.
} // End of insertIntoVector();

编译器错误:

error C2145: syntax error: missing ';' before identifier 'insertIntoVector'
error C2065: 'T' : undeclared identifier
error C2923: 'std::vector' : 'T' is not a valid template type argument for parameter '_Ty'

明智了,我试过了:

template<typename T> typename std::vector<T>::iterator
              insertIntoVector(std::vector<T>& vec, const T& val)

更多编译器错误:

error C1075: end of file found before the left brace '{'

如果此问题解锁,我会在下面发布我的回答。否则,请参阅Where and why do I have to put the "template" and "typename" keywords?上的答案。

1 个答案:

答案 0 :(得分:1)

奇怪的是,我必须在返回类型周围添加括号以使其编译。也就是说,

 template<typename T> (typename std::vector<T>::iterator)    // Tested--works as expected.
             insertIntoVector(std::vector<T>& vec, const T& val)

持续工作,但

 template<typename T>  typename std::vector<T>::iterator     // Compile errors
             insertIntoVector(std::vector<T>& vec, const T& val)

在MSVC 2013上给我带来了问题,但在MSVC 2012上编译。我不知道为什么。任何人吗?

以下代码在MSVC 2013上正确编译并运行。

// The following is based on Matt Austern's article,
// "Why you shouldn't use set (and what you should use instead)"
// (http://lafstern.org/matt/col1.pdf).
// I modified it slightly, mostly just reformatting and adding comments,
// but I also changed it to return an iterator to the inserted element.
// Also changed sorted_vector to sortedVector (cammelCase)

#include <vector>
#include <algorithm>  // std::lower_bound


 template<typename T> (typename std::vector<T>::iterator)  // Tested--works as expected.
             insertIntoVector(std::vector<T>& vec, const T& val)
{  // Insert t into vector vec such that v remains in sorted order
   // and all elements of vec are unique (like a set).
   // This only makes sense if the vector elements are
   // maintained in sorted order.
   // A sorted vector might perform better than a set if
   // there are many more access operations than insertions
   // (smaller storage, fast access via [], ... at the cost
   // of much slower insertions).
   // Note: Type T must have a defined < operator.
   /// Todo: overload this function to allow passing a Compare()
   /// function pointer.

   // std::lower_bound() gives log2(N) + O(1) performance.
   typename std::vector<T>::iterator itr
             = lower_bound(vec.begin(), vec.end(), val);
   if ( (vec.end() == itr) || (val < *itr) )
   {  itr = vec.insert(itr, val);
   }
   return itr;  // return iterator to val in vec.
} // End of insertIntoVector();