【王道训练营 C/C++方向基础 60 题(51-60)】

51. 如何从一个 list<int>初始化一个 vector<double>?从一个 vector<int>又
该如何创建?编写代码验证你的答案。

#include<iostream>
#include<vector>
#include<list>
using namespace std;

void printVector(vector<double> &v)
{
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{
	//采用迭代器范围初始化

	list<int> L{ 1,2,3,4,5 };
	vector<double> V1(L.begin(), L.end());
	printVector(V1);

	vector<int> V2{ 6,7,8,9,10 };
	vector<double> V3(V2.begin(), V2.end());
	printVector(V3);
	return 0;
}

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

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

void print(vector<string>& v)
{
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{
	list<const char *> L={ "I"," LOVE ","YOU" };
	vector<string> V;
	V.assign(L.begin(), L.end());
	cout << "capacity:" << V.capacity() << " size:" << V.size() << endl;
	print(V);
	return 0;
}

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

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

void print(deque<string>& d)
{
	for (auto it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{
	string s;
	deque<string> d;
	while (cin >> s)
	{
		d.push_back(s);
	}
	print(d);
}

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

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

void print(deque<int>& d)
{
	for (auto it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{
	list<int> L{ 10,2,3,9,5,6,7,8,4,1 };
	deque<int> d1, d2;
	for (auto it = L.begin(); it != L.end(); it++)
	{
		if (*it % 2 == 0)
		{
			d1.push_back(*it);
		}
		else
		{
			d2.push_back(*it);
		}
	}
	print(d1);
	print(d2);
}

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

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

int main()
{
	string s;
	char c;
	s.reserve(100);
	while (cin >> c)
	{
		s += c;
	}
	cout << s << endl;
}

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

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

//string &insert(int p0, const char *s);
//1、在p0位置插入字符串s
//string &insert(int p0, const char *s, int n);
//2、在p0位置插入字符串s的前n个字符
//string &insert(int p0,const string &s);
//3、在p0位置插入字符串s
//string &insert(int p0,const string &s, int pos, int n);
//4、在p0位置插入字符串s从pos开始的连续n个字符
//string &insert(int p0, int n, char c);
//5、在p0处插入n个字符c
//iterator insert(iterator it, char c);
//6、在it处插入字符c,返回插入后迭代器的位置
//void insert(iterator it, const_iterator first, const_iteratorlast);
//7、在it处插入从first开始至last-1的所有字符
//void insert(iterator it, int n, char c);
//8、在it处插入n个字符c

int main()
{
	//前缀,后缀
	string prefix="Ms.", suffix="III";
	string s="Right";
	s.insert(s.begin(), prefix.begin(), prefix.end());
	s.append(suffix.begin(), suffix.end());
	cout << s << endl;

}

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

#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;

//添加家庭的姓
void add_family(map<string, vector<string>>& families, const string& family)
{
	families[family];
}

void add_child(map<string, vector<string>>& families, const string& family, const string& child)
{
	families[family].push_back(child);
}

int main()
{
	map<string, vector<string>> families;
	add_family(families, "Li");
	add_child(families, "Li", "Si");
	add_child(families, "Li", "Wu");
	add_family(families, "Zhang");
	add_child(families, "Zhang", "san");
	add_family(families, "Wang");
	for (auto a : families)
	{
		cout << a.first << " family: ";
		for (auto b : a.second)
		{
			cout << b << " ";
		}
		cout << endl;
	}

}

//map<int, string> m;
//map插入方式:
//m.insert(pair<int, string>(1, "student"));
//m.insert(map<int, string>::value_type(1, "student"));
//m.insert(make_pair(1, "student"));
//m[1] = "student";
//m.insert({ 1, "student });

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

#include<iostream>
#include<algorithm>
#include<fstream>
#include<string>
#include<vector>
#include<set>
using namespace std;

/*
vector保存不重复的单词,需要用find查找新单词是否已在vector中,
而set会自动检测。vector可以保持输入的单词顺序,
而set在输入时,会自动排序
*/

string& trans(string& s)
{
	for (int p = 0; p < s.size(); p++)
	{
		if (s[p] >= 'A' && s[p] <= 'Z')
		{
			s[p] -= ('A' - 'a');
		}
		else if (s[p] == ',' || s[p] == '.')
		{
			s.erase(p, 1);
		}
	}
	return s;
}

//set
//int main(int argc, char* argv[])
//{
//	ifstream in(argv[1]);
//	if (!in)
//	{
//		cout << "打开输入文件失败" << endl;
//		exit(1);
//	}
//	set<string> unique_word;
//	string word;
//	while (in >> word)
//	{
//		trans(word);
//		if (find(unique_word.begin(), unique_word.end(), word) == unique_word.end())
//		{
//			unique_word.insert(word);
//		}
//	}
//	for (auto& w : unique_word)
//	{
//		cout << w << " ";
//	}
//	cout << endl;
//	return 0;
//}

//vector
int main(int argc, char* argv[])
{
	ifstream in(argv[1]);
	if (!in)
	{
		cout << "打开输入文件失败" << endl;
		exit(1);
	}
	vector<string> unique_word;
	string word;
	while (in >> word)
	{
		trans(word);
		if (find(unique_word.begin(), unique_word.end(), word )== unique_word.end())
		{
			unique_word.push_back(word);
		}
	}
	for (auto& w : unique_word)
	{
		cout << w << " ";
	}
	cout << endl;
	return 0;
}

 

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

/*
对map进行下标操作,应使用其key_type,即关键字的类型
而下标操作返回的类型是mapped_type,即关键字关联的值的类型
例如:
map<string,int>
下标操作的类型:string
下标运算符将会返回的类型:int
*/

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

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

void printVector(vector<int>& v)
{
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void bubbleSort(vector<int>& v)
{
	for (auto i = v.begin(), j = v.end(); i < j; j--)
	{
		for (auto k = i + 1; k < j; k++)
		{
			if (*k < *(k - 1)) swap(*k, *(k - 1));
		}
	}
}

int main()
{
	vector<int> v = { 10,6,3,9,5,2,7,8,4,1 };
	cout << "排序前:" << endl;
	printVector(v);
	bubbleSort(v);
	cout << "排序后:" << endl;
	printVector(v);
	return 0;
}

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
王道训练生产者消费者问是一个经典的同步问,其中包含一个生产者和一个消费者,它们共享一个有限的缓冲区(队列),生产者向缓冲区中放入数据,而消费者则从缓冲区中取出数据。采用基于对象的方式可以简化问的处理和封装。 在基于对象的实现中,我们可以创建两个对象:生产者对象和消费者对象。这两个对象都可以访问共享的任务队列对象,而任务队列对象则实现对缓冲区的访问和操作。 首先,我们需要定义任务队列对象,它可以有一个固定大小的缓冲区和一些基本的操作方法,如向队列中添加任务和从队列中取出任务。 接下来,我们创建生产者对象和消费者对象。生产者对象通过调用任务队列对象的添加任务方法,将任务加入队列。而消费者对象则通过调用任务队列对象的取出任务方法,从队列中取出任务。为了使生产者和消费者能够同时运行,我们可以使用多线程或多进程的方式。 在生产者和消费者的运行过程中,需要注意的是对任务队列的互斥访问,避免生产者在队列已满时继续添加任务,或者消费者在队列已空时继续取出任务。可以采用互斥锁或信号量来实现对任务队列的互斥访问,确保生产者和消费者的安全执行。 通过以上的设计和实现,我们可以实现一个使用基于对象的方式解决生产者消费者问王道训练。这种方式可以帮助我们更好地封装和管理任务队列,使生产者和消费者的交互更加简单和高效。同时,这种基于对象的设计也符合面向对象的思想,提高了代码的可维护性和可扩展性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值