473,549 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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){
...
}
Jun 6 '06 #1
4 2626
Stuart Moore wrote:
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_as sign(std::map<T ,int> &m,
std::set<T> const& s)
{
// whatever...
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 6 '06 #2
On 06/06/06 17:56, Victor Bazarov wrote:

This compiles for me (with proper includes):

template<class T> void increment_or_as sign(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_i terator 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
Jun 7 '06 #3
Stuart Moore wrote:
template <typename T>
void increment(map<T , int>& theMap, const set<T>& theSet){
set<T>::const_i terator itr;
typename set<T>::const_i terator 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?


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

Jun 7 '06 #4
Stuart Moore wrote:
On 06/06/06 17:56, Victor Bazarov wrote:

This compiles for me (with proper includes):

template<class T> void increment_or_as sign(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_i terator 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1424
by: mcassiani | last post by:
Hi, I need use map faster as possible (I store in the map data about open network connections). First a question, this code fragment is from "The C++ Programming........
14
1805
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
10
2949
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The...
2
2238
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include...
4
2305
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef Tparent::Tptr Tptr; Consider example:
3
2932
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I suppose it's supported and should behave well when I used it correctly. BUT it didn't. Here is my code snippet: class MyKey { public: virtual...
2
1577
by: DaTurk | last post by:
Hi, I'm trying to hold a map of ints,and function pointers in C++ map<int, (*functPtr)(int, int)something I need to hold a list of callbacks. For some reason this syntax is not working. Any ideas?
12
5811
by: jabbah | last post by:
Actually I'm quite sure I've missed something trivial here, but I just can't find it. Seemingly I cannot read from a const map& I try #include <iostream> #include <map> using namespace std;
2
2771
by: jabbah | last post by:
I have some data in a map and I want to sort it. Currently I have implemented it like this: #include <iostream> #include <map> #include <string> using namespace std; int main(){
6
5412
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.