简体   繁体   English

如何在模板类C ++中使用具有多个参数的模板函数

[英]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.

相关问题 如何使用模板类的子面板来推断C ++中模板函数的模板参数? - how to use a sublass of a template class to deduce the template parameters of a template function in C++? 如何使用GTest测试具有多个模板参数的C ++模板类? - How to test C++ template class with multiple template parameters with GTest? C ++:如何在类成员中使用未命名的模板参数? - C++: How to use unnamed template parameters in class members? 当模板参数相同时,C ++优化类模板函数 - C++ optimise class template function when template parameters identical 在C ++ 11中使用多个模板参数的模板函数的专业化 - Specialization of a template function with multiple template parameters in C++ 11 如何使用gtest测试带有多个模板参数的c ++模板类? - How to test c++ template class with multiple template parameters using gtest? c++ - 如何在 class 模板的纯虚拟 function 中使用迭代器? - c++ - How to use iterators in a pure virtual function of a class template? C ++模板实例化函数模板参数 - C++ template instantiation of function template parameters C ++-如何在不多次定义模板的情况下实现模板类的多种功能? - C++ - How can I implement multiple function of a template class without defining the template multiple times? 如何在C ++中的另一个模板类中使用模板类 - How to use a template class in another template class in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM