c++-----------------------关联容器操作以及动态内存

本文深入探讨C++标准库中的关联容器应用,如map、multimap、set等,通过实例展示如何利用这些容器进行高效的数据管理和操作。文章涵盖关联容器的基本使用、高级特性及常见应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <memory>
#include <map>
#include <set>
#include "StrBlob.h"
#include "sale_data.h"
#include <utility>

using namespace std;

bool Not_in(vector<string> &H, string &word);
bool _compare_isbn(const StrBlob &ihs, const StrBlob &rhs); 

void main()
{
	//allocator<string> alloc;
	map<string, vector<int>> kkl = { {"das",{1,3,4}} };//初始化,注意花括号的个数
	kkl.insert(pair<string, vector<int>>("fds", { 1,3,4 }));
	kkl.insert(map<string, vector<int>>::value_type("fda", { 1,4,7 }));
	kkl.insert({ "ert",{1,3,4} });
	map<string, vector<int>>::iterator itro=kkl.find("ert");


	multimap<string, string> writer;
	writer.insert(pair<string, string>("张维", "学到死1"));
	writer.insert(pair<string, string>("张维", "学到死2"));
	writer.insert(pair<string, string>("张维", "学到死3"));
	writer.insert(pair<string, string>("张维", "学到死4"));
	//writer.erase(writer.find("张维"));   //若是迭代器,则删除之后,返回的还是迭代器,
	                                       //是指向下一个的迭代器,此例中是end()
	
	cout << writer.erase("张维");   //输出是4
	 
	
	
	//string * p = alloc.allocate(2);
	//auto q = p;   //q指向最后构造的元素之后的位置,因为p是未构造的,所以也就是p
	//alloc.construct(q,"hi");
	//while (q != p)
	//	alloc.destroy(--q);
	//alloc.deallocate(p, 2);
	////cout << *p << "das";
	//
	//string *kip= alloc.allocate(10);
	//string *io = kip;
	//string s;
	//while (cin >> s && io != kip + 10)
	//	alloc.construct(io++, s);
	//
	//while (io != kip)
	//	alloc.destroy(--io);   //因为io是在最后一个构造函数的后面
	//alloc.deallocate(kip, 10);

	/*----------------文件查询程序需要用到关联容器,我觉得这个很有用------------------*/
	 //开始关联容器的学习
	map<string, size_t> word_count;
	string word;
	//while (cin >> word)
	//	++word_count[word];  //使用数组的形式,将覆盖(修改)先前相同关键字的值
	//for (auto &c : word_count)
	//	cout << c.first << "    occurs   " << c.second
	//	<< ((c.second > 1) ? "    times" : "    time") << endl;

	//set可以保存那些想要忽略的单词
	set<string> exclude = { "the","but","and","or","an","a",
						 "The","But","And","Or","An","A" };
	vector<string> _exclude= { "the","but","and","or","an","a",
						 "The","But","And","Or","An","A" };

	
	while (cin >> word)
	{
		if(Not_in(_exclude, word))
		//if (exclude.find(word) == exclude.end())
			++word_count[word];    //当它两相等时,则说明它不在里面,否则就在里面
	}
	for (auto &c : word_count)
		cout << c.first << "    occurs   " << c.second
		<< ((c.second > 1) ? "    times" : "    time") << endl;
	//当你输入 this is a text this is the text
	//输出:
	//is    occurs   2    times
	//text    occurs   2    times
	//	this    occurs   2    times
	cin.clear(); 

	map<string, vector<string>> map_famliy;
	
	string file_name="C:\\Users\\信计捡球员\\Desktop\\project_text\\project_text\\file_name.txt";
	ifstream iss(file_name, fstream::in| fstream::out);

	string line;
	while (getline(iss, line))
	{
		istringstream kill(line);
		string name;
		vector<string> e_word;
		string word;
		kill >> name;
		while (kill >> word)
			e_word.push_back(word);
		map_famliy[name] = e_word;
	}

	string new_name;
	vector<string> new_famliy;
	cout << "please enter your new first name" << endl;
	while (cin >> new_name)
	{
		string new_word;
		cout << "please enter your names" << endl;
		while(cin>> new_word)
		new_famliy.push_back(new_word);
		map_famliy.insert(pair<string, vector<string>>(new_name, new_famliy));


	}
	cin.clear();   //不知道为什么cin的状态有问题;
	cout << "please enter existent first name " ;
	string F_name;
	cin >> F_name;
	cout << endl;
	string new_members;
	cout << "please enter new members ";
	cout << endl;
	while (std::cin >> new_members)
	{
		map_famliy[F_name].push_back(new_members);
	}
	
	

	for (map<string, vector<string>>::iterator iter = map_famliy.begin(); iter != map_famliy.end(); ++iter)
	{
		cout << iter->first<<"  ";
		for (auto &c : iter->second)
		{
			cout << c << ' ';
		}
		cout << endl;
	}
	iss.close();

	//输出
	/*  please enter your new first name
		钱
		please enter your names
		一 二 三
		^Z
		please enter existent first name 王

		please enter new members
		题 系
		^Z
		陈  一 二 三 四
		郭  一 二 三 四
		刘  一 二 三 四
		钱  一 二 三
		王  一 二 三 四 题 系
		张  一 二 三 四        */

	///multiset< StrBlob, decltype(_compare_isbn)*> bookstore(_compare_isbn);   
	//multiset< StrBlob, bool(*)(const StrBlob &ihs, const StrBlob &rhs)> bookstore(_compare_isbn);
	
	//multiset< StrBlob, bool(*)(const StrBlob &ihs, const StrBlob &rhs)>::iterator iter = bookstore.begin();

	vector<pair<string, int>> _pair;
	

	pair<string, int> o("sad", 0);
	pair<string, int> o1 = { "sad",0 };
	_pair.push_back(make_pair("das", 0));

	vector<pair<string, vector<string>>>  jo;
	for (auto &c : jo)
		map_famliy.insert(c);  //map中的元素就是pair

	map<string, vector<string>>::const_iterator  iter = map_famliy.cbegin();
	//map<string, vector<string>>::iterator  iter = map_famliy.begin();
	 

	system("pause");

}

bool Not_in(vector<string> &H,string &word)
{
	for (vector<string>::iterator iter = H.begin(); iter != H.end(); ++iter)
	{
		if (*iter == word) { return false; break; }

	}
	return true;
}



bool _compare_isbn(const StrBlob &ihs, const StrBlob &rhs)
{
	return ihs.isbn() < rhs.isbn();
}

2019-6-1

补充一下之前primer落下的小知识点:

/*  用来实现字符串与枚举类型的转换   */
std::map<string, Year> str2enum{
	make_pair("Mon",Mon),
	make_pair("Tue",Tue),
	make_pair("Wed",Wed),
	make_pair("Thu",Thu),
	make_pair("Fri",Fri),
	make_pair("Sat",Sat),
	make_pair("Sun",Sun),

};

/*  此结构体可以实现对于string 类型的日期的输入以及转换 

        like this  Jan 1,1900
                    1/1/1990
                    Jan 1 1900
  只不过我把Jan换成了星期了
            */
struct Date {
	Year mont;
	unsigned  ye;
	unsigned  date;
	unsigned int month;
	Date(string print_date)
	{
		if (isalpha(print_date[0]))
		{
			auto inde = print_date.find_first_of("0123456789");
			mont = str2enum[print_date.substr(0,inde-2)];
			string f= print_date.substr(inde);
			auto inde1=(print_date.substr(inde)).find_first_not_of("0123456789");
			date = stoul(print_date.substr(inde, inde+inde1-1));
			ye = stoul(print_date.substr(inde).substr(inde1+1));
		}
		else {
			auto inde2 = print_date.find_first_not_of("0123456789");
			month = stoi(print_date.substr(0,inde2));
			auto inde3 = print_date.substr(inde2 + 1).find_first_not_of("0123456789");
			date= stoul(print_date.substr(inde2+1, inde2+inde3));
			ye = stoul(print_date.substr(inde2 + inde3+2));
		}
	}
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值