简体   繁体   English

如何使用以集合为参数的模板化客户端display()函数

[英]How to use a templated client display() function that takes a set as the parameter

I have to write a templated client function called DisplaySet() that gets as an argument a set, and displays the contents of the set. 我必须编写一个称为DisplaySet()的模板化客户端函数,该函数将一个集合作为参数,并显示该集合的内容。 I am confused on how I can output the set, which is part of a class, in a client function. 我对如何在客户端函数中输出作为类的一部分的集合感到困惑。 Here is my code: 这是我的代码:

"set.h" “set.h”

template<class ItemType>
class Set
{
 public:
  Set();
  Set(const ItemType &an_item);
  int GetCurrentSize() const;
  bool IsEmpty() const;
  bool Add(const ItemType& new_entry);
  bool Remove(const ItemType& an_entry);
  void Clear();
  bool Contains(const ItemType& an_ntry) const; 
  vector<ItemType> ToVector() const;
  void TestSetImplementation() const;

 private:
  static const int kDefaultSetSize_ = 6;
  ItemType items_[kDefaultSetSize_]; 
  int item_count_;                    
  int max_items_;                 
  int GetIndexOf(const ItemType& target) const;
};
template<class ItemType>
void DisplaySet(const Set<ItemType> &a_set);

"set.cpp" “set.cpp”

template<class ItemType>
void DisplaySet(const Set<ItemType> &a_set){
    int a_size = a_set.GetCurrentSize(); //gets size of the set
    cout <<"Size display "<< a_size << endl;
    for (int i = 0; i < a_size; i++) {
        cout << a_set[i] << endl; //i know this does not work because a_set is part of a class
    }
}

"main.cpp" “的main.cpp”

#include <iostream>
#include <vector>
#include <string>   
#include "Set.h"   

using namespace std;

int main()
{
   Set<int> b_set;

   b_set.Add(setArray[1]);
   b_set.Add(setArray[2]);
   b_set.Add(setArray[4]);
   b_set.Add(setArray[8]);
   DisplaySet(b_set);

   return 0;
}

I hope someone can explain how to use the function. 我希望有人可以解释如何使用该功能。 Let me know if I need to post more of the code 让我知道是否需要发布更多代码

Your Set class does not have an overloaded operator[] , so calling a_set[i] is not going to work in your DisplaySet function. 您的Set类没有重载的operator[] ,因此调用a_set[i]DisplaySet函数中将无法正常工作。

Assuming that your ToVector function returns a vector of the items in the set, the DisplayFuntion can look like this: 假设您的ToVector函数返回集合中各项的向量,则DisplayFuntion可能如下所示:

#include <iterator>
#include <algorithm>
#include <iostream>
//...
template<class ItemType>
void DisplaySet(const Set<ItemType> &a_set)
{
    std::vector<ItemType> v = a_set.ToVector();
    std::copy(v.begin(), v.end(), std::ostream_iterator<ItemType>(cout, "\n"));
}

Again, this is assuming that ToVector does as stated. 再次,这假设ToVector如所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 采用模板化长度参数的 Function - Function that takes templated length parameter 如何编写以通用迭代器为参数的模板化 function - How can one write a templated function that takes a generic iterator as a parameter 如何使用模板化 std::function 作为参数? - How to use templated std::function as parameter? 为模板化函数使用默认参数 - Use default parameter for templated function 如何将模板化函数参数的默认参数设置为函数指针? - How to set a default argument for a templated function parameter to a function pointer? 如何创建一个模板化函数,该函数采用作为 boost::bind 结果的参数? - How can I make a templated function that takes a parameter that is the result of boost::bind? 如何通过 C++ 中模板化类型的子类传递模板化 function 参数? - How to pass a templated function parameter with a subclass of the templated type in C++? 如何在捕获 lambda 参数时使用模板化 typedef? - How to use a templated typedef on capturing lambda parameter? 如何使用模板中的参数调用模板化函数? - How to call a templated function with a parameter in the template? 如何模拟一个带有模板化args的函数,并在c ++中对它们应用模板化函数? - How do I template a function that takes templated args and applies a templated function on them in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM