C++ 从模板函数向STL容器返回迭代器

C++ 从模板函数向STL容器返回迭代器,c++,stl,iterator,return-type,template-function,C++,Stl,Iterator,Return Type,Template Function,我试图从模板函数(还不是模板类成员——我还在编写)返回一个迭代器到向量。编译器不断地给我错误(复制到下面以便于谷歌搜索)。我基本上知道问题出在哪里,但确切的语法难以捉摸 我在网上读过,搜索过,包括,但没有找到一个有效的答案。我想我应该问这个问题,然后自己回答 (缩写)原始代码如下: #include <vector> #include <algorithm> // std::lower_bound template<typename T> std::vec

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

我在网上读过,搜索过,包括,但没有找到一个有效的答案。我想我应该问这个问题,然后自己回答

(缩写)原始代码如下:

#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();
变得聪明了,我尝试了这个:

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

如果这个问题被解锁,我会在下面发布我的答案。否则,请参见上的答案。

奇怪的是,我不得不在返回类型周围添加括号以使其进行编译。就是

 template<typename T> (typename std::vector<T>::iterator)    // Tested--works as expected.
             insertIntoVector(std::vector<T>& vec, const T& val)
template(typename std::vector::iterator)//已测试--按预期工作。
插入向量(std::vector&vec,const T&val)
工作始终如一,但

 template<typename T>  typename std::vector<T>::iterator     // Compile errors
             insertIntoVector(std::vector<T>& vec, const T& val)
模板typename std::vector::iterator//编译错误
插入向量(std::vector&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();
//以下内容基于Matt Ostern的文章,
//“为什么不应该使用set(以及应该使用什么替代)”
// (http://lafstern.org/matt/col1.pdf).
//我稍微修改了一下,主要是重新格式化和添加注释,
//但我也修改了它,将迭代器返回到插入的元素。
//还将sorted_vector更改为sortedVector(cammelCase)
#包括
#include//std::下限
模板(typename std::vector::iterator)//已测试--按预期工作。
插入向量(std::vector&vec,const T&val)
{//将t插入向量vec,使v保持排序顺序
//vec的所有元素都是唯一的(就像一个集合)。
//这只有在向量元素为
//保持有序的。
//排序后的向量可能比集合的性能更好,如果
//访问操作比插入操作多得多
//(更小的存储空间,通过[]快速访问,…成本更低
//插入速度要慢得多)。
//注:类型T必须有一个定义的<运算符。
///Todo:重载此函数以允许传递比较()
///函数指针。
//lower_bound()提供log2(N)+O(1)性能。
typename std::vector::迭代器itr
=下限(向量开始(),向量结束(),val);
如果((向量结束()==itr)| |(val<*itr))
{itr=vec.insert(itr,val);
}
return itr;//将迭代器返回到vec中的val。
}//insertIntoVector()的结尾;

使用
模板typename std::vector::iterator
,因为您有一个依赖类型,我不同意这个问题是重复的。首先,我搜索了一下我的*ss,寻找答案。我怎么知道要寻找的神奇问题?问题是关于将迭代器返回到stl容器的模板函数的返回类型。这些关键词并没有把我引向引用的问题。@vsoftco。typename std::vector::iterator也不起作用,尽管依赖类型的问题已经得到解决。当我被一个重复的问题拒之门外时,我正在发布我的答案。阅读引用的问题,看看您的建议是否有问题,或者它是否涉及返回类型。@T.C.我再次查看了您引用的问题,使用浏览器搜索功能查找“返回类型”。返回一词甚至不在其中。vsoftco上面的评论也不太正确。显然你的引用并没有解决我(以前)的问题。我本想尽我的本分,给社区一些回报,但在花了半个小时写了我的答案后,你把我踢开了。“我不确定我是否想再试一次。@riderBill依赖类型的问题出现得非常频繁,这就是为什么我们中的一些人把这些问题作为规范问题的重复来关闭的原因。”。那些搜索者仍然可以找到重复的问题和答案。如果你能解释你的答案会涉及哪些额外的理由,我很乐意投票重新开始,因为据我所知,你发布的编译器错误都是由缺少的
类型名引起的,唯一的问题是函数体中缺少
typename std::
之前的
vector::iterator
。@T.C.我在MSVC 2013上试过。它现在编译有没有paren。星期五我肯定把别的事情搞砸了——我不好意思承认,但我没有其他解释。当我删除返回类型上的参数并重新编译时,编译器说“正在跳过…(未检测到相关更改)”。我猜它认为()是多余的。我的编译器没有抱怨itr声明/定义行的正文中缺少typename std::的问题,但我根据您的建议添加了它,以提高健壮性/可移植性,因为您说您有问题。你是用GCC编译的吗?我将来可能需要在Solaris上运行它。我不知道我是怎么摆脱的,因为我没有使用性病;文件中的语句。
 template<typename T>  typename std::vector<T>::iterator     // Compile errors
             insertIntoVector(std::vector<T>& vec, const T& val)
// 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();