C++Primer第五版 习题答案 第九章 顺序容器

9.1

a)list,需要在中间插入数据,list最好;
b)deque,需要在头部和尾部插入删除数据;
c)vector,没有特别需求选用vector;

9.2

list<deque<int>> l;

9.3

begin和end分别指向同一个容器中的元素或者时尾元素后一个位置,可以通过反复递增begin来到达end,end不能begin之前;

9.4

#include
#include
using namespace std;

bool find_int(vector<int>::const_iterator begin_, vector<int>::const_iterator end_, int n)
{
	while (begin_!=end_)
	{
		if (*begin_ == n)
		{
			return true;
		}
		++begin_;	
	}
	return false;
}

int main()
{
	vector<int>vi{ 0,1,2,3,4,5 };
	cout << find_int(vi.begin(), vi.end(), 9) << endl;
}

9.5

#include
#include
using namespace std;

vector<int>::const_iterator find_int(vector<int>::const_iterator begin_, vector<int>::const_iterator end_, int n)
{
	while (begin_!=end_)
	{
		if (*begin_ == n)
		{
			return begin_;
		}
		++begin_;	
	}
	return begin_;
}

int main()
{
	vector<int>vi{ 0,1,2,3,4,5 };
	find_int(vi.begin(), vi.end(), 9);
	return 0;
}

9.6

while(iter1!=iter2)

9.7

vector<int>::size_type

9.8

vector<string>::const_iterator
vector<string>::iterator

9.9

begin返回iterator类型,当我们需要写操作时使用;
cbegin返回const_iterator类型,当我们不需要写操作时使用;

9.10

it1: vector<int>::iterator,
it2: vector<int>::const_iterator
it3: vector<int>::const_iterator
it4 vector<int>::const_iterator

9.11

vector<int>v1; //v1为空
vector<int>v2(v1);//v2为空
vector<int> v3 =v2;//v3为空
vector<int> v4(10); //10个0
vector<int> v5(10,1);//10个1
vector<int> v6{123}; // 1,2,3
vector<int> v7 ={1,2,3} ;//1,2,3
vector<int> v8(v7.begin(),v7.end());//1,2,3

9.12

两个容器分类型及其元素必须匹配;
如果传递的参数时迭代器的范围,不要求容器类型相同,只要能将拷贝的元素转换;

9.13

#include
#include
#include
using namespace std;


int main()
{
	list<int> ilst(5, 4);
	vector<int> ivc(5, 5);

	//form list to vector
	vector<double> dvc(ilst.begin(), ilst.end());
	for (auto i : ilst)
		cout << i << " ";
	cout << endl;
	
	for (auto i : dvc)
		cout << i << " ";
	cout << endl;

	//form vector to vector

	vector<double>dvc2(ivc.begin(), ivc.end());
	for (auto i : ivc)
		cout << i << " ";
	cout << endl;

	for (auto i : dvc2)
		cout << i << " ";
	cout << endl;

	return 0;
}

9.14

#include 
#include 
#include 

using namespace std;

int main()
{
	list<const char*> l1{ "hello","world"};
	vector<string> v1;
	v1.assign(l1.cbegin(), l1.cend());
	for (auto s : v1)	cout << s << " ";
	cout << endl;
	return 0;
}

9.15

#include 
#include 

using namespace std;

int main()
{
	vector<int>v1{ 1,2,3 };
	vector<int>v2{ 1,2 };
	cout << boolalpha << (v1 == v2) << endl;	
}

9.16

#include 
#include 
#include

using namespace std;

int main()
{
	vector<int>v1{ 1,2,3 };
	list<int>l1{ 1,2,3 };
	cout << boolalpha << (v1 == vector<int>(l1.begin(),l1.end())) << endl;	
}

9.17

c1和c2不能是无序容器;容器类型相同;元素都支持运算符。

9.18

#include 
#include
#include

using namespace std;

int main()
{
	string s;
	deque<string>d;
	while (cin>>s)
	{
		d.push_back(s);
	}
	/*for (auto s : d)
		cout << s << " ";
	cout << endl;*/
	for (auto iter = d.cbegin(); iter != d.cend(); ++iter)
		cout << *iter << " ";
	cout << endl;
	return 0;

}

9.19

只需要将deque改为list

#include 
#include
#include

using namespace std;

int main()
{
	string s;
	list<string>l;
	while (cin>>s)
	{
		l.push_back(s);
	}
	for (auto it=l.cbegin();it!=l.cend();++it)
	{
		cout << *it << " ";
	}
	cout << endl;
	return 0;
}

9.20

#include 
#include
#include
using namespace std;

int main()
{
	list<int>list1 = { 1,2,3,4,5,6,7,8,9 };
	deque<int>deque_odd, deque_even;
	for (const auto i : list1)
		(i % 2) ? deque_odd.push_back(i) : deque_even.push_back(i);

	for (const auto i : deque_even)
		cout << i << " ";
	cout << endl;
	for (const auto i : deque_odd)
		cout << i << " ";
	cout << endl;
	return 0;
}

9.21

还是一样的操作,只是将list换位vector,在特定的位置反复插入元素;

9.22

问题
1,循环不会停止;
2、迭代器在插入后会发生改变

9.23

同一个元素;

9.24

#include 
#include

using namespace std;

int main()
{
	vector<int>v1;
	cout << v1.at(0) << endl; //std::out of range
	cout << v1[0] << endl; 
	cout << v1.front() << endl;
	cout << *v1.begin() << endl;
	
}

9.25

如果elem1等于elem2,则一个元素都不会删除;
如果elem2是尾后迭代器,则会从elem1删除到最后一个元素;
如何elem1和elem2都是尾后迭代器,则一个元素不会删除;

9.26

#include 
#include
#include

using namespace std;

int main()
{
	int ia[] = { 0,1,1,2,3,5,8,13,21,55,89};
	vector<int> v1(ia, end(ia));
	list<int>l1(ia, end(ia));

	for (auto iter = v1.begin(); iter != v1.end(); )
	{
		if (*iter % 2 == 0)  iter = v1.erase(iter);
		else
			++iter;
	}

	for(auto iter =l1.begin();iter!=l1.end();)
	{
		if (*iter % 2) iter = l1.erase(iter);
		else
			++iter;
	}

	for (const auto i : v1)
		cout << i << " ";
	cout << endl;

	for (const auto i : l1)
		cout << i << " ";
	cout << endl;
	return 0;
}

9.27

#include 
#include

using namespace std;

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

9.28

#include 
#include
#include

using namespace std;

void insert_string(forward_list<string>& flst, const string& find_str, const string& insert_string)
{
	auto prev = flst.before_begin();
	auto curr = flst.begin();
	while (curr!=flst.end())
	{
		if (*curr == find_str)
		{
			flst.insert_after(curr, insert_string);
			return;
		}
		else
		{
			prev = curr;
			++curr;
		}
	}
	flst.insert_after(prev, insert_string);
}

int main()
{
	forward_list<string> flst = { "aa","bb","cc","dd" };
	insert_string(flst,"hello","world");
	for (const auto s : flst)
		cout << s << " ";
	cout << endl;
	
}

9.29

添加75个元素,并且对新元素进行初始化;
删除后面90个元素;

9.30

如果元素类型是类类型,则元素类型必须提供一个默认构造函数;

9.31

list

#include 
#include
#include

using namespace std;

int main()
{
	list<int> l1 = { 0,1,2,3,4,5,6,7,8,9 };
	auto iter = l1.begin();
	while (iter!=l1.end())
	{
		if (*iter % 2)
		{
			iter=l1.insert(iter, *iter);
			++iter;
			++iter;
		}
		else
		{
			iter = l1.erase(iter);
		}
	}
	for (const auto i : l1)
		cout << i << " ";
	cout << endl;
	return 0;
}

forward_list

#include 
#include
#include

using namespace std;

int main()
{
	forward_list<int> flst = { 0,1,2,3,4,5,6,7,8,9 };
	auto prev = flst.before_begin();
	auto curr = flst.begin();
	while (curr!=flst.end())
	{
		if (*curr % 2)
		{
			curr = flst.insert_after(curr, *curr);
			prev = curr;
			++curr;
		}

		else
		{
			curr = flst.erase_after(prev);
		}
	}
	for (const auto i : flst)
		cout << i << " ";
	cout << endl;
	return 0;
}

9.32

不合法,insert参数的运行顺序是未定义的,因此不确定iter运行的是iter+1还是未+1的状态;

9.33

读取访问权限异常;
原因:插入操作,如果存储空间被重新分配,则迭代器全部失效,如果没有重新分配,则插入位置之后的迭代器全部失效;

#include
#include

using namespace std;
int main()
{
	vector<int>v1 = { 0,1,2,3,4,5,6 };
	auto begin = v1.begin();
	while (begin != v1.end())
	{
		++begin;
		//begin = v1.insert(begin, 42);
		v1.insert(begin, 42);
		++begin;
	}
	for (const auto i : v1)
		cout << i << " ";
	cout << endl;

}

9.34

会无限循环,当碰到第一个奇数时,在之前插入一个奇数,iter从insert()中返回新插入元素的迭代器,+1后仍然指向刚才判断的那个奇数,下次判断还是奇数,程序陷入无限循环。

#include
#include

using namespace std;
int main()
{
	vector<int>v1 = { 0,1,2,3,4,5,6 };
	auto begin = v1.begin();
	while (begin != v1.end())
	{
		++begin;
		//begin = v1.insert(begin, *iter);
		v1.insert(begin, 42);
		++begin;
	}
	for (const auto i : v1)
		cout << i << " ";
	cout << endl;

}

9.35

size是指容器已经保存元素的数目;
capacity是指不重新分配内存的前提下最多可以保存的元素;

9.36

不可能;

9.37

list不是连续存储的;
array是固定size的

9.38

#include
#include
#include

using namespace std;
int main()
{
	vector<string>v1;
	for (string buf; cin >> buf; v1.push_back(buf))
		cout << "v1.size:" << v1.size() << " " << "v1.capacity:" << v1.capacity() << endl;
	return 0;
}

9.39

为sevc预留1024空间;
将输入添加到sevc;
将sevc的size增加当前的一半;

9.40

当读入256,512个词时,size增加到384,768,capacity不变;
当读入1000,1048词时,size增加到1500,1572,capacity增加到至少容纳当前容量;

9.41

#include
#include
#include

using namespace std;

int main()
{
	vector<char>vc = { 'h','i' };
	string s(vc.begin(),vc.end());
	for (const auto c : s)
		cout << c << " ";
	cout << endl;
}

9.42

string s;
s.reserve(100);

9.43

#include
#include

using namespace std;

void replace_with_string(string& s, const string& oldVal, const string& newVal)
{
	auto iter = s.begin();
	while (iter!=s.end())
	{
		if (oldVal == string(iter, iter + oldVal.size()))
		{
			iter = s.erase(iter, iter + oldVal.size());
			iter = s.insert(iter, newVal.begin(), newVal.end());
			iter += newVal.size();
			return;
		}
		else
			++iter;
	}	
}

int main()
{
	string s("tho thru");
	replace_with_string(s, "tho", "though");
	cout << s << endl;
	replace_with_string(s, "thru", "through");
	cout << s << endl;
	return 0;
}

9.44

#include
#include

using namespace std;

void replace_with_string(string& s, const string& oldVal, const string& newVal)
{
	string::size_type index = 0;
	while (index!=s.size())
	{
		if (oldVal == string(s, index, oldVal.size()))
		{
			s.replace(index, oldVal.size(), newVal);
		}
		++index;
	}
}

int main()
{
	string s("tho thru");
	replace_with_string(s, "tho", "though");
	cout << s << endl;
	replace_with_string(s, "thru", "through");
	cout << s << endl;
	return 0;
}

9.45

#include
#include

using namespace std;

string add_pre_post(const string& name, const string& pre, const string& post)
{
	string s=name;
	s.insert(s.begin(), pre.cbegin(), pre.cend());
	return s.append(post);
}

int main()
{
	string name("gxy");
	cout << add_pre_post(name, "Mr.", "Jr.") << endl;
	cout << add_pre_post("XZX", "Ms.", "Jr.");
}

9.46

#include
#include

using namespace std;

string add_pre_post(const string& name, const string& pre, const string& post)
{
	string s = name;
	s.insert(s.begin(), pre.cbegin(), pre.cend());
	return s.insert(s.size(), post);
}
int main()
{
	string name("gxy");
	cout << add_pre_post(name, "Mr.", "Jr.") << endl;
	cout << add_pre_post("XZX", "Ms.", "Jr.");
}

9.47

#include
#include

using namespace std;


int main()
{
	string numbers{ "0,1,2,3,4,5,6,7,8,9" };
	string alphabet { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" };
	string str{ "ab2c3d7R4E6" };

	string::size_type pos = 0;
	while ( (pos =str.find_first_of(numbers,pos))!=string::npos)
	{
		cout << "found number of index: " << pos << " element is " << str[pos] << endl;
		++pos;
	}

	 pos = 0;
	while ((pos = str.find_first_of(alphabet, pos)) != string::npos)
	{
		cout << "found alphabet of index: " << pos << " element is " << str[pos] << endl;
		++pos;
	}

	pos = 0;
	while ((pos = str.find_first_not_of(numbers, pos)) != string::npos)
	{
		cout << "found alphabet of index: " << pos << " element is " << str[pos] << endl;
		++pos;
	}
	pos = 0;
	while ((pos = str.find_first_not_of(alphabet, pos)) != string::npos)
	{
		cout << "found number of index: " << pos << " element is " << str[pos] << endl;
		++pos;
	}
	return 0;
}

9.48

string::npos

9.49

#include
#include
#include

using namespace std;


int main()
{
	ifstream ifs("letter.txt");
	if (!ifs) return -1;
	string longest;
	auto update_with = [&longest](string const& curr)
	{
		if (string::npos == curr.find_first_not_of("aceimnorsuvwxz"))
			longest = longest.size() < curr.size() ? curr : longest;
	};
	for (string curr; ifs >> curr; update_with(curr));
	cout << longest << endl;
}

9.50

#include
#include
#include

using namespace std;

int main()
{
	vector<string> v1(10, "5");
	int sum_int = 0;
	for (const auto s : v1)
		sum_int += stod(s);
	cout << sum_int << endl;
	vector<string>v2(10, "3.14");
	double sum_double=0;
	for (const auto s : v2)
		sum_double += stod(s);
	cout << sum_double << endl;
}

9.51

#include 
#include 

using namespace std;

class my_date
{
public:
	my_date(const string&);
private:
	unsigned int year;
	unsigned int month;
	unsigned int day;
};

my_date::my_date(const string& s)
{
	string date_str = s;
	string::size_type index1 = 0;
	string::size_type index2 = 0;

	if (s.find(',') != string::npos)//January 1, 1900
	{
		index1 = s.find(' ');
		index2 = s.find(',', index1 + 1);
		cout << "year: " << s.substr(index2 + 1, s.size()) << "; month: " << s.substr(0, index1) << "; day: " << s.substr(index1 + 1, index2 - index1 - 1) << endl;
		// month = stoi(s.substr(0, index1));
		if (s.find("Jan") < s.size())  month = 1;
		if (s.find("Feb") < s.size())  month = 2;
		if (s.find("Mar") < s.size())  month = 3;
		if (s.find("Apr") < s.size())  month = 4;
		if (s.find("May") < s.size())  month = 5;
		if (s.find("Jun") < s.size())  month = 6;
		if (s.find("Jul") < s.size())  month = 7;
		if (s.find("Aug") < s.size())  month = 8;
		if (s.find("Sep") < s.size())  month = 9;
		if (s.find("Oct") < s.size())  month = 10;
		if (s.find("Nov") < s.size())  month = 11;
		if (s.find("Dec") < s.size())  month = 12;
		day = stoi(s.substr(index1 + 1, index2 - index1 - 1));
		year = stoi(s.substr(index2 + 1, s.size()));
	}
	else if (s.find('/') != string::npos)//1/1/1900
	{
		index1 = s.find('/');
		index2 = s.find('/', index1 + 1);
		cout << "year: " << s.substr(index2 + 1, s.size()) << "; month: " << s.substr(0, index1) << "; day: " << s.substr(index1 + 1, index2 - index1 - 1) << endl;
		month = stoi(s.substr(0, index1));
		day = stoi(s.substr(index1 + 1, index2 - index1 - 1));
		year = stoi(s.substr(index2 + 1, s.size()));
	}
	else//Jan 1 1900
	{
		index1 = s.find(' ');
		index2 = s.find(' ', index1 + 1);
		cout << "year: " << s.substr(index2 + 1, s.size()) << "; month: " << s.substr(0, index1) << "; day: " << s.substr(index1 + 1, index2 - index1 - 1) << endl;
		// month = stoi(s.substr(0, index1));
		if (s.find("Jan") < s.size())  month = 1;
		if (s.find("Feb") < s.size())  month = 2;
		if (s.find("Mar") < s.size())  month = 3;
		if (s.find("Apr") < s.size())  month = 4;
		if (s.find("May") < s.size())  month = 5;
		if (s.find("Jun") < s.size())  month = 6;
		if (s.find("Jul") < s.size())  month = 7;
		if (s.find("Aug") < s.size())  month = 8;
		if (s.find("Sep") < s.size())  month = 9;
		if (s.find("Oct") < s.size())  month = 10;
		if (s.find("Nov") < s.size())  month = 11;
		if (s.find("Dec") < s.size())  month = 12;
		day = stoi(s.substr(index1 + 1, index2 - index1 - 1));
		year = stoi(s.substr(index2 + 1, s.size()));
	}
	cout << "year: " << year << "; month: " << month << "; day: " << day << endl;
}

int main()
{
	my_date my_date1("January 1, 1900");
	my_date my_date2("1/1/1900");
	my_date my_date3("Jan 1 1900");

	return 0;
}

9.52

#include
#include
#include

using namespace std;
int main()
{
	string expression{ "This is (gxy)." };
	bool bSeen = false;
	stack<char> stk;

	for (const auto& s : expression)
	{
		if (s == '(') { bSeen = true; continue; }
		else if (s == ')') bSeen = false;
		if (bSeen) stk.push(s);
	}

	string repstr;
	while (!stk.empty())
	{
		repstr += stk.top();
		stk.pop();
	}

	expression.replace(expression.find("(") + 1, repstr.size(), repstr);
	cout << expression << endl;

}

你可能感兴趣的:(C++,c++,开发语言,visual,studio)