映射唯一值和重复值的有效方法。可以访问键或值的地方

时间:2018-09-26 16:46:51

标签: c++ c++11 stdmap

没有n个需要与另一个字符串映射的字符串。

package com.company;

import javax.swing.*;

public class Main
{
    public static void main(String[] args)
    {
        {
            BankAccount myBank = new BankAccount();

            myBank.calculateNewBalance();
        }
    }
}

有些字符串需要与重复的字符串进行映射。

package com.company;

import javax.swing.*;

public class BankAccount
{
    private int accountNumber;
    private String accountType;
    private double minSavings = 2500.00;
    private double minChecking = 1000.00;
    private double currentBalance;

    public BankAccount()
    {
        accountNumber = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account number"));
        accountType = JOptionPane.showInputDialog("Please enter your account type");
        currentBalance = Double.parseDouble(JOptionPane.showInputDialog("Please enter your current balance."));
    }

    public int getAccountNumber()
    {
        return accountNumber;
    }

    public void setAccountNumber(int accountNumber)
    {
        this.accountNumber = accountNumber;
    }

    public String getAccountType()
    {
        return accountType;
    }

    public void setAccountType(String accountType)
    {
        this.accountType = accountType;
    }

    public double getMinSavings()
    {
        return minSavings;
    }

    public void setMinSavings(double minSavings)
    {
        this.minSavings = minSavings;
    }

    public double getMinChecking()
    {
        return minChecking;
    }

    public void setMinChecking(double minChecking)
    {
        this.minChecking = minChecking;
    }

    public double getCurrentBalance()
    {
        return currentBalance;
    }

    public void setCurrentBalance(double currentBalance)
    {
        this.currentBalance = currentBalance;
    }

    public void calculateNewBalance()
    {
        if (accountType.equals("S") || accountType.equals("s"))
        {
            accountType = "Savings";
            calculateSavingsBalance();
        } else if (accountType.equals("C") || accountType.equals("c"))
        {
            accountType = "Checking";
            calculateCheckingBalance();
        }
    }

    private void calculateSavingsBalance()
    {
        if (currentBalance >= minSavings)
        {
            double newBalance = currentBalance + (currentBalance * .04 / 12);

            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(currentBalance < minSavings)
        {
            isServiceCharge();
        }
    }

    private void calculateCheckingBalance()
    {
        if (currentBalance > 6000)
        {
            double newBalance = currentBalance + (currentBalance * .03 / 12);
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if (currentBalance >= minChecking)
        {
            double newBalance = currentBalance + (currentBalance * .05 / 12);
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(currentBalance < minChecking)
        {
            isServiceCharge();
        }
    }

    public void isServiceCharge()
    {
        if(accountType.equals("s") || accountType.equals("S"))
        {
            double newBalance = currentBalance - 10.0;
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
        else if(accountType.equals("c") || accountType.equals("C"))
        {
            double newBalance = currentBalance - 25.0;
            JOptionPane.showMessageDialog(null, "Account Number: " + getAccountNumber() + "\nAccount Type: " + getAccountType() + "\nMinimum Balance: $" + getMinSavings()
                + "\nBalance Before Interest and Fees: $" + getCurrentBalance() + "\n\nNew Balance: $" + newBalance);
        }
    }
}

要求:如下情况

情况1:输入品牌名称时。所有者名称作为输出。

pip install apache_beam[gcp]

情况2:当所有者名称输入品牌名称作为输出时。

Ex :     Bacardi_old - > Facundo 
         Smirnoff_old -> Pyotr 
         Seagram_old  -> Joseph
         This keep on ..... may be around 1000

我的方法:

1。我的地图如下:

Ex :     Bacardi_new  -> Facundo 
         Smirnoff_new -> Facundo 
         Seagram_new  -> Facundo 

2。我应该创建两个地图,一个是唯一的映射,另一个是重复的

input : Bacard_old
output: Facundo

就所有方面而言,第二选择是否比第一选择好? 请提出最佳方法。

注意:我坚持使用c ++ 11。没有增强库。

2 个答案:

答案 0 :(得分:2)

最好的方法取决于您的需求。您对访问速度或插入速度感兴趣吗?还是您有兴趣减少已用的内存空间?

您提出的第一个解决方案(具有key = brand和value = owner的地图)使用较少的内存,但需要完整扫描才能按所有者进行搜索。

第二种解决方案:

  • 具有key = brand和value = owner的地图
  • 具有key = brand和value =所有者列表的地图

对于按所有者进行搜索和按品牌进行搜索都更快。但是,它需要更多的内存,并且您还需要为每个新对执行2次插入。

答案 1 :(得分:1)

最好是相对的:)

您可以使用std::multimap

std::multimap<std::string,std::string> my_map;
my_map.insert(std::make_pair("owner_name", "brand_name"));

现在,您可以根据需要根据keyvalue进行搜索。