C++primer第九章

(1)9_4答案

bool lookfor_val ( vector<int>::iterator  bn ,vector<int>::iterator ed,int val)
{
	for (; bn!=ed; bn++)
	{
		if (*bn==val)
		{
			return true;
			break;
		}
	}
	return false;
}
int main()
{
	vector<int> vec{1,2,3,4};
	vector<int>::iterator begin = vec.begin();
	vector<int>::iterator end = vec.end();
	bool resault = lookfor_val(begin,end,0);

	cout << resault << endl;
	system("pause");
	return 0;
}

(2)9_5答案

vector<int>::iterator lookfor_val(vector<int>::iterator  bn, vector<int>::iterator ed, int val)
{
	vector<int>::iterator null = bn;
	for (; bn != ed; bn++)
	{
		if (*bn == val)
		{
			return bn;
			break;
		}	
	}
	//cout << "没有要找得值" << endl;输出语句在主函数中处理
	return ed;//怎么解决没有相等得值得情况
	
}
int main()
{
	vector<int> vec{ 1,2,3,4 };
	vector<int>::iterator begin = vec.begin();
	vector<int>::iterator end = vec.end();
	int look_for_number = 0;
	if (lookfor_val(begin, end, look_for_number)!=end)
	{
		int resault = *lookfor_val(begin, end, look_for_number);
		cout << resault << endl;
	}
	else
	{
		cout << "没有要找的值" << endl;
	}
	system("pause");
	return 0;
}

(3)9_14答案

void main()
{
	list<char* > li = {"wo","shi","shui"};
	vector<string> str;
	str.assign(li.begin(),li.end());
	for (auto c:str)
	{
		cout << c << endl;
	}
	system("pause");
	return;
}

(4)9_18答案

int main()
{
	//在deque尾部添加元素
	deque<string> deq;
	string str;
	cout << "please input the string" << endl;
	/*while (cin >> str)
	{
		deq.push_back(str);
	}
	for (auto c:deq)
	{
		cout << c << endl;
	}*/
	//在deque首部添加元素
	while (cin >> str)
	{
		deq.push_front(str);
	}
	for (deque<string>::iterator it = deq.begin(); it != deq.end();it++)
	{
		cout << *it << endl;
	}
	system("pause");
	return 0;
}
(5) 9_20答案
int main()
{
	list<int> li{1 ,2 ,3 ,4 ,5 ,6};
	deque<int> deq1, deq2;
	for (list<int>::iterator it = li.begin(); it != li.end(); it++)
	{
		if ((*it)%2==0)
		{
			deq1.push_back(*it);
		}
		else
		{
			deq2.push_back(*it);
		}
	}
	for (auto c : deq1)
	{
		cout << "deq1="<<c << endl;
	}
	for (auto c : deq2)
	{
		cout << "deq2=" << c << endl;
	}

	system("pause");
	return 0;
}



猜你喜欢

转载自blog.csdn.net/weixin_41484240/article/details/80150046