Template Help: Map<T, int> and Set<T>

S

Stuart Moore

Hi, I'm quite new to templates and I seem to be getting myself messed
up. I want to write a function that takes a map<T, int> and a set<T>,
iterates over the set, and increments the corresponding int in the map
(or sets it to 1 if it doesn't already exist).

I can write the function itself for a given type, but I can't figure out
what template statement(s) I need to put before it to make it
generalise. Stuff I've tried unsuccessfully is below.

Any help much appreciated; please reply to the group as email address is
invalid.


template <typename T>
void increment(map<T, int>& theMap, const set<T> theSet){
...
}


template <typename T, template <T, int> class map, template <T> class set>
void increment(map<T, int>& theMap, const set<T> theSet){
...
}
 
V

Victor Bazarov

Stuart said:
Hi, I'm quite new to templates and I seem to be getting myself messed
up. I want to write a function that takes a map<T, int> and a set<T>,
iterates over the set, and increments the corresponding int in the map
(or sets it to 1 if it doesn't already exist).

I can write the function itself for a given type, but I can't figure
out what template statement(s) I need to put before it to make it
generalise. Stuff I've tried unsuccessfully is below.

Any help much appreciated; please reply to the group as email address
is invalid.


template <typename T>
void increment(map<T, int>& theMap, const set<T> theSet){

Pass the set by [const] reference.
...
}


template <typename T, template <T, int> class map, template <T> class
set> void increment(map<T, int>& theMap, const set<T> theSet){
...
}

This compiles for me (with proper includes):

template<class T> void increment_or_assign(std::map<T,int> &m,
std::set<T> const& s)
{
// whatever...
}

V
 
S

Stuart Moore

This compiles for me (with proper includes):

template<class T> void increment_or_assign(std::map<T,int> &m,
std::set<T> const& s)
{
// whatever...
}

Thank you. It seems my problem is different to what I thought in that
case. As you say,

template <typename T>
void increment(map<T, int>& theMap, const set<T>& theSet){
};

compiles, but

template <typename T>
void increment(map<T, int>& theMap, const set<T>& theSet){
set<T>::const_iterator itr;
};

Doesn't - instead I get

error: expected `;' before "itr"

Replacing T with a specific class all the way through compiles again.

How should I be obtaining an iterator for the set?

Stuart
 
J

Jonathan Mcdougall

Stuart said:
template <typename T>
void increment(map<T, int>& theMap, const set<T>& theSet){
set<T>::const_iterator itr;

typename set said:
};

Doesn't - instead I get
error: expected `;' before "itr"
Replacing T with a specific class all the way through compiles again.
How should I be obtaining an iterator for the set?

Because T is a template argument, the compiler has no way, when it
parses this statement, to know what it is. That means it cannot know
what "const_iterator" is in this context (std::set could be specialized
for a given T and that specialization could have no member "iterator"
or "iterator" could even be an object and not a type). By adding
"typename", you specify the compiler that "iterator" is a type name
inside std::set. It'll double check later.


Jonathan
 
V

Victor Bazarov

Stuart said:
Thank you. It seems my problem is different to what I thought in that
case. As you say,

template <typename T>
void increment(map<T, int>& theMap, const set<T>& theSet){
};

compiles, but

template <typename T>
void increment(map<T, int>& theMap, const set<T>& theSet){
set<T>::const_iterator itr;

Read about dependent names. And then add 'typename' before this
declaration.
};

Doesn't - instead I get

error: expected `;' before "itr"

Replacing T with a specific class all the way through compiles again.

How should I be obtaining an iterator for the set?

You almost got it right.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top