Archived

This topic is now archived and is closed to further replies.

Multimap Question

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know where I could find information on the basics of using Multimap. All I really need to know is Inserting, Deleting, Weither or not deleting will delete the object (If using pointers), returning a record from a key, and searching if a key exists (bool). If you have any other good STL tutorials and what not, feel free to post them. Thanks, Dave.

Share this post


Link to post
Share on other sites
I understand most of it now, but I'm still puzzled how one would go about searching for a key and getting a return of a returntype

std::multimap<int, int> newmap;
newmap.insert(std::pair<int, int>(100, 6));
newmap.insert(std::pair<int, int>(80, 7));
newmap.insert(std::pair<int, int>(120, 8));
newmap.insert(std::pair<int, int>(110, 9));

for (std::multimap<int, int>::iterator it = newmap.begin();
it != newmap.end();
++it)
std::cout << " [" << (*it).first << ", " << (*it).second << "]" << std::endl;


For searching, one wouldn't do a classical for loop with a multimap -- would they?

[edited by - PumpkinPieman on November 9, 2003 9:01:48 PM]

Share this post


Link to post
Share on other sites
Well, I''m going to using it in two ways, one for handling pointers to an object. And the other to handle the objects themselves. But for some reason, when I try to work with pointers it''s giving me alot of errors.

Share this post


Link to post
Share on other sites
What compiler are you using, and what kind of errors are you getting? I''ve never had multimap give me any problems instantiating it with pointer types.

Share this post


Link to post
Share on other sites
As far as I know the standard .net one.

int newint = 20;

std::multimap<int, *int> newmap;
newmap.insert(std::pair<int, *int>(100, &newint));
newmap.insert(std::pair<int, *int>(80, &newint));
newmap.insert(std::pair<int, *int>(120, &newint));
newmap.insert(std::pair<int, *int>(110, &newint));

Maybe I'm doing something wrong.

c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(18) : error C2062: type 'int' unexpected
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(19) : error C2062: type 'int' unexpected
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(19) : error C2143: syntax error : missing ',' before ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(19) : error C2059: syntax error : ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(20) : error C2062: type 'int' unexpected
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(20) : error C2143: syntax error : missing ',' before ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(20) : error C2059: syntax error : ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(21) : error C2062: type 'int' unexpected
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(21) : error C2143: syntax error : missing ',' before ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(21) : error C2059: syntax error : ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(22) : error C2062: type 'int' unexpected
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(22) : error C2143: syntax error : missing ',' before ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(22) : error C2059: syntax error : ')'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(33) : error C2143: syntax error : missing ';' before '}'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(34) : error C2143: syntax error : missing ';' before '}'
c:\Documents and Settings\Dave\My Documents\Visual Studio Projects\Twilight\Main.cpp(34) : fatal error C1004: unexpected end of file found






[edited by - PumpkinPieman on November 9, 2003 9:22:23 PM]

Share this post


Link to post
Share on other sites
It's because you typed "* int" instead of "int *" when you declared your map.

edit: and your pairs.

[edited by - SiCrane on November 9, 2003 9:24:55 PM]

Share this post


Link to post
Share on other sites
Here''s another question

#include <map>

#pragma once

template<class _key, class _data>
class CBag
{
public:
CBag(void);
~CBag(void);
//* TODO: Find element function

//* TODO: Add element function

// TODO: Delete element funtion

// TODO: Item Exists

std::multimap<_key, _data> Bag;
public:
_data Find(_key m_pElement);
void Add(_key m_pKey, _data m_pData);
};


#include ".\bag.h"

template<class _key, class _data>
CBag<_key, _data>::CBag(void)
{
}

template<class _key, class _data>
CBag<_key, _data>::~CBag(void)
{
}

template<class _key, class _data>
_data CBag<_key,_data>::Find(_key m_pElement)
{
if(Bag.count(m_pElement)!=0)
return (*Bag.find(m_pElement)).second;
else
return NULL;
}

template<class _key, class _data>
void CBag<_key,_data>::Add(_key m_pKey, _data m_pData)
{
Bag.insert(std::pair<_key, _data>(m_pKey, m_pData));
}


CBag<int, float> newBag;
newBag.Add(100, 5.0);


Okay, I don''t understand these errors.


Main.obj : error LNK2019: unresolved external symbol "public: __thiscall CBag::~CBag(void)" (??1?$CBag@HM@@QAE@XZ) referenced in function _main
Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CBag::Add(int,float)" (?Add@?$CBag@HM@@QAEXHM@Z) referenced in function _main
Main.obj : error LNK2019: unresolved external symbol "public: __thiscall CBag::CBag(void)" (??0?$CBag@HM@@QAE@XZ) referenced in function _main

Share this post


Link to post
Share on other sites
did you remember to include the template source at the end of your template header? That needs to be done since the compiler needs the actual definition of the methods and data members of the class to make the instantiation





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
Not with your compiler. In order to do that, you''d need to find one that implements the export keyword. As far as I know, only one compiler commercially available does: the Comeau C++ compiler.

Share this post


Link to post
Share on other sites
Assuming your compiler didn''t implement the export keyword, you have two options really. One is the inclusion method where you include the cpp file at the end of your .h header file and the other is explicit instantiation.

For the first method all you need to do is include the template .cpp file at the end of your template .h file so something like this:


#ifndef _CBAGS_H
#define _CBAGS_H
template <typename _key, typename _data>
class CBags
{
...
};//CBags


#include "cbags.cpp"
#endif


Using the first method your compiler will automatically create instances of classes w/ the required type from the template. The other way is to create these instances manually yourself but it also means you''ll need to keep track of it too on which ones you''ve instantiated. To use the second method you just need to add these extra lines at the end of your template .cpp file:


//cbags.cpp file

//all your template implementations up here


//put the classes w/ the datatypes that you need down here

//say you want an instance of a class using int, int as template

//parameters

template class CBags<int, int>;

//if you need a class that uses ints and chars for template

//parameter also

template class CBags<int, char>;

//and so on so forth



Using the second method you can separate the template .h and .cpp files and compile them as you normally do with class .h and .cpp files. The disadvantage of course is that you have to keep track of what you''ve instantiated so far and as your projects get bigger keeping track of this could be a problem.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
You need to use the single iterator form of erase(). Use find(), lower_bound() or equal_range() to get an iterator for the key, then iterate to find which one you want to delete.

Share this post


Link to post
Share on other sites