c++primer第五版课后练习答案(第九章)

chapter9_9.4

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int search(vector<int> s, int num)
{
	int flag = 0;
	for (auto it = s.begin(); it != s.end(); ++it)
	{
		if (*it == num)
		{
			flag = 1;
			break;
		}
	}
	if (flag)
		return 1;
	else
		return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> ivec{1,2,3,4,5,6,7,8,9};
	cout<<search(ivec, 6)<<endl;
	return 0;
}

chapter9_9.5

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int search(vector<int> s, int num)
{
	int flag = 0;
	for (auto it = s.begin(); it != s.end(); ++it)
	{
		if (*it == num)
		{
			flag = 1;
			break;
		}
	}
	if (flag)
		return num;
	else
	{
		cout << "容器中无此元素!"<<endl;
		return 0;
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	search(ivec, 15);
	return 0;
}

chapter9_9.15

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
bool compare(vector<int>v1, vector<int> v2)
{
	int flag = 0;
	if (v1.size() == v2.size())
	{
		for (auto it1 = v1.begin(), it2 = v2.begin(); it1 != v1.end(); it1++, it2++)
		{
			if (*it1 == *it2)
			{
				flag = 1;
				continue;
			}
			else
			{
				flag = 0;
				break;
			}
		}
		if (flag)
			return true;
		else
			return false;
	}
	else
		return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> s1 = { 1, 3, 4 };
	vector<int> s2 = { 1, 3, 2 };
	if (compare(s1, s2))
		cout << "两容器相等" << endl;
	else
		cout << "两容器不相等" << endl;
	return 0;
}

chapter9_9.18

#include "stdafx.h"
#include <deque>
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	deque<string> d;
	string word;
	while (cin >> word)
		d.push_back(word);
	for (auto it = d.begin(); it != d.end(); it++)
		cout << *it << endl;
	return 0;
}

chapter9_9.23

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> ivec{1};
	cout << "容器的长度="<<ivec.size()<<endl;
	cout << "-------------------"<<endl;
	if (!ivec.empty())
	{
		auto val1 = *ivec.begin();
		auto val2 = ivec.front();
		auto last = ivec.end();
		auto val3 = *(--last);
		auto val4 = ivec.back();
		cout << val1 << endl;
		cout << val2 << endl;
		cout << val3 << endl;
		cout << val4 << endl;
	}
	return 0;
}
结果:


chapter9_9.24

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int fun1_at(vector<int> ivec)
{
	if (ivec.empty())
		return NULL;
	else
		return ivec.at(0);
}
int fun2_xb(vector<int> ivec)
{
	if (ivec.empty())
		return NULL;
	else
		return ivec[0];
}
int fun3_front(vector<int> ivec)
{
	if (ivec.empty())
		return NULL;
	else
		return ivec.front();
}
int fun4_begin(vector<int> ivec)
{
	if (ivec.empty())
		return NULL;
	else
		return *ivec.begin();
}
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> vec{3,2,1};
	cout << fun1_at(vec) << endl;
	cout << fun2_xb(vec) << endl;
	cout << fun3_front(vec) << endl;
	cout << fun4_begin(vec) << endl;
	return 0;
}

chapter9_9.25

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> v{ 2, 3, 4, 5, 6, 7 };
	auto it = v.end();
	it = v.erase(it-1, it);
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << endl;
	}
	return 0;
}

chapter9_9.26

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	int a[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 99 };
	vector<int> v1;
	list<int> v2;
	for (int i = 0; i < sizeof(a) / sizeof(int); i++)
	{
		v1.push_back(a[i]);
		v2.push_back(a[i]);
	}
	for (auto it = v1.begin(); it != v1.end();)
	{
		if (*it % 2==0)
			it = v1.erase(it);
		else
			it++;
	}
	for (auto it = v2.begin(); it != v2.end();)
	{
		if (*it % 2==1)
			it = v2.erase(it);
		else
			it++;
	}
	for (int i = 0; i < v1.size(); i++)
		cout << setw(3)<<v1[i];
	cout << endl<<"-----------------" << endl;
	for (auto it = v2.begin(); it != v2.end();it++)
		cout << setw(3)<<*it;
	cout << endl;
	return 0;
}


chapter9_9.27

#include "stdafx.h"
#include <iostream>
#include <forward_list>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	forward_list<int> flist = { 2, 1, 3, 5, 4, 6, 7, 8, 9 };
	auto pre = flist.before_begin();
	auto p = flist.begin();
	while (p!=flist.end())
	{
		if (*p % 2)
		{
			p = flist.erase_after(pre);
		}
		else
		{
			pre = p;
			p++;
		}
	}
	for (auto q = flist.begin(); q != flist.end(); q++)
	    	cout << *q;
	cout << endl;
	return 0;
}


chapter9_9.28

#include "stdafx.h"
#include <forward_list>
#include <iostream>
#include <string>
using namespace std;
forward_list<string>::iterator func(forward_list<string> &list, string word1, string word2)
{
	auto p = list.begin();
	int flag = 0;
	while (p != list.end())
	{

		if (*p == word1)
		{
			flag = 1;
			list.insert_after(p, word2);
			p++;
		}
		else
		{  
			p++;
		}
	}
	if (flag == 0)
		list.insert_after(p, word2);

	return list.begin();
}
int _tmain(int argc, _TCHAR* argv[])
{
	forward_list<string> flist{ "a", "abb", "abc" };
	auto q=func(flist, "abb", "cc");
	for (; q != flist.end(); q++)
		cout << *q << endl;
	return 0;
}


chapter9_9.29

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> vec(25, 1);
	//初始化,vector中有25个1
	for (vector<int>::iterator vi = vec.begin(); vi != vec.end(); vi++)
		cout << *vi << endl;
	cout << "-------------------------------------" << endl;
	vec.resize(100);
	//将75个0元素添加到vector的末尾
	for (vector<int>::iterator vi = vec.begin(); vi != vec.end(); vi++)
		cout << *vi << endl;
	cout << "-------------------------------------" << endl;
	vec.resize(10);
	//从vector末尾删除90个元素
	for (vector<int>::iterator vi = vec.begin(); vi != vec.end(); vi++)
		cout << *vi << endl;
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值