STL练习

目标:实现一个通用的容器,能够支持插入多种不同的普通类型(包含 int char float double 等)和自定义结构体和自定义类的对象,并能根据每种不同类型的比较规则从容器中取得最大或最小的那个值或对象

方案:使用自定义的模板,默认使用set容器,也可指定为其他容器

[代码] myContainer.hpp

#ifndef _MYCONTAINER_
#define _MYCONTAINER_
#include <set>
#include <iostream>

template <class _Ty,
	class _Container = set<_Ty>>
	class MyContainer {
	public:
		typedef MyContainer<_Ty, _Container> _MyCon;
		typedef typename _Container::size_type size_type;
		typedef typename _Container::value_type value_type;

		MyContainer() :container() {
			//无参构造函数,初始化内置容器
		}

		MyContainer(const _MyCon &right) :container(right.container) {
			//拷贝构造函数,用另一个对象的内置容器赋给当前对象的内置容器
		}

		_MyCon &operator= (const _MyCon &other) {
			container = other.container;
			return *this;
		}

		size_type size() {
			return container.size();
		}

		bool empty() {
			return container.empty();
		}

		bool insert(const value_type &_Val) {
			typename _Container::iterator it = container.insert(_Val);
			if (it != container.end()) {
				std::cout << "插入成功" << _Val << std::endl;
				return true;
			}

			std::cout << "插入失败" << _Val << std::endl;
			return false;
		}

		bool erase(const value_type &_Val) {
			if (container.erase(_Val) > 0) {
				return true;
			}
			return false;
		}

		std::pair<value_type, bool> getMin() {
			std::pair<value_type, bool> ret;
			if (container.begin() != container.end()) {
				ret.second = true;
				ret.first = *(container.begin());
				return ret;
			}
			ret.second = false;
			return ret;
		}

		std::pair<value_type, bool> getMax() {
			std::pair<value_type, bool> ret;
			if (container.begin() != container.end()) {
				ret.second = true;
				ret.first = *(--container.end());
				return ret;
			}
			ret.second = false;
			return ret;
		}

	protected:
		_Container container;		//内置容器
};

#endif

[代码] main.cpp

#include <iostream>
#include "myContainer.hpp"
#include <set>
#include <map>

using namespace std;

int main(void) {

	//	MyContainer<int> mc;
	MyContainer<int, multiset<int>> mc;

	for (int i = 0; i < 5; i++) {
		mc.insert(i * 10);
	}

	mc.erase(0);
	mc.erase(40);

	cout << "最大的值:" << mc.getMax().first << endl;
	cout << "最小的值:" << mc.getMin().first << endl;

	if (!mc.empty()) {
		cout << "元素个数: " << mc.size() << endl;
	}

	return 0;
}
### C++ STL 练习题及答案 以下是基于引用内容设计的几道 C++ STL 练习题及其解答。 --- #### **练习题一:单词计数器** **题目描述**: 使用 `std::map` 实现一个简单的单词计数器程序。给定一段文字,统计其中每个单词出现的次数并按字母顺序输出结果。 **解决方案**: 为了实现此功能,可以利用 `std::map` 来存储单词与其对应的频率,并通过遍历输入文本完成统计工作[^1]。 ```cpp #include <iostream> #include <sstream> #include <map> #include <algorithm> int main() { std::string text; getline(std::cin, text); // 将所有字符转为小写以便统一处理 for(char& c : text){ c = tolower(c); } std::istringstream iss(text); std::string word; std::map<std::string, int> word_count; while(iss >> word){ ++word_count[word]; } // 输出结果 for(const auto& pair : word_count){ std::cout << pair.first << ": " << pair.second << "\n"; } return 0; } ``` --- #### **练习题二:字符串去重** **题目描述**: 利用 `std::set` 去除重复的字符串项。对于每一条指令,如果操作码为 `0`,表示插入新字符串;如果是 `1`,则查询当前是否存在指定字符串。 **解决方案**: 可以通过 `std::set` 自动去除重复元素的功能来简化逻辑[^2]。 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; set<string> st; for(int i = 0; i < n; ++i){ int opt; string word; cin >> opt >> word; if(opt == 0){ st.insert(word); }else{ if(st.find(word) != st.end()){ cout << "Yes\n"; }else{ cout << "No\n"; } } } return 0; } ``` --- #### **练习题三:消息队列管理** **题目描述**: 设计一个支持优先级的消息队列管理系统。当接收到 `"PUSH"` 指令时,需将带有特定优先级的消息存入队列;而接到 `"POP"` 指令时,则应弹出最高优先级的消息[^3]。 **解决方案**: 采用 `std::priority_queue` 结合自定义比较函数的方式能够高效解决此类问题。 ```cpp #include <iostream> #include <queue> #include <vector> #include <string> struct Message { std::string data; int priority; }; // 定义大顶堆 struct Compare { bool operator()(const Message& m1, const Message& m2) { if(m1.priority != m2.priority) return m1.priority < m2.priority; return true; // 若优先级相同,默认保持原序即可 } }; int main(){ std::priority_queue<Message, std::vector<Message>, Compare> pq; std::string command; while(getline(cin, command)){ if(command.substr(0,4) == "PUSH"){ size_t pos = command.find(' '); std::string str = command.substr(pos+1,command.rfind(' ')-pos-1); int pri = stoi(command.substr(command.rfind(' ')+1)); pq.push(Message{str,pri}); } else if(command == "POP"){ if(pq.empty()) std::cout << "NULL!\n"; else{ std::cout << pq.top().data << '\n'; pq.pop(); } } } return 0; } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值