C++笔记--泛型

一.第一题 

 

/*读取文件中的字符串,并把非字典的数据剔除,然后输出*/
#include<iostream>
#include<fstream>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;

int main()
{

	ifstream infile;
	string t1;
	string t2[4] = {"but","and","or","yes"};
	vector<string> swqp = {t2,t2+4};
	set<string> swap2 = {swqp.begin(),swqp.end()};
	map<string, int>word;

	infile.open("G:\\soft-package\\C++\\test\\test.txt");
	if (!infile.is_open())
	{
		cout << "open file failure" << endl;
	}
	else
	{
		while (!infile.eof())
		{
			infile >> t1;
			if (swap2.count(t1))
			{
				continue;
			}
			else
			{
				word[ t1 ] ++;
			}
			
		}
		infile.close();
	}
	cout << "This program will output" << endl;
	map<string, int> ::iterator i;
	for ( i = word.begin(); i != word.end(); i++)
	{
		cout << "this word is " << i->first << " arise " << i->second << endl;
	}

	return 0;
}


out:
This program will output
this word is asd arise 1
this word is asda arise 2
this word is eqweq arise 1
this word is nonononono arise 2
this word is we arise 1
this word is wq arise 1
this word is wqe arise 1
this word is wqeqw arise 2
this word is wqreqw arise 1

二.第二题

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

int main()
{
	map<string, string>data = {{ "yi","liu" }, {"er","hu"} ,{"san","liu"},{"si","liu"},{"wu","wang"},{"liu","wang"},{"zha","wang"}};
	string data1;
	string data2;
	map<string, string> ::iterator it;
	map<string, string>data3;

	cout << "please input surname" << endl;
	while (cin >> data1)
	{
		for(it=data.begin();it!=data.end();it++)
		{
			if (it->second == data1)
			{
				data3.insert(std::make_pair(it->first,it->second));
			}
		}
		cout << "姓名册中含有的该姓的孩子有" << endl;
		for (it = data3.begin(); it != data3.end(); it++)
		{
			
			cout << "his name is " << it->second << " " << it->first << endl;
		}
		data3.clear();
	}

	

}
output:
please input surname
liu
姓名册中含有的该姓的孩子有
his name is liu san
his name is liu si
his name is liu yi
wang
姓名册中含有的该姓的孩子有
his name is wang liu
his name is wang wu
his name is wang zha
hu
姓名册中含有的该姓的孩子有
his name is hu er

泛型解决了一个什么样的问题。在开始的时候,我以为泛型就是对各种数据类型的操作,把同种操作都归置为函数。但是这样的描述是不够准确的。

应该说操作系统->C语音的环境->抽象机制->泛型。为什么会有一层抽象机制,在STL中,list是存在两个指针的,vector却只有一个指针,那么这些指针怎么会根据begin()和end()来识别泛型的结束和末尾,所以啊,它存在一个抽象机制,将这个问题集成化了。

关于,泛型我有几个小细节和大家分享。

map的插入函数并不能插入所有的对象。

例如

map<string,string>data={{"wang","yi"},{"wang","er"},{"wang","san"}};

在这个map对象的初始化中,我们最终存储的数据只有一个,就是{“wang”,"yi"}。为什么会没有其他呢。map实际上<key,value>,

它只有一个key,如果出现了两个key,它不会覆盖,只会识别到,然后就跳过,我们可以看insert的源码

template <class _Valty, enable_if_t<is_constructible_v<value_type, _Valty>, int> = 0>
    pair<iterator, bool> insert(_Valty&& _Val) {
        return this->emplace(_STD forward<_Valty>(_Val));
    }

对了还有,map赋值的时候使用pair,它的源码定义是这样的 

template <class T1, class T2>
  pair<V1,V2> make_pair (T1&& x, T2&& y);  // see below for definition of V1 and V2

它是将两个类型(可以相同也可以不同)的数据进行整合赋值操作。 

未完待续,继续加油

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值