I'd like to use <vector> to create a multi-dimensional array holding
a string, int, int. . .
something like this
Name Wage Age Ranking
===== ==== === =======
Ben 9 29
Matt 16 38
.
.
Tom 21 50
Then I would like to use std::sort to sort first by age, then by wage. Then I'd like to rank them by wage within their age decade (twenties, thirties, etc.).
Though this is new ground for me (typically i'd hack something using linked list or a bubble sort; but i have seen
single array examples which are more efficient).
After the sort, I would like to rank by the first int ,"wage", creating
a table that can be presented in a Windows GUI app. It should be compatible with .NET (how is that different from what I'm accustomed to Visual C++ 6.0?]
Thanks :)
vinvin
Paul McKenzie
November 28th, 2006, 01:29 PM
I'd like to use <vector> to create a multi-dimensional array holding
a string, int, int. . . I would suggest a struct that represents each item, not a multidimensional array.
Here is a simple code sample:
#include <string>
struct PersonInfo
{
std::string name;
int wage;
int age;
};
Also, your problem needs to be divided into two pieces. The first piece has nothing to do with the Windows API (sorting). Therefore you would have been better asking the question concerning sorting in the non-Visual C++ forum.
Once you can sort them, then the part about how to display this in a GUI is more relevant to this forum.
Regards,
Paul McKenzie
vinniez_ok
November 29th, 2006, 01:06 AM
Thanks Much Paul ! Prayer answered you are!
I modified a little and now have the question taken to the next step to get . . .
Expected Output:
Sorted by Age + Wage
Name Age Wage Ranking
===== ==== === =======
Ben 9 20 1
Matt 10 28 2
Steve 12 33 1
Cliff 13 33 2
Brandon 15 14 1
Vini 17 28 3
Tom 21 30 3
Mark 22 37 4
Tim 34 28 4
A long, long time ago, member Krishnaa posted that to get output in a GUI app that I'd need to "use Win32 API's inside your own classes or WinMain function".
Also, member Roy Rosinnes said that 'Inter Process Communication' may require mailslots (and then provided many, many lines of code~ didn't look like C++ to me).
As this code is a one time app & will not be re-used, I was wondering if there was an easy way to provide the most basic GUI output of the table generated by this code into Windows and where in the C++ code I would insert it to ouptut to a GUI app. I'm thinking that things have progressed since those postings in 2000 & 2002; however, i didn't find any threads posted on this within the last year.
Thx much! :wave:
Vin
// commented out since names/data will be provided
// std::string NameGenerator()
// {
// static int i = 0;
// ++i;
// std::string sName = "Joe";
// return sName += (char)(i + '0');
// }
void SortByAgeWage(std::vector<PersonInfo>& info)
{
// first sort by age
std::sort(info.begin(), info.end(), SortByAge);
// sort by wage given that the array has been sorted by age
std::sort(info.begin(), info.end(), SortByWage);
}
int main()
{
std::vector<PersonInfo> theInfo;
theInfo.push_back(Tim, 12, 33);
theInfo.push_back(Mark, 34, 28);
theInfo.push_back(Tom, 21, 30);
theInfo.push_back(Cliff, 13, 33);
theInfo.push_back(Vini, 17, 28);
theInfo.push_back(Matt, 10, 28);
theInfo.push_back(Ben, 9, 29);
theInfo.push_back(Brandon, 15, 14);
// removed this since I supplied the data to be used -- theInfo.push_back(PersonInfo(NameGenerator(), 7, 1));
//...
int i;
std::cout << "Sorted by Age + Wage\n\n";
std::cout << "Name Age Wage Ranking\n";
std::cout << "===== ==== ==== =======\n";
SortByAgeWage(theInfo);
for ( i = 0; i < theInfo.size(); ++i )
std::cout << theInfo[i].name << " "
<< theInfo[i].age << " " << theInfo[i].wage << "\n";
// Somehow here the cout makes it to a GUI App in Windows ??
}
vinniez_ok
November 29th, 2006, 11:40 AM
I found this
2 #include <afxwin.h>
3 // Declare the application class
4 class CHelloApp : public CWinApp
5 {
6 public:
7 virtual BOOL InitInstance();
8 };
9 // Create an instance of the application class
10 CHelloApp HelloApp;
11 // Declare the main window class
12 class CHelloWindow : public CFrameWnd
13 {
14 CStatic* cs;
15 public:
16 CHelloWindow();
17 };
18 // The InitInstance function is called each
19 // time the application first executes.
20 BOOL CHelloApp::InitInstance()
21 {
22 m_pMainWnd = new CHelloWindow();
23 m_pMainWnd->ShowWindow(m_nCmdShow);
24 m_pMainWnd->UpdateWindow();
25 return TRUE;
26 }
27 // The constructor for the window class
28 CHelloWindow::CHelloWindow()
29 {
30 // Create the window itself
31 Create(NULL,
32 "Here's where my table data goes ?? ",
33 WS_OVERLAPPEDWINDOW,
34 CRect(0,0,200,200));
35 // Create a static label
36 cs = new CStatic();
37 cs->Create("hello world",
38 WS_CHILD|WS_VISIBLE|SS_CENTER,
39 CRect(50,80,150,150),
40 this);
41 }
Would something like this work?
Thx
Vin
Paul McKenzie
November 29th, 2006, 12:18 PM
Well, I would start a new topic. Since you now have the data in a vector, the only thing needed is to get the data into a GUI control.
Since this is the WinAPI forum, the code you have looks a lot like MFC, which isn't covered here. You can go to the Visual C++ forum, and ask how to output to a GUI window using MFC.
Regards,
Paul McKenzie
vinniez_ok
November 29th, 2006, 12:28 PM
Thanks again ~ I do want Win API, unless it's overly complicated. . . . bite-sized pieces are for me now.
But I'll post another thread and reference this progress.
Thanks so much!
Vin :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.