c++ primer 学习笔记-第九章

习题9.4:

#include <iostream>
#include <vector>

using std::cin; using std::cout; using std::endl;
using std::vector;

using Iter = vector<int>::const_iterator;
bool find(Iter beg, Iter end, int num)
{
	while (beg != end)
	{
		if (*beg == num)
			return true;
		++beg;
	}
	return false;
}
int main()
{
	vector<int> ivec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	cout << std::boolalpha << find(ivec.cbegin(), ivec.cend(), 22) << endl;
	getchar();
	getchar();
	return 0;
}

习题9.5:

#include <iostream>
#include <vector>
#include <stdexcept>

using std::cin; using std::cout; using std::endl;
using std::vector;
using std::runtime_error;

using Iter = vector<int>::const_iterator;
Iter find(Iter beg, Iter end, int num)
{
	try{
		for (auto iter = beg; iter != end; ++iter)
			if (*iter == num)
				return iter;
		throw runtime_error("Cannot find the input number and iterator is out of range.");
	}
	catch(runtime_error err){
		cout << err.what()<<endl;
		return beg;
	}
}
int main()
{
	vector<int> ivec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	cout << *find(ivec.cbegin(), ivec.cend(), 22) << endl;
	getchar();
	getchar();
	return 0;
}

习题9.13:

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


using std::cin; using std::cout; using std::endl;
using std::vector; using std::list;


int main()
{
	list<int> lvec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	vector<int> ivec{ 11, 22, 33, 44, 55, 66, 77, 88, 99 };
	vector<double> dvec(lvec.cbegin(), lvec.cend());
	//vector<double> dvec(ivec.cbegin(), ivec.cend());
	for (auto d : dvec)
		cout << d << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题9.14:

#include <iostream>
#include <string>
#include <vector>
#include <list>

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;

int main()
{
	list<char *> clst{ "aaa", "bbb", "ccc" };
	vector<string> svec;
	svec.assign(clst.cbegin(), clst.cend());
	for (const auto s : svec)//注意此处还是该写const的啦,别彪
		cout << s << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题9.15:

#include <iostream>
#include <string>
#include <vector>
#include <list>

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;

bool compare_vec_int(const vector<int> &ivec1, const vector<int> &ivec2)
{
	if (ivec1.size() == ivec2.size())
	{
		for (auto i = 0; i != ivec1.size(); ++i)
			if (ivec1[i] != ivec2[i])
				return false;
		return true;
	}
	else
		return false;
}

int main()
{
	vector<int> ivec1{ 1, 2, 3, 4, 5 }, ivec2{ 1, 2, 3, 4, 5 };
	cout << std::boolalpha << compare_vec_int(ivec1, ivec2) << endl;
	cout << (ivec1 == ivec2 ? "true" : "false") << endl;//注意<<比?:优先级高
	getchar();
	getchar();
	return 0;
}

习题9.16:

#include <iostream>
#include <string>
#include <vector>
#include <list>

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list;

int main()
{
	const vector<int> ivec{ 1, 2, 3, 4, 5};
	const list<int> ilst{ 1, 2, 3, 4, 5 };
	cout << (vector<int>(ilst.cbegin(), ilst.cend()) == ivec ?
		"true" : "false") << endl;
	getchar();
	getchar();
	return 0;
}

习题9.18:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;

int main()
{
	deque<string> sdeq;
	for (string seq; cin >> seq; sdeq.emplace_front(seq));
	for (auto iter = sdeq.cbegin(); iter != sdeq.cend(); ++iter)
		cout << *iter << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题9.19:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;

int main()
{
	list<string> sdeq;
	for (string seq; cin >> seq; sdeq.emplace_back(seq));
	for (auto iter = sdeq.cbegin(); iter != sdeq.cend(); ++iter)
		cout << *iter << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题9.22:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;

int main()
{
	vector<int> iv{ 1, 2, 3, 4, 5 };
	vector<int>::iterator mid = iv.begin() + iv.size() / 2;
	for (auto iter = iv.begin(); iter != mid; ++iter)
		if (*iter == 2)
		{
			iter = iv.insert(iter, 2 * 4);
			mid = iter + iv.size() / 2;
			++iter;
		}
	for (auto i : iv)
		cout << i << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题9.24:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stdexcept>

using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;

using std::out_of_range;
int main()
{
	vector<int> ivec;
	try{
		cout << ivec.at(0);
	}
	catch (out_of_range err){
		cout << err.what() << endl;
	}
	getchar();
	getchar();
	return 0;
}

习题9.26:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; using std::deque;
using std::begin; using std::end;<pre name="code" class="cpp">#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;
int main()
{
	forward_list<int> iflst = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
	auto prev = iflst.before_begin();
	auto curr = iflst.begin();
	while (curr != iflst.end())
	{
		if (*curr & 0x1)
		{
			curr = iflst.erase_after(prev);
		}
		else
		{
			prev = curr;
			++curr;
		}
	}
	for (auto i : iflst)
		cout << i << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

int main(){int a[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };vector<int> ivec(begin(a), end(a));list<int> ilst(begin(a), end(a));for (auto iter = ivec.begin(); iter != ivec.end();)if (!(*iter & 0x1))iter = ivec.erase(iter);else++iter;for (auto i : ivec)cout << i << " ";cout << endl;for (auto iter = ilst.begin(); iter != ilst.end();)if (*iter & 0x1)iter = ilst.erase(iter);else++iter;for (auto i : ilst)cout << i << " ";cout << endl;getchar();getchar();return 0;}

 习题9.27: 

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

void insert_string(forward_list<string> &sflst, const string &s1, const string &s2)
{
	auto prev = sflst.before_begin();
	auto curr = sflst.begin();
	bool flag = true;
	while (curr != sflst.end())
		if (*curr == s1)
		{
			flag = false;
			prev = sflst.insert_after(curr, s2);
			curr = ++prev;
		}
		else
		{
			++curr;
			++prev;
		}
	if (flag)
	{
		sflst.insert_after(prev, s2);
	}
}
int main()
{
	forward_list<string> sflst{ "a", "b", "c", "d", "b", "e" };
	string s1("b"), s2("插");
	insert_string(sflst, s1, s2);
	for (auto s : sflst)
		cout << s << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}

习题9.31:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

int main()
{
	list<int> ilst{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	auto iter = ilst.begin();
	while (iter != ilst.end())
		if (*iter & 0x1){
			iter = ilst.insert(iter, *iter);
			++(++iter);
		}
		else
		{
			iter=ilst.erase(iter);
		}
	for (auto i : ilst)
		cout << i << " ";
	cout << endl;
	getchar();
	getchar();
	return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

int main()
{
	forward_list<int> iflst{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	auto prev = iflst.before_begin();
	for (auto curr = iflst.begin(); curr != iflst.end();)
	{
		if (*curr & 0x1)
		{
			curr = ++(iflst.insert_after(curr, *curr));
			++(++prev);
		}
		else
			curr = iflst.erase_after(prev);
	}
	for (auto i : iflst)
		cout << i << " ";
	cout << endl;
	cout << endl;
	getchar();
	getchar();
	return 0;
}


习题9.38:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

int main()
{
	vector<int> ivec;
	for (auto i = 0; i < 100; ++i)
	{
		cout << i << ":  siez-" << ivec.size() << "  capacity-" << ivec.capacity() << endl;
		ivec.push_back(0);
	}
	getchar();
	getchar();
	return 0;
}

习题9.41:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

int main()
{
	vector<char> cvec{ 'a', 'b', 'c' };
	string str(cvec.begin(),cvec.end());
	cout << str << endl;
	getchar();
	getchar();
	return 0;
}

习题9.43:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

void replace_s(string &s, const string &oldVal, const string &newVal)
{
	for (auto iter = s.begin(); s.end() - iter >= oldVal.size();)
	{
		if (oldVal == string(iter, iter + oldVal.size()))
		{ //先检查防止out of range(参考git后发现前者写在for内更好),然后再检测string是否相同
			iter=s.erase(iter, iter + oldVal.size());
			iter=s.insert(iter, newVal.begin(), newVal.end());
			iter += newVal.size();
		}
		else
			++iter;//一开始不作区分没有将++iter写在else里,后发现会多跳过一个
	}              //若是连续两个oldVal就会检测不到了
}

int main()
{
	string s("Hello,everybabybaby!");
	replace_s(s, "baby", "body");
	cout << s << endl;
	getchar();
	getchar();
	return 0;
}

习题9.44:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

void replace_s(string &s, const string &oldVal, const string &newVal)
{
	for (auto i = 0; i <= s.size() - oldVal.size();)//注意条件的写法,将i写在一边
	{
		if (string(s, i, oldVal.size()) == oldVal)//github上用的substr,可能比用构造函数好
		{
			s.replace(i, oldVal.size(), newVal);
			i += oldVal.size();
		}
		else
			++i;
	}
}

int main()
{
	string s("Hello,everybabybaby!");
	replace_s(s, "baby", "body");
	cout << s << endl;
	getchar();
	getchar();
	return 0;
}

习题9.45:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

string &pre_post(string &name, const string &pre, const string &post)
{
	name.insert(name.begin(), pre.begin(), pre.end());//只能这样赋,不能一个pre就赋值,参加p305用法
	name.append(post.begin(), post.end());
	return name;
}

int main()
{
	string name = "Ge";
	cout << pre_post(name, "Mr.", "III") << endl;
	getchar();
	getchar();
	return 0;
}

习题9.46:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

string pre_post(const string &name, const string &pre, const string &post)
{
	string full(name);
	full.insert(0,pre);
	return full;
}

int main()
{
	cout << pre_post("Ge", "Mr.", "III") << endl;
	getchar();
	getchar();
	return 0;
}

习题9.47:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

int main()
{
	string number("0123456789"),
		alphabet("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM");
	string name("ab2c3d7R4E6");
	string::size_type pos=0;
	while ((pos = name.find_first_not_of(alphabet, pos)) != string::npos)
	{
		cout << "find number at index: " << pos
			<< "  element is " << name[pos] << endl;
		++pos;
	}
	cout << endl;
	pos = 0;
	while ((pos = name.find_first_not_of(number, pos)) != string::npos)
	{
		cout << "find char at index: " << pos
			<< "  element is " << name[pos] << endl;
		++pos;
	}
	getchar();
	getchar();
	return 0;
}


习题9.49:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::istream; using std::ifstream;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

bool is_not_cender(string &word)
{
	if (word.find_first_not_of("aceimnorsuvwxz") == string::npos)
		return true;
	else
		return false;
}
bool longest(istream &is, string &long_word)
{
	bool exist = false;
	for (string word; is >> word;)
		if (is_not_cender(word))
			if (long_word.empty())
			{
				long_word = word;
				exist = true;
			}
			else
				if (word.size() > long_word.size())
					long_word = word;
	return exist;
}

int main()
{
	ifstream infile("words.txt");
	string long_word;
	if (longest(infile, long_word))
		cout << long_word << ": " << long_word.size() << endl;
	else
		cout << "all cenders" << endl;
	getchar();
	getchar();
	return 0;
}



习题9.50:

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <forward_list>
#include <stdexcept>
#include <iterator>
using std::cin; using std::cout; using std::endl;
using std::string; using std::vector; using std::list; 
using std::deque; using std::forward_list;
using std::begin; using std::end;

int main()
{
	vector<string> svec1{ "1", "2", "3", "4", "5" };
	int sum1 = 0;
	for (auto s : svec1)
		sum1 += stoi(s);
	cout << sum1 << endl;

	vector<string> svec2{ "1.1", "2.2", "0.1e1", "4.6", "-5" };
	double sum2 = 0;
	for (auto s : svec2)
		sum2 += stod(s);
	cout << sum2 << endl;
	getchar();
	getchar();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值