yuAriellexi 2018-09-12 13:14 采纳率: 100%
浏览 2073
已结题

可以建立map< vector<string>, set <string> >这样的map么

血型遗传对照表如下:
图片说明

 #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
map <vector <string>, set <string> > mp;//建立输入血型vector, 与输出可能会出现血型set之间联系的map
string f, m;//双方血型读取
vector <string> i;  //建立输入血型vector
set <string> o;//可能会出现血型set
set <string> un;//不可能会出现血型set
map <vector <string>, set <string> > unmp;//建立输入血型vector, 与不可能会出现血型set之间联系的map


void table()
{
    //将全部血型输入不可能会出现血型un这一set,当输出任意可能出现血型,使用erase()将un这一set里的该血型擦除
    un.insert("AB");
    un.insert("A");
    un.insert("B");
    un.insert("O");
    //应该把f, m放到打表法里面么
    if (f != "AB" || m != "AB")
    {
        o.insert("O");
        un.erase("O");
        o.insert(f);
        un.erase(f);
        o.insert(m);
        un.erase(m);
    }
    if ( (f == "A" && m == "B") || (f == "B" && m == "A"))
    {
        o.insert("AB");
        o.insert("A");
        o.insert("B");
        un.erase("AB");
        un.erase("A");
        un.erase("B");
    }
    if ( (f == "AB" && m != "O") || (m == "AB" && f!= "O"))
    {
        o.insert("A");
        o.insert("B");
        o.insert("AB");
        un.erase("AB");
        un.erase("A");
        un.erase("B");
    }
    if ( (f == "AB" && m == "O") || (m == "AB" && f == "O") )
    {
        o.insert("A");
        o.insert("B");
        un.erase("A");
        un.erase("B");      
    }
    mp[i] = o;
    unmp[i] = un;
}

int main()
{
/***********************************************
    string f, m;
    vector <string> v;
    cin >> f >> m;//这样读会读入','
************************************************/
    table();
    string str;
    cin >> str;
    string a = "";
    string b = "";
    int bstart = 0;

    for (int z = 0; z < str.length(); z++)
    {
        while (str[z] != ',')
        {       
            a += str[z];
        }
        if (str[z] == ',')
        {
            bstart = z;
        }
    }
    for (int z = bstart; z < str.length(); z++)
    {
        b+= str[z];
    }
    i.push_back(a);
    i.push_back(b);

    set <string> tempi;
    set <string> tempo;
    for (auto it = mp.begin(); it != mp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
    {
        tempi = *it;
        for (int i = 0; i < tempi.size(); i++)
        {
            if (i == 0)
            {
                cout << tempi[i];
            }
            else
            {
                cout << "," << tempi[i];
            }
        }
    }

    for (auto it = unmp.begin(); it != unmp.end(); it++)//注意这一句的写法auto it = .begin(); it != .end(); it++
    {
        tempo = *it;
        for (int i = 0; i < tempo.size(); i++)
        {
            if (i == 0)
            {
                cout << tempo[i];
            }
            else
            {
                cout << "," << tempo[i];
            }
        }
    }   
}


请实现一个程序,输入父母血型,判断孩子可能的血型。
给定两个字符串father和mother,代表父母的血型,请返回一个字符串数组,代表孩子的可能血型(按照字典序排列)。

测试样例:
”A”,”A”
返回:[”A”,“O”]

  • 写回答

5条回答 默认 最新

  • llxmedici 2018-09-13 01:08
    关注

    当然是可以的,只要提供的key的类型满足Compare的要求即可,C++标准规定了Compare要求提供strict weak ordering:

        comp(a,a)==false
        If comp(a,b)==true then comp(b,a)==false
        if comp(a,b)==true and comp(b,c)==true then comp(a,c)==true 
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable