| | |
Structure in Maps
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
Hi friends,
I am back. I was doing some hands-on with the C++ and I think I am a bit confident now
I came across an Exercise question in a C++ book which is as follows:
There is a structure with the variables payment_amount, method_of_payment and risk or priority. The output will give the risk wise cumulative frequency of the Amount. Oh, i dont know how to go about on explainin the question further lol I will give the code which I wrote using struct and basic for loops in VC++. Here it is:
So, say if I give the inputs as
Type--Amount --RiskLevel
20 -- 40 --1
21 --50 --1
22 --20 --2
23 --43 --3
24 --100 --1
The Output for my above code will be:
20--190--1
22--20--2
23--43--3
Now I want to implement the same using MAP in STL. I have no idea how I can go on with this. Can any one guide me through please? :rolleyes:
Oh yes! I forgot to say that I do have a logic in my mind for this but not sure how to implement though. I am planning to pass the risk level and struct as parameters for my map. Then i will check if the risk level already exists. If yes, i will add up the amount; otherwise, I will add a new row for the risk level (if the risk level is not already exists, i mean).
I am back. I was doing some hands-on with the C++ and I think I am a bit confident now
I came across an Exercise question in a C++ book which is as follows:There is a structure with the variables payment_amount, method_of_payment and risk or priority. The output will give the risk wise cumulative frequency of the Amount. Oh, i dont know how to go about on explainin the question further lol I will give the code which I wrote using struct and basic for loops in VC++. Here it is:
c++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include "iostream" #include <conio.h> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int i,j; int totAmt[3]; bool b[]={false, false ,false}; struct Tender { int tenderType[5]; int tenderAmt[5]; int tenderRiskLvl[5]; }tenderVariable; for (j=0;j<3;j++) totAmt[j]=0; for (i=0; i<5; i++) { cout << "Enter Tender Type: " << i+1 << endl; cin >> tenderVariable.tenderType[i]; cout << "Enter Tender Amount: " << i+1 << endl; cin >> tenderVariable.tenderAmt[i]; cout << "Enter Tender Risk Level: " << i+1 << endl; cin >> tenderVariable.tenderRiskLvl[i]; } for (i=0;i<5;i++) { if (tenderVariable.tenderRiskLvl[i]==1) totAmt[0]=tenderVariable.tenderAmt[i]+totAmt[0]; if (tenderVariable.tenderRiskLvl[i]==2) totAmt[1]=tenderVariable.tenderAmt[i]+totAmt[1]; if (tenderVariable.tenderRiskLvl[i]==3) totAmt[2]=tenderVariable.tenderAmt[i]+totAmt[2]; } cout << endl << "The result is " << endl << endl; cout << "Type \t" << "Amt \t" << "Risk\t" << endl; for (i=0;i<5;i++) { if (tenderVariable.tenderRiskLvl[i]==1 && b[0]==false) { cout << tenderVariable.tenderType[i] << "\t" << totAmt[0] << "\t" << tenderVariable.tenderRiskLvl[i] << endl; b[0] = true; } else if (tenderVariable.tenderRiskLvl[i]==2 && b[1] == false) { cout << tenderVariable.tenderType[i] << "\t" << totAmt[1] << "\t" << tenderVariable.tenderRiskLvl[i] << endl; b[1] = true; } else if (tenderVariable.tenderRiskLvl[i]==3 && b[2] == false) { cout << tenderVariable.tenderType[i] << "\t" << totAmt[2] << "\t" << tenderVariable.tenderRiskLvl[i] << endl; b[2] = true; } } getch(); return 0; }
So, say if I give the inputs as
Type--Amount --RiskLevel
20 -- 40 --1
21 --50 --1
22 --20 --2
23 --43 --3
24 --100 --1
The Output for my above code will be:
20--190--1
22--20--2
23--43--3
Now I want to implement the same using MAP in STL. I have no idea how I can go on with this. Can any one guide me through please? :rolleyes:
Oh yes! I forgot to say that I do have a logic in my mind for this but not sure how to implement though. I am planning to pass the risk level and struct as parameters for my map. Then i will check if the risk level already exists. If yes, i will add up the amount; otherwise, I will add a new row for the risk level (if the risk level is not already exists, i mean).
Last edited by dahlia_06; Mar 28th, 2007 at 8:00 am.
..... When you want it the most there is no easy way out, When you ready to go and your heart left in doubt, Dont give up on your faith, Victory comes to those who believe it and That's the way it is!...
There is a small problem in teh way you've used structure and arrays concepts. Better way of doing this would be to have a struct with 3 members and then use an array of struct. (instead of having arrays as members of the struct)
Like this:
A better usecase for using std::map is when you have to associate a value with a key (e.g. "name of student" is the key and "his marks in all exams" is the value).
For your problem in general I would recomment you try with vector instead of map.
In general for STL Map Intro you can see this and this. You should find things on vector as well in same links.
If you want to try out map check this may be there is a problem more suitable for usage of map.
Like this:
c Syntax (Toggle Plain Text)
struct Tender { int tenderType; int tenderAmt; int tenderRiskLvl; }tenderVariableArray[5];
A better usecase for using std::map is when you have to associate a value with a key (e.g. "name of student" is the key and "his marks in all exams" is the value).
For your problem in general I would recomment you try with vector instead of map.
In general for STL Map Intro you can see this and this. You should find things on vector as well in same links.
If you want to try out map check this may be there is a problem more suitable for usage of map.
•
•
Join Date: Mar 2007
Posts: 7
Reputation:
Solved Threads: 0
Okay mate. I shall look at the STL stuff where you linked me to. Meanwhile, could you please tellme what is the significance of using array of members and array of struct. I was in an assumption its actually two ways of implementing it. Has it got something to do with the way how the memory been allocated? Thanks
..... When you want it the most there is no easy way out, When you ready to go and your heart left in doubt, Dont give up on your faith, Victory comes to those who believe it and That's the way it is!...
•
•
•
•
I was in an assumption its actually two ways of implementing it. Has it got something to do with the way how the memory been allocated? Thanks
No it has nothing to do with memory allocation. Both use same amount of memory. The memory layout will be different though.
•
•
•
•
Meanwhile, could you please tell me what is the significance of using array of members and array of struct.
Now when you have a struct to represent a Tender, you can do it in 2 ways. But in your way (read this literally not technically) "ONE object of Tender does NOT represent ONE Tender". In my way it does.
So my way makes things easier to work with, understandable...
![]() |
Similar Threads
- How to create image maps that look so nice... (Graphics and Multimedia)
- Generic method parser / invoker (C#)
- Discrete Math or Data structure (Computer Science)
- Scandisc stops at "File Structure" (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Help with calculating avrg from input file.
- Next Thread: who will convert in to machine language
Views: 964 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





