Re: [C++] Help!: map<userdefined*, vector<int>>

From: Mark (mark_netNews_at_hotmail.com)
Date: 06/22/04


Date: Mon, 21 Jun 2004 21:44:20 -0400

On 21 Jun 2004 17:05:45 -0700, email_entropy123@yahoo.com (entropy123)
wrote:

>Hi All,
>
>I'm attempting to make a map of vectors, where the key is a user
>defined class.
>
>typedef std::map< Data*, std::vector <int> > vmap;
>
>Is what I'm working with right now. I can't figure out how to add/take
>out items to this structure.... I'm not sure I can use Data* as a key
>for starters...
>
>Though, in a pinch, I would take advice on a:
>
>typedef std::map< int, std::vector <int> > vmap;
>
>implementation...
>
>Any suggestions?
>Thanks,
>ent

Perhaps this might help

#include <iostream>
#include <string>
#include <vector>
#include <map>
 
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
using std::ostream;

//using namespace std; is not as 'tedious'

class Foo
 {
  private:
     string Data;
  public:
     Foo() : Data( "Foo" ) {}
     Foo( const string& S ) : Data( S ) {}
     string Print() const { return Data; }

     friend ostream& operator << ( ostream& Out, const vector<Foo>& B
)
     {
       vector<Foo>::const_iterator Pos = B.begin();
       for( Pos; Pos != B.end(); ++Pos )
            Out << Pos->Print() << endl;

       return Out;
     }
 };

int main()
 {
  std::map<std::string, std::vector<Foo> > MyMap;
  Foo A, B( "Hello" ), C( "World" );
  vector<Foo> V; // Fill the vector
  V.push_back( A );
  V.push_back( B );
  V.push_back( C );

  MyMap[ "A" ] = V; // Now we're ready. Create string, vector pair
  std::map<std::string, std::vector<Foo> >::iterator Pos =
MyMap.begin();

  cout << "Key (" << Pos->first << ") " << endl << Pos->second;

  return 0;
 }

Mark
------------------------------------------------------------------------
If I wanted to measure the natural resonances of any object
-- bell, Helmholz resonator -- I imagine that I would get a better
result by rigging up some kind of continuous exciter than by
attempting to analyze an impulse response



Relevant Pages

  • Re: [C++] Help!: map<userdefined*, vector<int>>
    ... >> I'm attempting to make a map of vectors, where the key is a user ... If I wanted to measure the natural resonances of any object ... Helmholz resonator -- I imagine that I would get a better ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question from a Newb
    ... >>int main ... >1) start infinite loop (i.e. termination will be internal) ... >6) apply algorithm ... If I wanted to measure the natural resonances of any object ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Attempt at fixing the issue.
    ... then use malloc to dynamically size tmp first. ... >> int main ... If I wanted to measure the natural resonances of any object ...
    (alt.comp.lang.learn.c-cpp)
  • more strings
    ... To do this I wrote a function to account for the 'minus' sign but ... int IntEquivalent(const char* str) ... If I wanted to measure the natural resonances of any object ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Dynamic arrays, memory allocation etc.
    ... >int main ... tendency to use push_back to insert ... elements into array. ... If I wanted to measure the natural resonances of any object ...
    (alt.comp.lang.learn.c-cpp)