c++primer 第五版 第九章顺序容器

ex9.2

#include<iostream>
#include<list>
#include<deque>

using namespace std;

int main()
{
	list<deque<int>>ldi;
	return 0;
}

ex9.4

#include<iostream>
#include<list>
#include<deque>//双端队列 顺序容器
#include<vector>

using namespace std;

typedef vector<int>::iterator it;

bool find(it begin, it end, int n) {
	while (begin != end) {
		if (*begin == n)return true;
		++begin;
	}
	return false;
}
int main()
{
	vector<int>vi = { 1,5,4,7,6,8 };
	cout << find(vi.begin(),vi.end(),7 );
	return 0;
}

ex9.5

#include<iostream>
#include<list>
#include<deque>//双端队列 顺序容器
#include<vector>

using namespace std;

typedef vector<int>::iterator it;

it find(it begin, it end, int n) {
	while (begin != end) {
		if (*begin == n)return begin;
		++begin;
	}
	return end;
}
int main()
{
	vector<int>vi = { 1,5,4,7,6,8 };
	it tep = find(vi.begin(), vi.end(), 7);
	if (find(vi.begin(), vi.end(), 7) == vi.end())cout << "false";
	cout <<*( tep);
	return 0;
}

ex9.6

#include<iostream>
#include<list>
#include<deque>//双端队列 顺序容器
#include<vector>

using namespace std;

typedef vector<int>::iterator it;

it find(it begin, it end, int n) {
	while (begin != end) {
		if (*begin == n)return begin;
		++begin;
	}
	return end;
}
int main()
{/*
	vector<int>vi = { 1,5,4,7,6,8 };
	it tep = find(vi.begin(), vi.end(), 7);
	if (find(vi.begin(), vi.end(), 7) == vi.end())cout << "false";
	cout <<*( tep);*/
	list<int>lst1;
	list<int>::iterator iter1 = lst1.begin(), iter2 = lst1.end();
	while (iter1 != iter2) {// <不支持 改为!=
		++iter1;
	}

	return 0;
}

ex9.18

#include<iostream>
#include<deque>
#include<string>

using namespace std;

int main()
{
	deque<string>str;
	string s;
	while (cin >> s)str.push_back(s);
	for (deque<string>::iterator it = str.begin();it != str.end();++it) {
		cout << *it << endl;
	}
	return 0;
}

ex9.19

#include<iostream>
#include<deque>
#include<string>
#include<list>
using namespace std;

int main()
{
	list<string>str;
	string s;
	while (cin >> s)str.push_back(s);
	for (list<string>::iterator it = str.begin();it != str.end();++it) {
		cout << *it << endl;
	}
	return 0;
}
#include<iostream>
#include<deque>
#include<string>
#include<list>
using namespace std;

int main()
{
	deque<string>str;
	string s;
	while (cin >> s)str.push_back(s);
	for (deque<string>::iterator it = str.begin();it <str.end();++it) {//可以用<
		cout << *it << endl;
	}
	return 0;
}

ex9.20

#include<iostream>
#include<deque>
#include<list>

using namespace std;

int main()
{
	list<int>num = {1,2,3,5,6};
	deque<int>odd, even;

	for (list<int>::iterator it = num.begin();it != num.end();++it) {
		if (*it % 2) {
			odd.push_back(*it);
		}
		else {
			even.push_back(*it);
		}
	}
	for (deque<int>::iterator it = odd.begin();it != odd.end();++it) {
		cout << *it << " ";
	}
	cout << endl;
	for (deque<int>::iterator it = even.begin();it != even.end();++it) {
		cout << *it << " ";
	}
	cout << endl;
	return 0;

}

ex9.32

#include<iostream>
#include<list>
#include<deque>//双端队列 顺序容器
#include<vector>

using namespace std;

typedef vector<int>::iterator it;

it find(it begin, it end, int n) {
	while (begin != end) {
		if (*begin ++== n)return begin;
	/*	++begin;*/
	}
	return end;
}
int main()
{
	vector<int>vi = { 1,5,4,7,6,8 };
	it tep = find(vi.begin(), vi.end(), 7);
	if (find(vi.begin(), vi.end(), 7) == vi.end())cout << "false";
	cout << *(tep);
	return 0;
}

注意第12行++后–

#include<iostream>
#include<list>
#include<deque>//双端队列 顺序容器
#include<vector>

using namespace std;

typedef vector<int>::iterator it;

it find(it begin, it end, int n) {
	while (begin != end) {
		if (*begin ++== n)return --begin;
	/*	++begin;*/
	}
	return end;
}
int main()
{
	vector<int>vi = { 1,5,4,7,6,8 };
	it tep = find(vi.begin(), vi.end(), 7);
	if (find(vi.begin(), vi.end(), 7) == vi.end())cout << "false";
	cout << *(tep);
	return 0;
}

ex10.2

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
	vector<int>vec_int;
	int n, t, val;
	cin >> n;
	while (n--) {
		cin >> t;
		vec_int.push_back(t);
	}
	cin >> val;
	int cnt = count(vec_int.begin(), vec_int.end(), val);
	cout << cnt;
		//1 2 4 2() 3;最初的排序
	//1 2 2() 3 4;稳定的排序2和2()次序不变
	//1 2() 2 3 4 ;//不稳定的排序2和2()次序变了
	return 0;
}
ex10.2 输入
5
1 2 1 2 1
1
就可以查1出现几次
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

bool cmp(string& s) {
	return s.size() >= 5;
}

int main()
{
	vector<string>words;
	string str;
	while (cin >> str) words.push_back(str);

	vector<string>::iterator it = partition(words.begin(), words.end(), cmp);
		for (vector<string>::iterator i = words.begin();i != it;++i)
			cout << *i << endl;
	return 0;
}
ex10.13

命令行输入
hello
c_++
world
type
^Z

ex10.14

#include<iostream>

using namespace std;

int main()
{
	auto f = [](int a, int b) {
		return a + b;
	};
	cout << f(3, 5);
	return 0;
}

ex10.15

#include<iostream>

using namespace std;

int sum(int a, int b)
{
	auto f = [a](int c) {
		return a + c;
	};
	return f(b);
}
int main()
{
	cout << sum(3, 7);
	

	return 0;
}

ex10.27

#include<iostream>
#include<list>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
	vector<int>vec = { 1,1,2,3,4 };
	list<int>lst;
	lst.resize(6);//大小
	unique_copy(vec.begin(), vec.end(), lst.begin());//去重 最后没有就0代替

	for (list<int>::iterator it = lst.begin();it != lst.end();++it)
		cout << *it << " ";
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值