c ++,使用for循环将键和值存储在地图中

时间:2015-05-16 03:35:45

标签: c++ for-loop dictionary

嘿伙计们我试图修改我的代码并且好奇如果可行的话。

现在我有两个从两个文件中读取的for循环 并将值存储到两个数组中 有点像这样:

fstream infile1(file_name1.c_str());
fstream infile2(file_name2.c_str());

map<string,float>my_map;    

for(int i=0;i<10;i++){

        infile1 >>my_map<string> ;

    for(int i=0;i<10;i++){

        infile2 >>my_map<float> ;

有没有办法通过用地图替换两个数组来修改它? 有点像这样:

{{1}}

1 个答案:

答案 0 :(得分:3)

如果要将数据输入地图,则需要从文件中获取值,然后将其添加到地图中。请注意,地图具有唯一键,因此如果重复键,则会覆盖以前在地图中的内容。如果您不想那样,那么您需要使用std::multimap。如果您需要使用[]此解决方案将无效,因为它没有fstream infile1(file_name1.c_str()); fstream infile2(file_name2.c_str()); map<string,float>my_map; std::string temp; float number; while(infile1 >> temp && infile2 >> number) my_map[temp] = number; 运算符。

infile1 >> temp

另请注意,如果您的字符串包含空格,则需要将getline(infile1, temp)替换为public static void main(String[] args) { ///declaration of variables int playAgain; int days, customers = 120; int CUPS_PITCHER = 14; double money = 20.00, earned = 0; double [] cupsPrice = {0.93, 1.65, 2.77}, lemonsPrice = {0.98, 2.05, 4.38}, sugarPrice = {0.74, 1.63, 3.31}, icePrice = {0.99, 2.01, 3.95}; //inventory ///string input String cupsS = "", lemonsS = "", sugarS = "", iceS = ""; //int input values int cups = 0, lemons = 0, sugar = 0, ice = 0; //String for Pitcher double priceCup; int lemonsPitcher, sugarPitcher, iceCup; ////array for items options String [] cupsOptions = {"25","50","100"}; String [] lemonsOptions = {"10","30","75"}; String [] sugarOptions = {"8","20","48"}; String [] iceOptions = {"100","250","500"}; int temperature; String [] weather = {"Sunny","Hazy","Cloudy","Overcast","Rainny"}; JOptionPane.showMessageDialog(null,"Welcome to our Lemonade Stand Game"); days = getDays(); ///set flag for loops boolean ingredients, cupsFlag, lemonsFlag, sugarFlag, iceFlag; boolean [] flag = {false,false,false,false}; NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); String [] itemName = {"Cups","Lemons","Sugar","Ice Cubes"}; String [][] itemOptions = {cupsOptions,lemonsOptions,sugarOptions,iceOptions}; for(int i=1; i<=days; i++){ /////random weather Random rand = new Random(); int randomWeather = rand.nextInt(5); //random temperature from 59 to 99 temperature = rand.nextInt(99-59) + 59; int response = 0; ///loop while not play game while(response != 1){ String[] options = new String[] {"Buy", "Play Game", "Cancel"}; response = JOptionPane.showOptionDialog(null, "You currently have " + defaultFormat.format(money) + " Day " + i + "\nCups: " + cups + " *** Lemons: " + lemons + "\nSugar: " + sugar + " *** Ice: "+ice + "\nHigh Temperature: " + temperature + "\nWeather Forecast: " + weather[randomWeather], "Inventory", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]); ingredients = false; ///if buy ingredientes ** when they click buy while(response == 0 && !ingredients){ String[] optionsBuy = new String[] {"Buy Cups", "Buy Lemons", "Buy Sugar", "Buy Ice","Back to Game"}; String line1 = "\n25 Cups: $" + cupsPrice[0] + " 10 Lemons: $" + lemonsPrice[0] + " 8 Cups of Sugar: $" + sugarPrice[0] + " 100 Ice Cubes: $"+icePrice[0]; String line2 = "\n50 Cups: $" + cupsPrice[1] + " 30 Lemons: $" + lemonsPrice[0] + " 20 Cups of Sugar: $" + sugarPrice[1] + " 250 Ice Cubes: $"+icePrice[1]; String line3 = "\n100 Cups: $" + cupsPrice[2] + " 75 Lemons: $" + lemonsPrice[2] + " 48 Cups of Sugar: $" + sugarPrice[2] + " 500 Ice Cubes: $"+icePrice[2]; int responsePurchase = JOptionPane.showOptionDialog(null, "You currently have " + defaultFormat.format(money) + " Day " + i + line1 + line2 + line3 + "\nHigh Temperature: " + temperature + "\nWeather Forecast: " + weather[randomWeather], "Inventory", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, optionsBuy, optionsBuy[0]); double [][] priceOptions = {cupsPrice,lemonsPrice,sugarPrice,icePrice}; int [] qty = {0,0,0,0}; for(int j=0; j<4; j++){ if(responsePurchase == j) qty[j] = buyItems(itemName[j],itemOptions[j],flag[j],money); //money = money - itemPrice[j][0]; } ///deduct money for(int k=0;k<4;k++){ for(int j=0;j<3;j++){ if(qty[k] == Integer.parseInt(itemOptions[k][j])){ money = money - priceOptions[k][j]; } } } ////add items purchased cups += qty[0]; lemons += qty[1]; sugar += qty[2]; ice += qty[3]; //System.out.println(itemOptions[0][1]); /* if(qty[0] == 25){ money = money - cupsPrice[0]; } if(qty[0] == 50){ money = money - cupsPrice[1]; } if(qty[0] == 100){ money = money - cupsPrice[2]; } // buy lemons cupsFlag = false; if (responsePurchase == 0) { int qty = buyItems("Cups",cupsOptions,cupsFlag,money); if (qty != 0) { cups += qty; } } // buy lemons lemonsFlag = false; if (responsePurchase == 1) { int qty = buyItems("Lemons",lemonsOptions,lemonsFlag,money); if (qty != 0) { lemons += qty; } } */ ///go back to game when back to game click if(responsePurchase == 4){ ingredients = true; } }///end while buy ingredients }///end while buy JTextField fieldCup = new JTextField("0.25"); JTextField fieldLemons = new JTextField("4"); JTextField fieldSugar = new JTextField("4"); JTextField fieldIce = new JTextField("4"); Object[] fields = {"Price per Cup in Cents", fieldCup,"Lemons per Pitcher", fieldLemons,"Sugar per Pitcher", fieldSugar, "Ice per Cup", fieldIce}; int responsePitcher = JOptionPane.showConfirmDialog(null,fields,"Price",JOptionPane.OK_CANCEL_OPTION); if(responsePitcher == JOptionPane.CANCEL_OPTION){ int stopGame = JOptionPane.showConfirmDialog(null,"Do you wish to cancel the game? All progress will be lost","",JOptionPane.YES_NO_OPTION); if(stopGame == JOptionPane.YES_OPTION){ System.exit(0); } } while(!validateDouble(fieldCup.getText()) || !validateInt(fieldLemons.getText()) || !validateInt(fieldSugar.getText()) || !validateInt(fieldIce.getText())){ if(responsePitcher == JOptionPane.CANCEL_OPTION){ int stopGame = JOptionPane.showConfirmDialog(null,"Do you wish to cancel the game? All progress will be lost","",JOptionPane.YES_NO_OPTION); if(stopGame == JOptionPane.YES_OPTION){ System.exit(0); } } JOptionPane.showMessageDialog(null,"One of the inputs is incorrect! try Again","ERROR",JOptionPane.ERROR_MESSAGE); responsePitcher = JOptionPane.showConfirmDialog(null,fields,"Price",JOptionPane.OK_CANCEL_OPTION); } priceCup = Double.parseDouble(fieldCup.getText()); lemonsPitcher = Integer.parseInt(fieldLemons.getText()); sugarPitcher = Integer.parseInt(fieldSugar.getText()); iceCup = Integer.parseInt(fieldIce.getText()); for(int k=0; k<5; k++){ if(weather[randomWeather].equals(weather[k])){ ////if weather is not sunny reduce possible customers if(!weather[randomWeather].equals(weather[0])){ customers = customers - (customers * k/15); } } } //System.out.println(customers);///testing results //System.out.println(fieldCup.getText());///testing results System.out.println(customers);///testing results //showBar(); ///perfect recepie if(temperature > 58){ ///if sunny if(weather[randomWeather].equals(weather[0])){ } } //too expensive or not right ingredients reduce possible customers if(priceCup > 0.25){ customers = customers - (customers * 10/100);///reduce customers by 10% } if(lemonsPitcher > 7 || lemonsPitcher < 5){ customers = customers - (customers * 10/100);///reduce customers by 10% } if(sugarPitcher > 7 || sugarPitcher < 5){ customers = customers - (customers * 10/100);///reduce customers by 10% } if(iceCup > 10 || iceCup < 6){ customers = customers - (customers * 15/100);///reduce customers by 15% } ///determine max cups according to inventory int maxCupsLemons = (lemons / lemonsPitcher) * CUPS_PITCHER; int maxCupsSugar = (sugar / sugarPitcher) * CUPS_PITCHER; int maxCupsIce = (ice / iceCup); int maxCupsp = cups; int [] maxCups = {maxCupsLemons, maxCupsSugar, maxCupsIce, maxCupsp}; System.out.println(Arrays.toString(maxCups)); int minValue = maxCups[0]; for(int m=0;m<maxCups.length;m++){ if(maxCups[m] < minValue){ minValue = maxCups[m]; } } System.out.println(minValue); if(minValue < customers){ customers = minValue; } System.out.println(customers);///testing results ////profit earned = priceCup * customers; money += earned; ///deduct inventory //14 cups per pitcher int lemonsSpent = (customers / CUPS_PITCHER) * lemonsPitcher; int sugarSpent = (customers / CUPS_PITCHER) * sugarPitcher; lemons = lemons - lemonsSpent; sugar = sugar - sugarSpent; cups = cups - customers; JOptionPane.showMessageDialog(null,"Your profit for day " + i + " is " + defaultFormat.format(earned)); /////reset variables for next day customers = 120; earned = 0; ice = 0; } //JOptionPane.showMessageDialog(null,days); } }