map小例子

#include <iostream>
#include <map>
#include <string>
#include <cstring>
using namespace std;
map<string, int> StringToint;
char p[10][11];
int spaces[10];
//定义空格数量
bool x_child_y(int x, int y)
{
	// x是不是y的 孩子
	if (spaces[x] - spaces[y] != 2)
	{
		return false;
	}
	for (int i = y + 1; i < x; i++)
	{
		if (spaces[i] < spaces[x])
		{
			return false;
		}
	}
	return true;
	//返回正确
}
bool x_parent_y(int x, int y)
{
	return x_child_y(y, x);
}
// 判断是不是
bool x_sibiling_Y(int x, int y)
{
	// 判断是否相同
	if (spaces[x] != spaces[y])
	{
		return false;
	}
	if (x > y)
	{
		swap(x, y);
	}
	for (int i = x + 1; i < y; i++)
	{
		if (spaces[i] < spaces[y])
		{
			return false;
		}
	}
	return true;
}
bool x_descendant_y(int x, int y)
{
	//返回
	if (spaces[x] < spaces[y])
	{
		return false;
	}
	for (int i = y + 1; i < x; i++)
	{
		if (spaces[i] == spaces[y])
		{
			return false;
		}
	}
	return true;
}
bool x_ancestor_y(int x, int y)
{
	// 返回值
	return x_descendant_y(y, x);
}
void result(int x, int y, char *a)
{
	if (strcmp(a, "child") == 0)
	{
		// 这个类型是 const * 类型的
		if (x_child_y(x, y))
		{
			cout << true;
		}
		cout << false;
	}

	if (strcmp(a, "parent") == 0)
	{
		if (x_parent_y(x, y))
		{
			cout << true;
		}
		else
			cout << false;
	}
	if (strcmp(a, "descendant"))
	{
		if (x_descendant_y(x, y))
		{
			cout << true;
		}
		else
			cout << false;
	}
	if (strcmp(a, "sibiling") == 0)
	{
		if (x_sibiling_Y(x, y))
		{
			cout << true;
		}
		else
			cout << false;
	}
	if (strcmp(a, "ancestor") == 0)
	{
		if (x_ancestor_y(x, y))
		{
			cout << true;
		}
		else
			cout << false;
	}
}
int main()
{
	// 使用map 会快一点 但是 , 在比较空格数量的时候需要别的数组 存储结果
	char c; //定义变量
	char name[11];
	int space = 0;
	int number, edge;
	cin >> number >> edge;
	//输入 人数,以及判断
	for (int i = 0; i < number; i++)
	{
		space = 0;
		while ((c = getchar()) == ' ')
		{
			space++;
		}
		spaces[i] = space;
		space = 1;
		name[0] = c;
		while ((c = getchar()) != '\n')
		{
			name[space++] = c;
		}
		name[space] = '\0';
		StringToint[name] = spaces[i];

		//结束标志
		strcpy(p[i], name);
	}
	for (int i = 0; i < edge; i++)
	{
		//输入陈述句
		char str1[11], str2[11], str3[11], str4[11], str5[11], str6[11];
		scanf("%s %s %s %s %s %s", str1, str2, str3, str4, str5, str6);
		int re1, re2;
		re1 = StringToint[str1];
		re2 = StringToint[str2];
		result(re1,re2,str4);
		//顺序不能错
	}
}

参考文档:https://blog.csdn.net/qq_41231926/article/details/84841386

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值