简体   繁体   English

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

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

I was trying to return an iterator to a vector from a template function (not yet a template class member--I'm still writing that). 我试图将迭代器从模板函数(还不是模板类成员,我仍在编写)中返回向量。 The compiler kept giving me errors (copied below to to facilitate Google searches). 编译器一直在给我错误(为了方便Google搜索,已在下面复制)。 I knew basically what the problem was, but the exact syntax was elusive. 我基本上知道问题出在哪里,但是确切的语法却难以捉摸。

I read the internet, searched SO, including Where and why do I have to put the "template" and "typename" keywords? 我阅读了互联网,进行了搜索,包括为什么以及为什么必须在何处放置“模板”和“类型名”关键字? , but didn't find an answer that worked. ,但找不到有效的答案。 I figured I should ask the question and answer it myself here. 我想我应该问这个问题,然后自己在这里回答。

(Abbreviated) original code follows: (缩写)原始代码如下:

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

Compiler errors: 编译器错误:

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'

Getting wise, I tried this: 明智地,我尝试了这个:

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

More Compiler errors: 更多编译器错误:

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

I'll post my answer below if this question gets unlocked. 如果此问题解锁,我将在下面发布我的答案。 Otherwise, see my answer on Where and why do I have to put the "template" and "typename" keywords? 否则,请参阅我在何处以及为何必须放置“模板”和“类型名”关键字的答案 .

Strangely, I had to add parentheses around the return type to get it to compile. 奇怪的是,我必须在返回类型周围加上括号才能进行编译。 That is, 那是,

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

Worked consistently, but 一直工作,但是

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

Gave me problems on MSVC 2013, but compiles on MSVC 2012. I don't know why. 在MSVC 2013上给我带来了问题,但在MSVC 2012上给了我。我不知道为什么。 Anyone? 任何人?

The code below compiles and runs correctly on MSVC 2013. 下面的代码可以编译并在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