Map with struct as key and vector as value


 
Thread Tools Search this Thread
Top Forums Programming Map with struct as key and vector as value
# 15  
Old 09-17-2014
Simpler version using more C functions:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>     // You need this for atoi

#include <map>
#include <vector>
#include <string>
using namespace std;

struct course {
    char abbrev[11];
    int credit;
    char grade;
};

int main(void)
{
        char buf[4096];
        map<int, string> names;
        map<int, vector < course > > marks;

        // Read a line into 'buf'
        while(fgets(buf, 4096, stdin) != NULL)
        {
                course grade;
                char name[21];
                int id;

                if(sscanf(buf, "%d %20s %10s %d %c",
                        &id,name,grade.abbrev,&grade.credit,&grade.grade) != 5)
                {
                        fprintf(stderr, "Bad line %s\n", buf);
                        continue;
                }

                names[id]=name;
                marks[id].push_back(grade);
        }

        for(map<int, string>::iterator i=names.begin();
                i != names.end();
                i++)
        {
                int id=(*i).first;
                printf("%d %s\n", id, (*i).second.c_str());

                for(vector<course>::iterator n=marks[id].begin();
                        n != marks[id].end();
                        n ++ )
                {
                        printf("\t%s %d %c\n",
                                (*n).abbrev, (*n).credit, (*n).grade);
                }
        }
}

sscanf shortens it by 20 lines, you can just tell it "lines should like this" and it'll sort it into the arguments you asked for and tell you whether it could. It even protects against overflows if you tell it to (%20s, so it doesn't overrun a 21-char buffer).

This is what bugs me most about the 'scanf is dangerous, strtok is dangerous' camp. As opposed to what, hand-writing your own unchecked ad-hoc string parser? At least C has a parser.

Last edited by Corona688; 09-17-2014 at 05:18 PM..
This User Gave Thanks to Corona688 For This Post:
# 16  
Old 09-17-2014
It even protects against overflows if you tell it to (%20s, so it doesn't overrun a 21-char buffer).
Those are only concerns of advanced programmers. I can't predict the situations even you pointed it out.
BTW, the last version has a bug if there is a blank line in the input file....(No worry, I'll try to fix it myself.)
The biggest point for me is from the lines:
Code:
names[id] = name;
marks[id].push_back(grade);

I did not know this way to assign a map_pair so much simply.

Thanks a lot!
# 17  
Old 09-17-2014
Quote:
Originally Posted by yifangt
Those are only concerns of advanced programmers.
A crashed program doesn't do the job whether you're beginner or advanced.

If you don't start thinking about it when you're a beginner, you never will.

Quote:
BTW, the last version has a bug if there is a blank line in the input file....(No worry, I'll try to fix it myself.)
No it doesn't:

Code:
$ cat student:

2333021 Bokow,R. NS201 3 A
2333021 Bokow,R. MG342 3 A
2333021 Bokow,R. FA302 1 A
2574063 Failin,D. MK106 3 C
2574063 Failin,D. MA208 3 B
2574063 Failin,D. CM201 3 C
2574063 Failin,D. CP101 2 B
2663628 Kingsley,M. QA140 3 A
2663628 Kingsley,M. CM245 3 B
2663628 Kingsley,M. EQ521 3 A
2663628 Kingsley,M. MK341 3 A
2663628 Kingsley,M. CP101 3 B
$ ./a.out

Bad line

2333021 Bokow,R.
        NS201 3 A
        MG342 3 A
        FA302 1 A
2574063 Failin,D.
        MK106 3 C
        MA208 3 B
        CM201 3 C
        CP101 2 B
2663628 Kingsley,M.
        QA140 3 A
        CM245 3 B
        EQ521 3 A
        MK341 3 A
        CP101 3 B

$

This User Gave Thanks to Corona688 For This Post:
# 18  
Old 09-17-2014
BTW, the last version has a bug if there is a blank line in the input file....I meant your first version.
No worry at all!
How much do I owe you?!

Last edited by yifangt; 09-17-2014 at 07:55 PM..
# 19  
Old 09-18-2014
Nothing, I post here for free.
These 2 Users Gave Thanks to Corona688 For This Post:
# 20  
Old 09-18-2014
Posted by Corona688:
Quote:
Nothing, I post here for free.
Really Like you Corona688 Smilie. You are really ideal for people like me and others who want to learn technology.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vector normalisation

In AWK For 3 individual vectors of the form: -2.772 -9.341 -2.857 -5.140 -6.597 -1.823 -2.730 -5.615 1.159 I would like to write a script that parses line by line to (i) normalise, (ii) divide by the norm for *each* vector. I.e. sqrt(-2.772^2 + -9.341^2 + -2.857^2)=10.154 ... (4 Replies)
Discussion started by: chrisjorg
4 Replies

2. Programming

vector c++

hello guys. i'm new to c++. i've problem using two dimensional vector. i've a project of making conway's game of life. this is the code that i have made so far. my problem is how can i give a two dimensional vector through main. glider.vec1 = vec; is not correct way to give a two... (2 Replies)
Discussion started by: nishrestha
2 Replies

3. Programming

Array and Vector

Hi all, from my understanding I understand that I can use array in this manner. struct test { int a; int b; int c; }; test testing; //creating an array with the structer type testing.a=1; testing.b=2; testing.c=3; If I'm not wrong we can use array in this manner,... (12 Replies)
Discussion started by: vinzping
12 Replies

4. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

5. Solaris

Solaris 8 ssh public key authentication issue - Server refused our key

Hi, I've used the following way to set ssh public key authentication and it is working fine on Solaris 10, RedHat Linux and SuSE Linux servers without any problem. But I got error 'Server refused our key' on Solaris 8 system. Solaris 8 uses SSH2 too. Why? Please help. Thanks. ... (1 Reply)
Discussion started by: aixlover
1 Replies

6. Programming

C++ Map using a Vector as a Value Type?

I modified some code I found on Wikipedia concerning maps to see if it would work before applying it to a project I'm working on that has a similar idea. What I would want is for a user to be able to enter sentences and then be able to distinguish how many times a the person entered a word in a... (4 Replies)
Discussion started by: kcgb20
4 Replies

7. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

8. Programming

Vector Traversing

Hi i have the following structure struct S { char Mod_num; char val; char chr_nm_cd; } I am reading a 2GB file and inserting into the structure and writing into a vector. I feel like only vector will be a right option. I tried with multimap but it is memory intensive and hence i... (1 Reply)
Discussion started by: dhanamurthy
1 Replies

9. UNIX for Dummies Questions & Answers

Pressing backspace key simulates enter key

Hi, Whenever i press the backspace key, a new line appears, i.e. it works like a enter key. :confused: Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies
Login or Register to Ask a Question