template-c++ 编写类似于标准库中find算法的模板,非引用形参和引用形参的区别是什么

问题描述

c++ 编写类似于标准库中find算法的模板,非引用形参和引用形参的区别是什么
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<typename Init,typename T> Init find(Init begin,Init end,const T& val){
    while(begin!=end){
        if(val==(*begin)){
            return begin;
        }
        ++begin;
    }
    return end;
}
//为什么前2个参数改成引用就不行了
template<typename Init,typename T> Init find_(Init &begin,Init &end,const T& val){
    while(begin!=end){
        if(val==(*begin)){
            return begin;
        }
        ++begin;
    }
    return end;
}

int main(){
    int arr[5] = {1,2,3,45,5};
    string sarr[5] = {"b","c ","dd","ee","ff"};
    vector<int> ivec(arr,arr+5);
    vector<string> svec(sarr,sarr+5);
    vector<int>::iterator it;
    vector<string>::iterator sit;
    if((it=find(ivec.begin(),ivec.end(),3))!=ivec.end()){
        cout << *it << "is in the vector" << endl;
    }else{
        cout << "not found!" << endl;
    }
    if((sit = fd_(svec.begin(),svec.end(),string("b")))!=svec.end()){
        cout << "find" << endl;
    }
    else{
        cout << "not find";
    }
}

#把find函数的前两个形参改成引用,会产生编译错误,为什么

error: invalid initialization of non-const reference of type ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >&’ from an rvalue of type ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
     if((it=find_(ivec.begin(),ivec.end(),3))!=ivec.end()){
                                           ^
 error: in passing argument 1 of ‘Init find_(Init&, Init&, const T&) [with Init = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; T = int]’
 template<typename Init,typename T> Init find_(Init &begin,Init &end,const T& val){

解决方案

if((it=find(ivec.begin(),ivec.end(),3))!=ivec.end()){
cout << it << "is in the vector" << endl;
}else{
cout << "not found!" << endl;
}
**if((sit = fd_(svec.begin(),svec.end(),string("b")))!=svec.end()){
cout << "find" << endl;
}
*
else{
cout << "not find";
}
fd_是不是写错了,应该是find_,若改为find_,我试了一下,自己这可以编译过,没有问题的

解决方案二:

如果加了引用,就相当于要用int来引用iterator,int不是iterator的基类,不能这么做。。而如果是没加引用,这发生了隐式转换,从iterator转换为int型。所以在find那里应该改成iterator的模板转换,这样才能引用。

时间: 2024-08-03 10:04:02

template-c++ 编写类似于标准库中find算法的模板,非引用形参和引用形参的区别是什么的相关文章

PHP SPL标准库中的常用函数介绍

  这篇文章主要介绍了PHP SPL标准库中的常用函数介绍,本文着重讲解了spl_autoload_extensions().spl_autoload_register().spl_autoload()三个函数,需要的朋友可以参考下 PHP SPL标准库中提供了一些函数用来处理如自动加载.迭代器处理等. spl_autoload_extensions()添加spl_autoload()可加载的文件扩展名 spl_autoload_register()注册函数到SPL __autoload函数栈中

c++标准库-C++标准库中,set容器的insert函数中的比较函数重写问题

问题描述 C++标准库中,set容器的insert函数中的比较函数重写问题 在**set **容器里我把它的其中的元素定义为map,然后我就不会写compare函数了.因此他的insert函数就跪了--求大神助--哭-- 解决方案 #include #include using std::string; #include using std::map; #include using std::set; #include using std::make_pair; #include using s

关于c++标准库中regex的疑问。

问题描述 关于c++标准库中regex的疑问. 如图为什么输出结果不是 ABC 12 4 我正则表达式没有写错,已测试过了. code int splitkey() { list cols; string weekend = "ABC(012,04)"; regex pattern("(w+)|([1-9]d*)"); const std::sregex_token_iterator end; //需要注意一下这里 int no1=1; for (std::sreg

使用Python标准库中的wave模块绘制乐谱的简单教程_python

在本文中,我们将探讨一种简洁的方式,以此来可视化你的MP3音乐收藏.此方法最终的结果将是一个映射你所有歌曲的正六边形网格地图,其中相似的音轨将处于相邻的位置.不同区域的颜色对应不同的音乐流派(例如:古典.嘻哈.重摇滚).举个例子来说,下面是我所收藏音乐中三张专辑的映射图:Paganini的<Violin Caprices>.Eminem的<The Eminem Show>和Coldplay的<X&Y>. 为了让它更加有趣(在某些情况下更简单),我强加了一些限制.

PHP SPL标准库中的常用函数介绍_php技巧

PHP SPL标准库中提供了一些函数用来处理如自动加载.迭代器处理等. spl_autoload_extensions()添加spl_autoload()可加载的文件扩展名 spl_autoload_register()注册函数到SPL __autoload函数栈中. 复制代码 代码如下: /*test1.php*/ <?php class Test1 { }   /*test2.lib.php*/ <?php class Test2 { }   /*test.php*/ <?php /

C++标准库中sstream与strstream的区别详细解析_C 语言

在C++有两种字符串流,一种在sstream中定义,另一种在strstream中定义.它们实现的东西基本一样. strstream里包含class strstreambuf;class istrstream;class ostrstream;class strstream;它们是基于C类型字符串char*编写的 sstream中包含class istringstream;class ostringstream;class stringbuf;class stringstream;class --

Golang的os标准库中常用函数的整理介绍_Golang

os.Rename()这个函数的原型是func Rename(oldname, newname string) error,输入的是旧文件名,新文件名,然后返回一个error其实这个函数的真正实现用的syscall.Rename()然后通过MoveFile(from *uint16, to *uint16) (err error) = MoveFileW来重新命名 复制代码 代码如下:  import (  "fmt"  "os" ) func main() {  

Go 语言的 HTTP 标准库中的内存泄漏问题?

它将使用大约850kb的内存启动.通过你的浏览器向它发送一些请求. 你会观察到它(内存的使用)迅速上升到1mb. 如果你等着,你会发现它从来不会降下来. 现在(使用下面的脚本)用 Apache Bench 动一下它,你会发现内存使用仍然在增长. 一段时间过后它最终会维持在8.2mb左右. 文章转载自 开源中国社区 [http://www.oschina.net]

C++著名类库和C++标准库介绍

C++著名类库 1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库Boost 3.C++各大有名库的介绍--GUI 4.C++各大有名库的介绍--网络通信 5.C++各大有名库的介绍--XML 6.C++各大有名库的介绍--科学计算 7.C++各大有名库的介绍--游戏开发 8.C++各大有名库的介绍--线程 9.C++各大有名库的介绍--序列化 10.C++各大有名库的介绍--字符串 11.C++各大有名库的介绍--综合 12.C++各大有名库的介绍--其他库 1