简单的搜索方式(c++)(stl容器)(初版)

假设我们有以下食物在搜索数据库中
在这里插入图片描述
输入面可搜索出相关食物,如输入面,输出面条,面包
效果图
在这里插入图片描述
在这里插入图片描述
示例代码

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void Menu() //菜单
{
	cout << "################################################" << endl;
	cout << "################################################" << endl;
	cout << "################################################" << endl;
	cout << "################################################" << endl;
	cout << "################################################" << endl;
	cout << "#############*_*欢迎来到小彭搜索*_*#############" << endl;
	cout << "################################################" << endl;
	cout << "################################################" << endl;
	cout << "##################__________####################" << endl;
	cout << "################################################" << endl;
	cout << "############请输入你想要查找的内容##############" << endl;
	cout << "################################################" << endl;
	cout << "################################################" << endl;
}

bool addData(vector<string> &V,string data)//增加数据
{
	int pre_count = 0;
	pre_count = V.size();
	int now_count = 0;
	V.push_back(data);
	now_count = V.size();
	if (now_count > pre_count)
	{
		return true;
	}
	else
		return false;
}

bool deleteData(vector<string>& V, string data)//删除数据
{
	for (vector<string>::iterator it = V.begin(); it != V.end(); it++)
	{
		if (it->find(data) != string::npos) 
		{
			it = V.erase(it);//v.erase返回的是删除元素的下一个地址
			return true;
		}
	}
	return false;
}

bool searchData(vector<string>& V,string data) //查找功能
{
	int count = 0;
	cout << "以查找到相关关键字 =>:";
	for (vector<string>::iterator it = V.begin(); it != V.end(); it++)
	{
		if (it->find(data) != string::npos)
		{
			cout << *it << " ";
			count++;
		}
		
	}
	cout << endl;
	if (count)
	{
		return true; //返回值为查找的到相关关键字数量
	}
	else
	{
		cout << "0" << endl;
		cout << "未查找到相关的关键词" << endl;
		return false;
	}
}

vector<string> initMy_dataLibrary() //初始化数据库
{
	vector<string> V;
	addData(V, "米饭");
	addData(V, "饺子");
	addData(V, "面条");
	addData(V, "馒头");
	addData(V, "白粥");
	addData(V, "粽子");
	addData(V, "面包");
	addData(V, "米粉");
	return V;
}

void printLibrary(vector<string>& V) //查看数据库
{
	cout << "我们有以下食物 =>: ";
	for (vector<string>::iterator it = V.begin(); it != V.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}


void Way_of_search()
{
	string input;
	int a = 0;
	vector<string> Library = initMy_dataLibrary();
	do
	{
		cin >> input;
		searchData(Library, input);
		cout << "输入0可退出搜索" << endl;
		cin >> a;
		while (cin.fail()) //判断是否输入正确
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略缓冲区中的所有字符
			cout << "输入无效,请重新输入数字:";
			cin >> a;
		}
	} while (a);

	/*My_dataLibrary(Library);
	printLibrary(Library);*/
	
	
	//deleteData(Library, "面包");
	//printLibrary(Library);
}



int main()
{
	Menu();
	Way_of_search();
	return 0;
}

改善空间
1.初始化数据库复杂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值