小编use*_*888的帖子

在C++中,如何编写一个函数以便它可以处理任何数据类型?

我正在使用此资源学习C++ STL:http://community.topcoder.com/tc?module = static&d1 = tutorials&d2 = standardTemplateLibrary

下面给出了以下函数来反转数组的元素:

template<typename T> void reversearr(T *begin, T *end) { 
      // We should at first decrement 'end' 
      // But only for non-empty range 
      if(begin != end) 
      { 
           end--; 
           if(begin != end) { 
                while(true) { 
                     swap(*begin, *end); 
                     begin++; 
                     if(begin == end) { 
                          break; 
                     } 
                     end--; 
                     if(begin == end) { 
                          break; 
                     } 
                } 
           } 
      } 
 } 
Run Code Online (Sandbox Code Playgroud)

它适用于系统定义的类型数组,例如:

int arr[]={1,2,3,4,5}
reversearr(arr,arr+5);
Run Code Online (Sandbox Code Playgroud)

但它给出了以下编译器错误:

"Iterator02_ReverseIterators.cpp:39:32:错误:没有匹配函数来调用'reversearr(std :: vector :: iterator,std :: vector :: iterator)'"

如果我使用此代码:

vector<int> …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl

4
推荐指数
1
解决办法
195
查看次数

标签 统计

c++ ×1

iterator ×1

stl ×1