简体   繁体   中英

How to use a template function with multiple parameters in a template class C++

I'm trying to work on a question for a project in class where our data structures and methods are already pre-defined. It's my job to implement some functions that use these templates.

For example, I'm supposed to create a follows function, that does this: if I did follows(q) where q = {"h", "e", "l", "l", "o", "w", "r"} and q is of type ArrayQueue , it would create an ArrayMap that holds a single queue value as a key and a set of values that is next to that queue value.

If I looked inside the map, it would look like this:

Key -> Set Containing the Values
h -> set[e]
e -> set[l]
l -> set[l, o]
o -> set[w]
w -> set[r]

Now, I'm supposed to implement this function by using this template signature:

template<class T>
ics::ArrayMap<T,ics::ArraySet<T>> follows (const ics::ArrayQueue<T>& q) 
{
}

Although I understand the general logic behind how I would do this, I do need one thing: to be able to access and update the second parameter in the returned ArrayMap , which is ics::ArraySet<T>

So how would I call this ArraySet as a variable to do modifications on?

From your description it sounds like you should be able to do the following:

ics::ArraySet<T>& followers = the_array_map[x];

Assuming that you have a variable the_array_map which holds the follower map that you are constructing (and returning from the function).

Now you can update followers by appending the desired element to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM