为“ type”做一个共同的功能和“类型”的向量使用C++

发布于 2025-02-10 01:45:15 字数 321 浏览 0 评论 0原文

我正在编写功能,并遇到了这种情况:

void doSomeThing(std::vector<someType> vec)
{
  for(element : vec)
  {
    // do Something different if it is in vector
  }
}

void doSomeThing(someType element)
{
  // do Something else if it was not a vector
}

我需要像上面所述的那样分开。我想知道是否有一种方法使用模板来推断这种类型并进行相应的处理?

I was writing functions and came across this situation:

void doSomeThing(std::vector<someType> vec)
{
  for(element : vec)
  {
    // do Something different if it is in vector
  }
}

void doSomeThing(someType element)
{
  // do Something else if it was not a vector
}

I need them to be separate like above stated. I was wondering if there was a way using templates to deduce the type and do the processing accordingly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宛菡 2025-02-17 01:45:15

好吧,是的,这是可能的。

例如,您可以做;

template<class someType>
void doSomeThing( const someType &obj )
{
      // do something assuming someType is not a vector of something
}

template<class someType>
void doSomeThing( const std::vector<someType> &obj )
{
      // do something different if a vector<someType> is passed
}

int main()
{
    int x = 24;
    std::vector<int> a{1,2,3};     // assuming C++11 and later

    doSomeThing(x);     //  will call a specialisation of the first form above
    doSomething(a);     //  will call a specialisation of the second form above
}

就个人而言,我会做一些不同的事情 - 而不是为矢量超载,而是对一对迭代器过载。例如,假设dosomething()是打印到std :: cout的功能

template<class someType>
void doSomeThing( const someType &obj )
{
      std::cout << obj;
}

template<class Iter>
void doSomeThing( Iter begin, Iter end)
{
    while(begin != end)
    {
       std::cout << *begin << ' ';
       ++begin;
    }
    std::cout << '\n';
}

,它将与任何标准容器中的迭代器一起使用(例如std ::的特殊性, vectorstd :: list)或其他任何可以提供有效迭代器的东西。

int main()
{
    int x = 24;
    std::vector<int> a{1,2,3};     // assuming C++11 and later
    std::list<double> b {42.0, 45.0};


    doSomeThing(x);     
    doSomething(std::begin(a), std::end(a));
    doSomething(std::begin(b), std::end(b));
}

Well, yes, it is possible.

For example, you could do;

template<class someType>
void doSomeThing( const someType &obj )
{
      // do something assuming someType is not a vector of something
}

template<class someType>
void doSomeThing( const std::vector<someType> &obj )
{
      // do something different if a vector<someType> is passed
}

int main()
{
    int x = 24;
    std::vector<int> a{1,2,3};     // assuming C++11 and later

    doSomeThing(x);     //  will call a specialisation of the first form above
    doSomething(a);     //  will call a specialisation of the second form above
}

Personally, I'd do it slightly differently though - instead of overloading for a vector, I'd overload for a pair of iterators. For example, assuming doSomething() is just function that prints to std::cout

template<class someType>
void doSomeThing( const someType &obj )
{
      std::cout << obj;
}

template<class Iter>
void doSomeThing( Iter begin, Iter end)
{
    while(begin != end)
    {
       std::cout << *begin << ' ';
       ++begin;
    }
    std::cout << '\n';
}

which will work with iterators from any standard container (e.g. specialisations of std::vector, std::list) or anything else that can provide a pair of valid iterators.

int main()
{
    int x = 24;
    std::vector<int> a{1,2,3};     // assuming C++11 and later
    std::list<double> b {42.0, 45.0};


    doSomeThing(x);     
    doSomething(std::begin(a), std::end(a));
    doSomething(std::begin(b), std::end(b));
}
歌入人心 2025-02-17 01:45:15

只需更改,您就可以这样做

#include <iostream>
#include <vector>

template <typename T>
void doSomeThing(std::vector<T> vec)
{
  for (element : vec)
  {
    // do Something different if it is in vector
  }
}

template <typename T>
void doSomeThing(T element)
{
  // do Something else if it was not a vector
}

With just little changes you can do so

#include <iostream>
#include <vector>

template <typename T>
void doSomeThing(std::vector<T> vec)
{
  for (element : vec)
  {
    // do Something different if it is in vector
  }
}

template <typename T>
void doSomeThing(T element)
{
  // do Something else if it was not a vector
}
友欢 2025-02-17 01:45:15

例如,您可以

template <class T>
void doSomeThing( const std::vector<T> &vec )
{
    for ( const auto &element : vec )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

template <class T>
void doSomeThing( const T &obj )
{
    for ( const auto &element : obj )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

在此处写一个演示程序。

#include <iostream>
#include <vector>
#include <type_traits>

template <class T>
void doSomeThing( const std::vector<T> &vec )
{
    std::cout << std::boolalpha
        << std::is_same_v<std::decay_t<decltype( vec )>, std::vector<T>>
        << '\n';

    for ( const auto &element : vec )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

template <class T>
void doSomeThing( const T &obj )
{
    std::cout << std::boolalpha
        << std::is_same_v<std::decay_t<decltype( obj )>, std::vector<T>>
        << '\n';
    for ( const auto &element : obj )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

int main() 
{
    std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    doSomeThing( v );

    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
    doSomeThing( a );
}

程序输出是

true
0 1 2 3 4 5 6 7 8 9 
false
0 1 2 3 4 5 6 7 8 9 

For example you can write

template <class T>
void doSomeThing( const std::vector<T> &vec )
{
    for ( const auto &element : vec )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

template <class T>
void doSomeThing( const T &obj )
{
    for ( const auto &element : obj )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

Here is a demonstration program.

#include <iostream>
#include <vector>
#include <type_traits>

template <class T>
void doSomeThing( const std::vector<T> &vec )
{
    std::cout << std::boolalpha
        << std::is_same_v<std::decay_t<decltype( vec )>, std::vector<T>>
        << '\n';

    for ( const auto &element : vec )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

template <class T>
void doSomeThing( const T &obj )
{
    std::cout << std::boolalpha
        << std::is_same_v<std::decay_t<decltype( obj )>, std::vector<T>>
        << '\n';
    for ( const auto &element : obj )
    {
        std::cout << element << ' ';
    }
    std::cout << '\n';
}

int main() 
{
    std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    doSomeThing( v );

    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
    doSomeThing( a );
}

The program output is

true
0 1 2 3 4 5 6 7 8 9 
false
0 1 2 3 4 5 6 7 8 9 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文