WD—C++课前作业—30题

怎么会手和脚都在桌子上

目录

31,声明一个类 String,其数据成员为 char head[100],构造函数 String(char*Head)实现 head 的初始化,成员函数 void reverse()实现 head 内字符串的逆序存放,成员函数 void print()实现 head 内字符串的输出。

32.定义盒子 Box类,要求具有以下成员:可设置盒子形状;可计算盒子体积;可计算盒子的表面积。

33.声明一个 Tree(树)类,有成员 ages(树龄),成员函数grow(int years)用以对 ages 加上 years,showAge()用以显示 tree 对象的 ages 值。在主函数中定义 Tree 类对象,并调用成员函数(自行指定实参数据)

34.有一个学生类 Student,包括学生姓名、成绩,设计一个友元函数,输出成绩对应的等级:(1)大于等于 90:优;(2)80~90:良;(3)70~79:中;(4)60~69:及格;(5)小于 60:不及格。

35.定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。

36.有一个 Time 类,包含数据成员 minute(分)和 sec(秒),模拟秒表,每次走一秒,满 60 秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。(提示:重载单目运算符++)

37.设计一个三角形类 Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。

38.使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。

39.定义一个抽象类 Shape 用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。程序中通过基类指针来调用派生类中的虚函数,计算不同形状的面积。

40.定义计数器类 Counter。要求具有以下成员:计数器值;可进行增值和减值记数;可提供记数值。

41.声明一个哺乳动物 Mammal 类,再由此派生出狗 Dog类,二者都定义 speak(成员函数,基类中定义为虚函数。声明一个 Dog类的对象,调用 speak()函数,观察运行结果。

42.编写一个程序计算“三角形、正方形、圆形”三种图形的面积,要求  :a)抽象出一个基类 Base;  b)在其中说明一个虚函数用来求面积;   c)利用派生类定义“三角形、正方形、圆形”;  编写主函数并测试。

C++字符串(std::string)、STL(标准模板库)相关;(这一部分的作业,请参考 C++ Primer5 的练习)

43.编写一段程序,从标准输入中一次读入一整行,存入 std::string 中,然后修改该程序,使其一次读入一个词。(练习3.2)

44.请说明std::string类的输入运算符和 getline函数分别是如何处理空白字符的。(练习3.3)

45.编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分隔开来。(练习 3.5)

46.编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。(练习 3. 10)

47.编写一段程序,创建一个含有 10 个整数的 vector 对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出 vector 对象的内容,检验程序是否正确。(练习 3.23)

48.编写一段程序,比较两个 std::string 对象。再编写一段程序,比较两个C风格字符串的内容。(练习3.39)

49.对于下面的程序任务,vector、deque 和 list 哪种容器最为合适?解释你的选择的理由。如果没有哪一种容器优于其他容器,也请解释理由。(练习9.1)(1)读取固定数量的单词,将他们按字典序插入到容器中。(2)读取未知数量的单词,总是将新单词插入到末尾。删除操作在头部进行。(3)从一个文件读取未知数量的整数。将这些数排序,然后将他们打印到标准输出。

50.对6种创建和初始化 vector 对象的方法,每一种都给出一个实例。解释每个 vector 包含什么值。(练习9.11)

51.如何从一个 list初始化一个 vector?从一个 vector又该如何创建?编写代码验证你的答案。(练习9.13)

52.编程程序,将一个 1ist 中的 char*指针(指向C风格字符串)元素赋值给-个vector中的string。(练习9.14) 

53,编写程序,从标准输入读取 string 序列,存入一个 deque 中。编写一个循环,用迭代器打印 deque 中的元素。(练习 9.18)

54.编写程序,从一个 1ist拷贝元素到两个 deque 中,其中值为偶数的所有元素都拷贝到一个 deque 中,而奇数元素都拷贝到另一个 deque 中。(9.20)

​55,假定你希望每次读取一个字符存入一个 std::string 中,而且知道最少需要读取 100个字符,应该如何提高程序的性能?(9.42)

​56.编写一个函数,接受一个表示名字的 std::string 参数和两个分别表示前缀(如“Mr.”或“Ms.”)和后缀(如“Jr.”“III”)的字符串。使用迭代器及insert 和 append 函数将前缀和后缀加到给定的名字中,生成新的 string 并返回。(9.45)

57.定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们)的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。(11.7)

0.1  bug,只有第一个添加人的函数能进入循环

​0.2 正常的

58,编写一个程序,在一个 vector 而不是一个 set 中保存不重复的单词。使用set的优点是什么?(练习 11.8)

59.可以用什么类型来对一个 map 进行下标操作?下标运算符返回的类型时什么?请给出一个具体例子,即定义一个 map,然后写出一个可以用来对 map 进行下标操作的类型以及下标运算符将会返回的类型。(11.26)

60.用冒泡法对 10 个整数排序。(用 STL 的 vector 容器实现)​​​​​​​

31,声明一个类 String,其数据成员为 char head[100],构造函数 String(char*Head)实现 head 的初始化,成员函数 void reverse()实现 head 内字符串的逆序存放,成员函数 void print()实现 head 内字符串的输出。
#include <iostream>
#include <cstring>
using namespace std;
/*
声明一个类 String,其数据成员为 char head[100]
构造函数 String(char*Head)实现 head 的初始化
成员函数 void reverse()实现 head 内字符串的逆序存放
成员函数 void print()实现 head 内字符串的输出。
*/
class String {
public:
	String(char* Head) {
		strcpy_s(head, sizeof(head), Head);
	}
	void reverse() {
		int len = strlen(head);
		for (int i = 0; i < len / 2; i++) {
			char temp = head[i];
			head[i] = head[len - i - 1];
			head[len - i - 1] = temp;
		}
	}
	void print() {
		cout << head << endl;
	}
	char head[100];
};
void test01() {
	String s(const_cast<char*>("hallo,world"));
	s.print();
	s.reverse();
	s.print();
}
int main() {
	test01();
	system("pause");
	return 0;
}

BUG:在你的代码中,test01 函数中创建 String 对象时,实参类型是 const char* 类型,但是构造函数参数类型是 char*,导致类型不匹配的错误。为了解决这个问题,你可以将构造函数的参数类型改为 const char*,或者在调用构造函数时将实参的类型转换为 char* 

32.定义盒子 Box类,要求具有以下成员:可设置盒子形状;可计算盒子体积;可计算盒子的表面积。
#include <iostream>
#include <string>
using namespace std;
class BOX {
public:
	BOX(int l,int w,int h) {
		m_l = l; m_w = w; m_h = h;
	}
	int gets() {
		return  m_l *m_w *m_h;
	}
	int getv() {
		return 2 * (m_l * m_w + m_l * m_h + m_w * m_h);
	}
	void shape() {
		cout << "BOX的长,宽,高分别为:" << m_l << " " << m_w << " " << m_h << endl;
	}
	int m_l;
	int m_w;
	int m_h;
};
void test01() {
	BOX b1(3, 4, 5);
	b1.shape();
	cout << "体积是:" << b1.getv() << endl;
	cout << "面积是:" << b1.gets() << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}

33.声明一个 Tree(树)类,有成员 ages(树龄),成员函数grow(int years)用以对 ages 加上 years,showAge()用以显示 tree 对象的 ages 值。在主函数中定义 Tree 类对象,并调用成员函数(自行指定实参数据)
#include <iostream>
#include <string>
using namespace std;
/*
* 声明一个 Tree(树)类,有成员 ages(树龄)
成员函数grow(int years)用以对 ages 加上 years
showAge()用以显示 tree 对象的 ages 值
在主函数中定义 Tree 类对象,并调用成员函数(自行指定实参数据)
*/
class Tree {
public:
	Tree() {
		ages = 0;
	}
	Tree(int age) {
		this->ages = age;
	}
	void grow(int years) {
		this->ages += years;
	}
	void showAge() {
		cout << "树的年龄为:" << ages << endl;
	}
	int ages;
};
void test01() {
	Tree t1(78);
	t1.showAge();
	t1.grow(200);
	t1.showAge();
}
int main() {
	test01();
	system("pause");
	return 0;
}

34.有一个学生类 Student,包括学生姓名、成绩,设计一个友元函数,输出成绩对应的等级:
(1)大于等于 90:优;(2)80~90:良;(3)70~79:中;(4)60~69:及格;(5)小于 60:不及格。
#include <iostream>
#include <ctime>
using namespace std;
/*
有一个学生类 Student,包括学生姓名、成绩,设计一个友元函数,输出成绩对应的等级:
(1)大于等于 90:优;(2)80~90:良;(3)70~79:中;(4)60~69:及格;(5)小于 60:不及格。
*/
class Student {
	friend void classgrade(Student& s);
public:
	Student(string name, int sc) {
		n_name = name;
		n_score = sc;
	}
	void show() {
		cout << "姓名:" << n_name << "\t成绩:" << n_score << "\t等级:";
	}
private:
	string n_name;
	int n_score;
};
void classgrade(Student& s) {
	int sco = s.n_score / 10;
	switch (sco) {
	case 10:cout << "优" << endl; break;
	case 9:cout << "优" << endl; break;
	case 8:cout << "良" << endl; break;
	case 7:cout << "中" << endl; break;
	case 6:cout << "及格" << endl; break;
	default:cout << "不及格" << endl; break;
	}
}
void test01() {
	srand((unsigned int)time(NULL));
	Student s1("栋栋",rand()%51+50);
	s1.show();
	classgrade(s1);
}
int main() {
	test01();
	system("pause");
	return 0;
}

35.定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。
#include <iostream>
#include <ctime>
using namespace std;
/*
.定义一个复数类,用友元函数实现对双目运算符“+”的运算符重载,使其适用于复数运算。
*/
class Complex {
	friend Complex& operator+(Complex& c1, Complex& c2);
public:
	Complex(int a1, int b1) {
		_a = a1;
		_b = b1;
	}
	void show() {
		cout << _a << "+" << _b << "i" << endl;
	}
private:
	int _a;
	int _b;
};
Complex& operator+(Complex& c1, Complex& c2) {
	Complex c(c1._a + c2._a, c1._b + c2._b);
	return c;
}
void test01() {
	Complex c1(3, 5);
	Complex c2(7, 4);
	Complex c3 = c1 + c2;
	c2.show();
	c1.show();
	c3.show();
}
int main() {
	test01();
	system("pause");
	return 0;
}

36.有一个 Time 类,包含数据成员 minute(分)和 sec(秒),模拟秒表,每次走一秒,满 60 秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。(提示:重载单目运算符++)
#include <iostream>
#include <ctime>
using namespace std;
/*
有一个 Time 类,包含数据成员 minute(分)和 sec(秒)
模拟秒表,每次走一秒,满 60 秒进一分钟,此时秒又从0开始算。
要求输出分和秒的值。(提示:重载单目运算符++)
*/
class Time {
public:
	Time(int min,int sce) {
		_min = min;
		_sec = sce;
	}
	void showt() {
		cout << _min << " minute\t" << _sec <<" second" << endl;
	}
	Time& operator++() {//前置
		_sec++;
		if (_sec == 60) {
			_min++;
			_sec = 0;
		}
		return *this;
	}
	Time operator++(int) {//后置
		Time t(this->_min, this->_sec);
		_sec++;
		if (_sec == 60) {
			_min++;
			_sec = 0;
		}
		return t;
	}
private:
	int _min;
	int _sec;
};

void test01() {
	Time t1(3, 59);
	t1.showt();
	(t1++).showt();
	t1.showt();
	(++t1).showt();
	t1.showt();
}
int main() {
	test01();
	system("pause");
	return 0;
}

37.设计一个三角形类 Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。
#include <iostream>
using namespace std;
/*
设计一个三角形类 Triangle,包含三角形三条边长的私有数据成员
另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。
*/
class Triangle {
public:
	Triangle() {
		_a = _b = _c = 0;
	}
	bool ist(int a, int b, int c) {
		return a < (b + c) && b < (a + c) && c < (a + b);
	}
	Triangle(int a, int b, int c) {
		if (ist(a,b,c)) {
			_a = a;_b = b; _c = c;
		}
		else {
			cout << "no no no" << endl;
		}
	}
	void showt() {
		cout << this->_a << " " << this->_b << " " << this->_c << endl;
	}
	double ss() {
		double d = (_a + _b + _c) / 2;
		double s = sqrt(d*(d - _a)*(d - _b)*(d - _c));
		return s;
	}
	double operator+(Triangle &b) {
		return this->ss() + b.ss();
	}
private:
	int _a;
	int _c;
	int _b;
};

void test01() {
	Triangle t1(60,57,60);
	Triangle t2(3, 4, 5);
	t1.showt();
	t2.showt();
	cout << "t1:s:" << t1.ss() << endl;
	cout << "t2:s:" << t2.ss() << endl;
	cout << "t1+t2:s:" << t1+t2 << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}

38.使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。
#include<iostream>
#include<string>
using namespace std;
/*
使用函数重载的方法定义两个重名函数,分别求出整型数的两数之和和浮点数的两数之和,并在主函数中调用。
*/
void plus01(int a,int b=10) {
	cout << a + b << endl;
}
void plus01(double a, int b=10) {
	cout << a + b << endl;
}
int main() {
	int a = 8; 
	plus01(a);
	double i = 90.998;
	plus01(i);
	return 0;
}

39.定义一个抽象类 Shape 用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。程序中通过基类指针来调用派生类中的虚函数,计算不同形状的面积。
#include<iostream>
#include<string>
using namespace std;
#define PAI 3.14159
/*
39.定义一个抽象类 Shape 用以计算面积,从中派生出计算长方形、梯形、圆形面积的派生类。
程序中通过基类指针来调用派生类中的虚函数,计算不同形状的面积。
*/
class shape {
public:
	virtual void getshape() {};
};
class rectangle :public shape {
public:
	rectangle(int l,int h) {
		this->_l = l; this->_h = h;
	}
	void getshape() {
		cout << "rectangle shape :" << endl;
		int shape = this->_l * this->_h;
		cout <<shape<< endl;
	}
public:
	int _l;
	int _h;
};
class trapezoid :public shape {
public:
	trapezoid(int ul,int dl,int uh) {
		this->_upl = ul; this->_downl = dl; this->_uph = uh;
	}
	void getshape() {
		cout << "trapezoid shape :" << endl;
		double shape = (this->_upl / 2.0 + this->_downl / 2.0) * this->_uph;
		cout << shape << endl;
	}
public:
	int _upl;
	int _downl;
	int _uph;
};
class circle :public shape {
public:
	circle(int r) {
		this->_r = r;
	}
	void getshape() {
		cout << "circle shape :" << endl;
		double shape = this->_r * this->_r * PAI;
		cout << shape << endl;
	}
public:
	int _r;
};
void test01() {
	rectangle t(5, 6);
	t.getshape();
	trapezoid tr(3, 5, 6);
	tr.getshape();
	circle c(5);
	c.getshape();
}
int main() {
	test01();
	return 0;
}

​​​​​​​

40.定义计数器类 Counter。要求具有以下成员:计数器值;可进行增值和减值记数;可提供记数值。
#include<iostream>
#include<string>
using namespace std;
#define PAI 3.14159
/*
40.定义计数器类 Counter。要求具有以下
成员:计数器值;
可进行增值和减值记数;可提供记数值。
*/
class Counter {
public:
	Counter(int a) {
		this->_count = a;
	}
	Counter& operator++() {//前置
		this->_count++;
		return *this;
	}
	Counter operator++(int) {//hou_zhi
		Counter p = *this;
		this->_count++;
		return p;
	}
	void getcount(int a) {
		cout << "begin count = " << this->_count << endl;
		system("pause");
		for (int i =0; i <= a; i++) {
			_count++;
			cout << this->_count << " ";
			system("cls");
		}
		cout << endl << "after count = " <<this->_count<< endl;
	}
public:
	int _count;
};
void test01() {
	Counter c(0);
	c.getcount(67);
	cout << (++c)._count << endl;
	cout << (c++)._count << endl;
	cout << (c)._count << endl;
}
int main() {
	test01();
	return 0;
}

41.声明一个哺乳动物 Mammal 类,再由此派生出狗 Dog类,二者都定义 speak(成员函数,基类中定义为虚函数。声明一个 Dog类的对象,调用 speak()函数,观察运行结果。
#include<iostream>
#include<string>
using namespace std;
#define PAI 3.14159
/*
41.声明一个哺乳动物 Mammal 类,再由此派生出狗 Dog类,二者都定义 speak(成员函数,
基类中定义为虚函数。声明一个 Dog类的对象,调用 speak()函数,观察运行结果。
*/
class Mammal {
public:
	virtual void speak() {};
};
class dog :public Mammal {
public:
	void speak() {
		cout << " wang wang wang " << endl;
	}
};
void test01() {
	dog facai;
	facai.speak();
}
int main() {
	test01();
	return 0;
}
42.编写一个程序计算“三角形、正方形、圆形”三种图形的面积,要求  :a)抽象出一个基类 Base;  b)在其中说明一个虚函数用来求面积;   c)利用派生类定义“三角形、正方形、圆形”;  编写主函数并测试。
#include<iostream>
#include<string>
using namespace std;
#define PAI 3.14159
/*
42.编写一个程序计算“三角形、正方形、圆形”三种图形的面积,要求  :
a)抽象出一个基类 Base;  
b)在其中说明一个虚函数用来求面积;  
c)利用派生类定义“三角形、正方形、圆形”;  编写主函数并测试。
*/
class shape {
public:
	virtual void getshape() {};
};
class square :public shape {
public:
	square(int l) {
		this->_l = l; 
	}
	void getshape() {
		cout << "square shape :" << endl;
		int shape = this->_l * this->_l;
		cout << shape << endl;
	}
public:
	int _l;
};
class triangle :public shape {
public:
	triangle(int ul, int dl, int uh) {
		this->_a = ul; this->_b = dl; this->_c = uh;
	}
	void getshape() {
		cout << "triangle shape :" << endl;
		double s = (_a + _b + _c) / 2.0;
		double shape = sqrt(s*(s-_a)*(s-_b)*(s-_c));
		cout << shape << endl;
	}
public:
	int _a;
	int _b;
	int _c;
};
class circle :public shape {
public:
	circle(int r) {
		this->_r = r;
	}
	void getshape() {
		cout << "circle shape :" << endl;
		double shape = this->_r * this->_r * PAI;
		cout << shape << endl;
	}
public:
	int _r;
};
void test01() {
	square t(5);
	t.getshape();
	triangle tr(4, 5, 3);
	tr.getshape();
	circle c(5);
	c.getshape();
}
int main() {
	test01();
	return 0;
}

C++字符串(std::string)、STL(标准模板库)相关;(这一部分的作业,请参考 C++ Primer5 的练习)

43.编写一段程序,从标准输入中一次读入一整行,存入 std::string 中,然后修改该程序,使其一次读入一个词。(练习3.2)
#include<iostream>
#include<string>
using namespace std;
#define PAI 3.14159
/*
43.编写一段程序,从标准输入中一次读入一整行,存入 std::string 中,
然后修改该程序,使其一次读入一个词。(练习3.2)
*/

void test01() {
	cout << " one word once :___" << endl;
	string c;
	cin >> c;
	cout << c << endl;//string读到空格停止读入
}
void test02() {
	cout << " one line once:___" << endl;
	string line;
	while (getline(cin, line)) {//getline(is,s)从is中读取一行赋给S[包括空格],返回is
		cout << line << endl;
	}
}
int main() {
	test01();
	test02();
	return 0;
}
44.请说明std::string类的输入运算符和 getline函数分别是如何处理空白字符的。(练习3.3)

输入运算符 :逐字读取,行编辑,只保留第一串不含空白字符的连续字符;
getline :逐字读取,行编辑,全保留/换行符不存贮到字符串中

#include<iostream>
#include<string>
using namespace std;
#define PAI 3.14159
/*
44.请说明std::string类的输入运算符和 
getline函数分别是如何处理空白字符的。(练习3.3)
*/

void test01() {
	cout << " one word once :___" << endl;
	string c;
	cin >> c;
	cout << c << endl;
}
void test02() {
	cout << " one line once:___" << endl;
	string line;
	while (getline(cin, line)) {
		cout << line << endl;
	}
}
int main() {
	test01();
	test02();
	return 0;
}

45.编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分隔开来。(练习 3.5)
#include<iostream>
#include<string>
using namespace std;
#define PAI 3.14159
/*
45.编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串。
然后修改上述程序,用空格把输入的多个字符串分隔开来。(练习 3.5)
*/

void test01() {
	string word;
	while (cin>>word) {
		cout << word;
	}
}
void test02() {
	string word;
	while (cin >> word) {
		cout << word<<" ";
	}
}
int main() {
	//test01();
	test02();
	return 0;
}

46.编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。(练习 3. 10)
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
/*
46.编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。(练习 3. 10)
*/

void test02() {
	string line;
	while (getline(cin, line)) {
		for (auto c:line) {
			if (!ispunct(c)) {
				cout << c;
			}
		}
	}
}
int main() {
	test02();
	return 0;
}
47.编写一段程序,创建一个含有 10 个整数的 vector 对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出 vector 对象的内容,检验程序是否正确。(练习 3.23)
#include<iostream>
#include<string>
#include<vector>
using namespace std;
/*
47.编写一段程序,创建一个含有 10 个整数的 vector 对象,
然后使用迭代器将所有元素的值都变成原来的两倍。
输出 vector 对象的内容,检验程序是否正确。(练习 3.23)
*/
void print01(vector<int> &v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		//*it *= 2;
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i);
	print01(v);
	for (int i = 0; i < 10; i++)
		v[i]*=2;
	print01(v);
}
int main() {
	test02();
	return 0;
}
48.编写一段程序,比较两个 std::string 对象。再编写一段程序,比较两个C风格字符串的内容。(练习3.39)
#include<iostream>
#include<string>
#include<vector>
using namespace std;
/*
48.编写一段程序,比较两个 std::string 对象。再编写一段程序,比较两个C风格字符串的内容。(练习3.39)
*/
void bigorsmall(string s1,string s2) {
	if (s1.compare(s2) == 0) {
		cout << s1<<" == "<<s2 << endl;
	}
	else if (s1.compare(s2) < 0) {
		cout << s1 << " > " << s2 << endl;
	}
	else if (s1.compare(s2) > 0) {
		cout << s1 << " < " << s2 << endl;
	}
}
void test02() {
	string s1 = "dsewfaewg";
	string s2 = "dsewfaewg";
	bigorsmall(s1, s2);
	const char* v1 = "nihao";
	const char* v2 = "aihao";
	bigorsmall(v1, v2);
}
int main() {
	test02();
	return 0;
}

49.对于下面的程序任务,vector、deque 和 list 哪种容器最为合适?解释你的选择的理由。如果没有哪一种容器优于其他容器,也请解释理由。(练习9.1)
(1)读取固定数量的单词,将他们按字典序插入到容器中。
(2)读取未知数量的单词,总是将新单词插入到末尾。删除操作在头部进行。
(3)从一个文件读取未知数量的整数。将这些数排序,然后将他们打印到标准输出。
/*
(1)LIST可以对任意位置进行快速插入或删除元素,操作方便,修改指针即可,不需要移动大量元素,LIST合适,
也可以每次尾部插入之后用排序算法重新排序,VECTOR合适
(2)固定头部删除,尾部插入,DEQUE更合适
(3)排序需要遍历元素,VECTOR访问元素的速度快于DEQUE,LIST,VECTOR合适
*/
50.对6种创建和初始化 vector 对象的方法,每一种都给出一个实例。解释每个 vector 包含什么值。(练习9.11)
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
using namespace std;
/*
50.对6种创建和初始化 vector 对象的方法,每一种都给出一个实例。解释每个 vector 包含什么值。(练习9.11)
*/
void myprint(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	vector<int> v1;     //vector<T> v; 
	for (int i = 0; i < 5; i++) {
		v1.push_back(i);
	}
	vector<int> v2(v1.begin(), v1.end());   //vector(v.begin(),v.end());
	vector<int> v3(15,1);   //vector(n,elem); 
	vector<int> v4(v3);   //vector(const vector &vec); 
	vector<int> v5;   //operator= 
	v5 = v1;
	vector<int> v6;   //assign(n,elem)
	v6.assign(20, 0);   
	vector<int> v7;  //assign(beg,end)
	v7.assign(v3.begin(), v3.end());
	myprint(v1);
	myprint(v2);
	myprint(v3);
	myprint(v4);
	myprint(v5);
	myprint(v6);
	myprint(v7);
}
int main() {
	test02();
	return 0;
}

51.如何从一个 list<int>初始化一个 vector<double>?从一个 vector<int>又该如何创建?编写代码验证你的答案。(练习9.13)
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
using namespace std;
/*
51.如何从一个 list<int>初始化一个 vector<double>?
从一个 vector<int>又该如何创建?编写代码验证你的答案。(练习9.13)
*/
void myprint(vector<double>& v) {
	for (vector<double>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	list<int>l(5, 6);
	vector<double>v0(l.begin(), l.end());
	vector<int> v1(10,1);
	vector<double>v2(v1.begin(),v1.end());
	myprint(v0);
	myprint(v2);
}
int main() {
	test02();
	return 0;
}

52.编程程序,将一个 1ist 中的 char*指针(指向C风格字符串)元素赋值给-个vector中的string。(练习9.14) 
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
using namespace std;
/*
52.编程程序,将一个 1ist 中的 char*指针(指向C风格字符串)元素
赋值给-个vector中的string。(练习9.14) 
*/
void myprint(vector<string>& v) {
	for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	list<const char*>l = { "cc","ii","ee"};
	vector<string>v0(l.begin(), l.end());
	myprint(v0);
}
int main() {
	test02();
	return 0;
}
​​​​​​​
53,编写程序,从标准输入读取 string 序列,存入一个 deque 中。编写一个循环,用迭代器打印 deque 中的元素。(练习 9.18)
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
using namespace std;
/*
53,编写程序,从标准输入读取 string 序列,存入一个 deque 中。
编写一个循环,用迭代器打印 deque 中的元素。(练习 9.18)
*/
void myprint(deque<string>& v) {
	for (deque<string>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	deque<string>v0;
	string word;
	while (cin >> word) {
		v0.push_back(word);
	}
	myprint(v0);
}
int main() {
	test02();
	return 0;
}
54.编写程序,从一个 1ist<int>拷贝元素到两个 deque 中,其中值为偶数的所有元素都拷贝到一个 deque 中,而奇数元素都拷贝到另一个 deque 中。(9.20)
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
using namespace std;
/*
54.编写程序,从一个 1ist<int>拷贝元素到两个 deque 中,其中值为偶数的所有元素
都拷贝到一个 deque 中,而奇数元素都拷贝到另一个 deque 中。(9.20)
*/
void myprint(deque<int>& v) {
	for (deque<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	deque<int> odd_number;
	deque<int> even_number;
	list<int> l;
	for (int i = 0; i < 101; i++) {
		l.push_back(i);
	}
	for (auto it = l.begin(); it != l.end(); it++) {
		if (*it % 2 == 0) {
			even_number.push_back(*it);
		}
		else {
			odd_number.push_back(*it);
		}
	}
	cout << " odd_number" << endl;
	myprint(odd_number);
	cout << " even_number" << endl;
	myprint(even_number);
}
int main() {
	test02();
	return 0;
}
55,假定你希望每次读取一个字符存入一个 std::string 中,而且知道最少需要读取 100个字符,应该如何提高程序的性能?(9.42)
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
using namespace std;
/*
55,假定你希望每次读取一个字符存入一个 std::string 中,而且知道最少需要
读取 100个字符,应该如何提高程序的性能?(9.42)
1.使用 reserve:在读取字符前,调用 reserve 函数来预留足够的内存空间,以减少动态内存分配的次数
2.使用移动语义:当将读取的字符添加到 std::string 中时,使用移动语义而不是复制语义,可以避免不必要的内存复制。
*/

void test02() {
	string str;
	str.reserve(100);//提前开辟空间
	cout << str.capacity() << endl;
	char c;
	/*while ((c = getchar()) != EOF) {
		str.push_back(c);
	}*/
	while (cin.get(c)) {
		str.push_back(move(c));//使用移位字符
	}
	cout << str << endl;
}
int main() {
	test02();
	return 0;
}
56.编写一个函数,接受一个表示名字的 std::string 参数和两个分别表示前缀(如“Mr.”或“Ms.”)和后缀(如“Jr.”“III”)的字符串。使用迭代器及insert 和 append 函数将前缀和后缀加到给定的名字中,生成新的 string 并返回。(9.45)
#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<list>
using namespace std;
/*
56.编写一个函数,接受一个表示名字的 std::string 参数和两个分别表示前缀(如“Mr.”或“Ms.”)和后缀(如“Jr.”“III”)的字符串。
使用迭代器及insert 和 append 函数将前缀和后缀加到给定的名字中,生成新的 string 并返回。(9.45)
*/

void test02() {
	string str="魏国";
	string prefix = "Mr.";
	string prefix2 = "Ms.";
	string suffix = "Jr.III";
	cout << str << endl;
	str.append(suffix,0,3);
	cout << str << endl;
	str.append(suffix, 3, 3);
	cout << str << endl;
	str.insert(0, prefix);
	cout << str << endl;
	str.insert(0, prefix2);
	cout << str << endl;
}
int main() {
	test02();
	return 0;
}
57.定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们)的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。(11.7)
0.1  bug,只有第一个添加人的函数能进入循环
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
/*
57.定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们)的名。
编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。(11.7)
*/
void addchild(map<string, vector<string>>&fam) {
	string ff, child;
	cout << " addchild : enter family,child" << endl;
	while(cin>>ff&&cin>>child){
		fam[ff].push_back(child);
	}
}
void addfamily(map<string, vector<string>> &fam) {
	cout << " addfamily:enter family" << endl;
	string ff;
	while (cin >> ff) {
		fam[ff];
	}
}
void show(map<string,vector<string>> &fam) {
	cout << "show family and children" << endl;
	for (auto a : fam) {
		cout << a.first << ":\t" << endl;
		for (auto b : a.second) {
			cout << b << " ";
		}
		cout << endl;
	}
}
void test02() {
	map<string, vector<string>> fam;
	addchild(fam);
	addfamily(fam);//bug,只有第一个添加人的函数能进入循环
	show(fam);
}
int main() {
	test02();
	return 0;
}
0.2 正常的
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
/*
57.定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们)的名。
编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。(11.7)
*/
void addchild(map<string, vector<string>>&fam, const string &ff, const string &child) {
		fam[ff].push_back(child);
}
void addfamily(map<string, vector<string>> &fam,const string &ff) {
	fam[ff];
}
void show(map<string,vector<string>> &fam) {
	cout << "show family and children" << endl;
	for (auto a : fam) {
		cout << a.first << ":\t" ;
		for (auto b : a.second) {
			cout << b << " ";
		}
		cout << endl;
	}
	cout << endl;
}
void test02() {
	map<string, vector<string>> fam;
	addchild(fam,"huang","dsd");
	addchild(fam, "huang", "34523");
	addchild(fam, "chen", "dddq");
	addfamily(fam,"xu");
	addfamily(fam, "zhuge");
	show(fam);
}
int main() {
	test02();
	return 0;
}

58,编写一个程序,在一个 vector 而不是一个 set 中保存不重复的单词。使用set的优点是什么?(练习 11.8)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
/*
58,编写一个程序,在一个 vector 而不是一个 set 中保存不重复的单词。使用set的优点是什么?(练习 11.8)
SET	会自动进行排序,且不允许重复元素出现
*/
void addVector(vector<string> &v,string a) {
	vector<string>::iterator isrepetition=find(v.begin(), v.end(), a);
	if (isrepetition == v.end()) {
		v.push_back(a);
	}
}
void show(vector<string>& v) {
	for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	vector<string> v;
	addVector(v, "hkhk");
	addVector(v, "23");
	addVector(v, "hkhk");
	show(v);
}
int main() {
	test02();
	return 0;
}
​​​​​​​
59.可以用什么类型来对一个 map 进行下标操作?下标运算符返回的类型时什么?请给出一个具体例子,即定义一个 map,然后写出一个可以用来对 map 进行下标操作的类型以及下标运算符将会返回的类型。(11.26)
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
/*
当对MAP进行下标操作时,会得到一个MAPPED_TYPE对象,解引用一个迭代器时,会得到一个VALUE_TYPE对象
STRING 和 VECTOR使用小标和解引用返回的数据类型相同
*/
void show(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	map<int, string> m;
	m.insert(make_pair(2, "dsfe"));
	cout <<" m[2]:\t" << m[2] << endl;
	map<int, string>::iterator it = m.begin();
	cout <<"*it:\t" << (*it).first << " " << (*it).second << endl;
}
int main() {
	test02();
	return 0;
}
60.用冒泡法对 10 个整数排序。(用 STL 的 vector 容器实现)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
/*
60.用冒泡法对 10 个整数排序。(用 STL 的 vector 容器实现)
*/
void bubble(vector<int> &v) {
	for (int t= 0; t < v.size()-1; t++) {
		for (int i = 0; i < v.size() - 1 - t; i++) {
			if (v[i] > v[i + 1]) {
				v[i] += v[i + 1];
				v[i + 1] = v[i] - v[i + 1];
				v[i]= v[i] - v[i + 1];
			}
		}
	}
}
void show(vector<int>& v) {
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
void test02() {
	srand(unsigned int(time(NULL)));
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		int t = rand() % 100;
		v.push_back(t);
	}
	show(v);
	bubble(v);
	show(v);
}
int main() {
	test02();
	return 0;
}
  • 48
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值