Sponsored Content
Top Forums Programming Map with struct as key and vector as value Post 302917416 by Corona688 on Tuesday 16th of September 2014 01:36:57 PM
Old 09-16-2014
Any challenge is too big when you take the path of maximum resistance.

This problem is exceptionally kind in giving you a unique, integer ID for each student... That's a gift to be embraced.

The code I gave you wasn't pseudocode. This is how I use it:

Code:
#include <stdio.h>

#include <map>
#include <vector>
#include <string>

using namespace std;

struct grades {
        string course, credit, grade;
};

int main(void)
{

        map<int, vector < grades > > marks;
        map<int, string> names;

        {
                int id=1234;
                const char *name="larry";
                grades grade={"a","b","c"};

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

If you wanted to bundle the data together and use it in a 'smart' way instead of updating individual variables, you could put it in a class with multiple members inside it. It still clearly belongs in multiple structures. How many times are you supposed to store someone's name and unique ID?

If you really wanted you could put the name and unique id in a pair<int, string>, which you should be able to plug into map < pair< int, string > , ... >
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
Vector(7rheolef)						    rheolef-6.1 						  Vector(7rheolef)

NAME
Vector - STL vector<T> with reference counting DESCRIPTION
The class implement a reference counting wrapper for the STL vector<T> container class, with shallow copies. See also: @quotation The Standard Template Library, by Alexander Stephanov and Meng Lee. @end quotation This class provides the full vector<T> interface specification an could be used instead of vector<T>. NOTE
The write accessors T& operator[](size_type) as in v[i] may checks the reference count for each access. For a loop, a better usage is: Vector<T>::iterator i = v.begin(); Vector<T>::iterator last = v.end(); while (i != last) { ...} and the reference count check step occurs only two time, when accessing via begin() and end(). Thus, in order to encourage users to do it, we declare private theses member functions. A synonym of operator[] is at. IMPLEMENTATION
template<class T> class Vector : private smart_pointer<vector_rep<T> > { public: // typedefs: typedef iterator; typedef const_iterator; typedef pointer; typedef reference; typedef const_reference; typedef size_type; typedef difference_type; typedef value_type; typedef reverse_iterator; typedef const_reverse_iterator; // allocation/deallocation: explicit Vector (size_type n = 0, const T& value = T ()); Vector (const_iterator first, const_iterator last); void reserve (size_type n); void swap (Vector<T>& x) ; // accessors: iterator begin (); const_iterator begin () const; iterator end (); const_iterator end () const; reverse_iterator rbegin(); const_reverse_iterator rbegin() const; reverse_iterator rend(); const_reverse_iterator rend() const; size_type size () const; size_type max_size () const; size_type capacity () const; bool empty () const; void resize (size_type sz, T v = T ()); // non-standard ? private: const_reference operator[] (size_type n) const; reference operator[] (size_type n); public: const_reference at (size_type n) const; // non-standard ? reference at (size_type n); reference front (); const_reference front () const; reference back (); const_reference back () const; // insert/erase: void push_back (const T& x); iterator insert (iterator position, const T& x = T ()); void insert (iterator position, size_type n, const T& x); void insert (iterator position, const_iterator first, const_iterator last); void pop_back (); void erase (iterator position); void erase (iterator first, iterator last); }; rheolef-6.1 rheolef-6.1 Vector(7rheolef)
All times are GMT -4. The time now is 06:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy