c++ primer chapter 16.4

Posted pgh79

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++ primer chapter 16.4相关的知识,希望对你有一定的参考价值。

/*
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
template <typename I, typename T>
I find(I b, I e, const T &v)
while (b != e && *b != v)
b++;
return b;



int main()
vector<int> vi = 0, 2, 4, 6, 8, 10;
list<string> ls = "Hello", "World", "!" ;

auto iter = find(vi.begin(), vi.end(), 6);
//vector<int> iter = find(vi.begin(), vi.end(), 6);

if (iter == vi.end())
std::cout << "find 6 at position" << std::endl;
else
std::cout << "find 6 at position" << iter - vi.begin() << std::endl;

auto iter1 = find(ls.begin(), ls.end(), "mom");
//list<string> iter1 = find(ls.begin(), ls.end(), "mom");

if (iter == ls.end())
std::cout << "can\'t find mom" << std::endl;

else
std::cout << "found mom" << std::endl;

return 0;


*/
#include <iostream>
#include <vector>
#include <list>

using namespace std;

template <typename I, typename T>
I find(I beg, I end, T val)

auto iter = beg;
for (; iter != end; ++iter)
if (*iter == val)
break;
return iter;


int main()

vector<int> iv = 1, 2, 3, 4, 5;
list<string> slst = "dog", "cat", "rat";

auto p1 = find(iv.begin(), iv.end(), 4);
if (p1 != iv.end())
cout << *p1 << endl;

auto p2 = find(slst.begin(), slst.end(), "rat");
if (p2 != slst.end())
cout << *p2 << endl;

return 0;

C++ Primer笔记16---chapter13 代码实例

以上是关于c++ primer chapter 16.4的主要内容,如果未能解决你的问题,请参考以下文章

C++ Primer笔记16---chapter13 代码实例

C++ Primer笔记15---chapter13 拷贝控制2

C++ Primer笔记15---chapter13 拷贝控制2

C++ Primer笔记17---chapter14 重载运算与类型转换

C++ Primer笔记17---chapter14 重载运算与类型转换

《C++ Primer》 chapter 15 TextQuery