Data structures and Algorithm Analysis in C++ vector iterator not dereferenable


// Data structures and Algorithm Analysis in C++
// ex 3-3
#include<iostream>
#include<iterator>
#include<assert.h>
#include<vector>
#include<string>

using namespace std;

template <typename Iterator,typename Object>
Iterator find(Iterator start,Iterator end,const Object& x)
{
	Iterator iter = start;
	while(iter != end && *iter != x)
	{
		cout << " *iter =  " << *iter << " ; x = " << x << endl;
		cout << (*iter == x) << endl;
		iter++;
	}
	return iter;
}

int main()
{
	vector<string> vec;
	vec.push_back("hello");
	vec.push_back("world");
	vec.push_back("this ");
	vec.push_back("is ");
	vec.push_back("c++ ");
	string s = "hello ";
	vector<string>::iterator iter = find(vec.begin(),vec.end(),s);
	//assert(iter!=vec.end());
	cout << *iter << endl;
}

notes: 如果不进行判断处理,直接进行cout ,在没有找到的情况下cout会出现“vector iterator  not dereferenable”的错误。


The fourth edition of Data Structures and Algorithm Analysis in C++ describes data structures, methods of organizing large amounts of data, and algorithm analysis, the estimation of the running time of algorithms. As computers become faster and faster, the need for programs that can handle large amounts of input becomes more acute. Paradoxically, this requires more careful attention to efficiency, since inefficiencies in programs become most obvious when input sizes are large. By analyzing an algorithm before it is actually coded, students can decide if a particular solution will be feasible. For example, in this text students look at specific problems and see how careful implementations can reduce the time constraint for large amounts of data from centuries to less than a second. Therefore, no algorithm or data structure is presented without an explanation of its running time. In some cases, minute details that affect the running time of the implementation are explored. Once a solution method is determined, a program must still be written. As computers have become more powerful, the problems they must solve have become larger and more complex, requiring development of more intricate programs. The goal of this text is to teach students good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amount of efficiency. This book is suitable for either an advanced data structures course or a first-year graduate course in algorithm analysis. Students should have some knowledge of intermediate programming, including such topics as pointers, recursion, and object-based programming, as well as some background in discrete math.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值