简体   繁体   English

是否可以将字符串重载为char元素的向量?

[英]is it possible to overload a string as a vector of char elements?

I have a class which acts on a list of elements, like so: 我有一个类作用于元素列表,如下所示:

template <typename T>
class MyClass {
    ...
};

I need to implement certain methods, that work both on a vector<T> and a string - which is what most of the users would be using when they initialise an object as MyClass<char>() . 我需要实现某些方法,这些方法同时适用于vector<T>string - 这是大多数用户在将对象初始化为MyClass<char>()时将使用的方法。

Due to this I am being forced to follow this overloading pattern: 由于这个原因,我被迫遵循这种重载模式:

void method(vector<T> elements){

}

void method(string elements){
    method(convertStringToVector(elements));
}

where vector<char> convertStringToVector(string str) is defined as expected. 其中vector<char> convertStringToVector(string str)定义为预期。

That is, for each class method I am being forced to add an additional method that overloads for string . 也就是说,对于每个类方法,我都被迫添加一个为string重载的附加方法。 I find it an unnecessary overhead, and also sometimes forget to add it. 我觉得这是一个不必要的开销,有时也会忘记添加它。

I will be doing exactly the same things on the string as I would do on the corresponding vector - same methods will be called in both cases, same iteration, etc. Thus, I was wondering if there is a cleaner way to do this, without adding much code overhead. 我将在字符串上完成与在相应向量上完全相同的事情 - 在两种情况下都会调用相同的方法,相同的迭代等等。因此,我想知道是否有更简洁的方法来做到这一点,没有添加很多代码开销。 Is there? 在那儿?

One possible way is to implement your method as a template, whose parameter is not restricted to be either string or vector<char> . 一种可能的方法是将您的方法实现为模板,其参数不限于stringvector<char>

template <typename T>
struct MyClass
{
    template <class C>
    void method(C container);
};

This solves your problem because one implementation is enough for both cases: 这解决了您的问题,因为一种实现对于两种情况都足够了:

template <typename T>
template <class C>
void MyClass<T>::method(C container)
{
    std::cout << "Container has size " << container.size() << '\n';
    std::cout << "First element is " << container[0] << '\n';
}

However, this will work on any container. 但是,这适用于任何容器。 It's not clear whether this is good (code is generic) or bad (code allows undesirable instantiations). 目前尚不清楚这是好的(代码是通用的)还是坏的(代码允许不合需要的实例化)。

Imagine what happens when people try to send vector<int> instead of vector<char> to your method by mistake. 想象一下当人们试图错误地将vector<int>而不是vector<char>发送到您的方法时会发生什么。 Because you didn't build your code for this case, it will either display obscure compilation errors, or generate code which silently does the wrong thing at runtime. 因为您没有为这种情况构建代码,所以它将显示模糊的编译错误,或生成在运行时静默执行错误操作的代码。

You can create a class to use as parameter to your methods, which accepts both std::vector<char> and std::string : 您可以创建一个类作为方法的参数,它接受std::vector<char>std::string

class MyString
{
public:
  MyString(const std::vector<char>& v) : vector(v) {}
  MyString(std::vector<char>&& v) : vector(std::move(v)) {}
  MyString(const std::string& s) : vector(convertStringToVector(s)) {}

  std::vector<char> vector;
};

void method(MyString elements)
{
}

You can define convertStringToVector as method of your class like this: 您可以将convertStringToVector定义为类的方法,如下所示:

vector<T> convertStringToVector(string str)

or a separate template function, if you need it independently for other classes. 或者单独的模板函数,如果您需要为其他类独立使用它。

Also I would pass the argument as reference const string &str . 我也将参数作为参考const string &str传递。

If any template parameter you use is incompatible with the algorithm, the compiler will tell you about. 如果您使用的任何模板参数与算法不兼容,编译器将告诉您。

For those special cases you can still write a template specialization like this: 对于这些特殊情况,您仍然可以编写如下模板专业化:

template<>
vector<SPECIALTYPE> convertStringToVector<SPECIALTYPE>(string str)
{
   ...
}

and implement different code for such SPECIALTYPE . 并为这种SPECIALTYPE实现不同的代码。

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

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