[英]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. 例如,我应该创建一个following函数,该函数将执行此操作:如果我follows(q)
,其中q = {"h", "e", "l", "l", "o", "w", "r"}
并且q
的类型为ArrayQueue
,它将创建一个ArrayMap
,该ArrayMap
将单个队列值作为键,并在该队列值旁边保存一组值。
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>
尽管我了解执行此操作的一般逻辑,但我确实需要做一件事:能够访问和更新返回的ArrayMap
的第二个参数,即ics::ArraySet<T>
So how would I call this ArraySet
as a variable to do modifications on? 那么,如何将这个ArraySet
作为变量进行修改呢?
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). 假设您有一个变量the_array_map
,该变量保存您正在构造(并从该函数返回)的跟随者映射。
Now you can update followers
by appending the desired element to it. 现在,您可以通过将所需元素附加到followers
来更新followers
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.