繁体   English   中英

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

[英]Return an iterator to an STL container from a template function

我试图将迭代器从模板函数(还不是模板类成员,我仍在编写)中返回向量。 编译器一直在给我错误(为了方便Google搜索,已在下面复制)。 我基本上知道问题出在哪里,但是确切的语法却难以捉摸。

我阅读了互联网,进行了搜索,包括为什么以及为什么必须在何处放置“模板”和“类型名”关键字? ,但找不到有效的答案。 我想我应该问这个问题,然后自己在这里回答。

(缩写)原始代码如下:

#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 '{'

如果此问题解锁,我将在下面发布我的答案。 否则,请参阅我在何处以及为何必须放置“模板”和“类型名”关键字的答案

奇怪的是,我必须在返回类型周围加上括号才能进行编译。 那是,

 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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM