簡體   English   中英

C ++在不同類型的向量上具有相同的功能

[英]C++ same function on vectors of different types

我有一個非常簡單的函數,可以將vector<double>打印到cout。 我有使用vector<int>的相同函數。 我可以用一個函數替換它們,如果可以的話,可以使用任何類型的向量?

void printv(vector<double> vec)
{
    copy(vec.begin(),vec.end(), ostream_iterator<double>(cout," "));
    cout << endl;
}

void printv(vector<int> vec)
{
    copy(vec.begin(),vec.end(), ostream_iterator<int>(cout," "));
    cout << endl;
}

萬一有人提出了專門用於打印任何類型矢量的解決方案,我在將矢量保存到文件的功能上也遇到了同樣的問題,所以重點是普遍的問題,而不是專門用於打印。

提前致謝!

當然。 這就是C ++的目的。

template<typename T>
void printv(vector<T> const& vec)
{
    copy(vec.begin(), vec.end(), ostream_iterator<T>(cout," "));
    cout << endl;
}

只要T是“可輸出流”的,它將起作用。

注意:我在簽名中添加了const&以避免復制。


現在,您可以更進一步:

template<typename Container>
void print(Container const& c)
{
    using std::begin;
    using std::end;
    using std::copy;

    copy(begin(c), end(c), std::ostream_iterator<typename Container::value_type>(cout, " "));
    cout << endl;
}

使它適用於所有標准容器,而不僅僅是矢量。

是的,使用模板:

template<typename T>
void printv(std::vector<T> const &vec)
{
    std::copy(vec.cbegin(),vec.cend(), ostream_iterator<T>(std::cout," "));
    std::cout << std::endl;
}

或者,您可以為std::vector定義模板重載的operator<< ,如下所示:

template<typename T>
std::ostream& operator<<(std::ostream &out, std::vector<T> const &v) {
  std::copy(v.cbegin(), v.cend(), std::ostream_iterator<T>(out, " "));
  out << std::endl;
  return out;
}

int main() {
  std::vector<int> iv {1, 2, 3, 4, 5};
  std::vector<double> dv {1.1, 1.2, 1.3, 1.4, 1.5};
  std::cout << iv << std::endl;
  std::cout << dv << std::endl;
}

現場演示

嘗試以下

#include <iostream>
#include <vector>
#include <cstring>

template <class T>

std::ostream & print( T &c, std::ostream &os = std::cout )
{
    for ( auto x : c ) os << x << ' ';
    os << std::endl;

    return os;
}

template <class T, size_t N>

std::ostream & print( T ( &a )[N], std::ostream &os = std::cout )
{
    for ( auto x : a ) os << x << ' ';
    os << std::endl;

    return os;
}

template <class T>

std::ostream & print( T *a, size_t n, std::ostream &os = std::cout )
{
    for ( auto p = a; p != a + n; ++p ) os << *p << ' ';
    os << std::endl;

    return os;
}

int main() 
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    double b[] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
    char s[] = "Hello zooombini";

    std::vector<int> v1( a, a + sizeof( a ) / sizeof( *a ) );
    std::vector<double> v2( b, b + sizeof( b ) / sizeof( *b ) );

    print( a );
    print( b );
    print( v1 );
    print( v2 );
    print( s, std::strlen( s ) );

    return 0;
}

輸出是

0 1 2 3 4 5 6 7 8 9 
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 
0 1 2 3 4 5 6 7 8 9 
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 
H e l l o   z o o o m b i n i

甚至可以添加一個重載函數

#include <iostream>
#include <vector>
#include <cstring>

template <class T>

std::ostream & print( T &c, std::ostream &os = std::cout )
{
    for ( auto x : c ) os << x << ' ';
    os << std::endl;

    return os;
}

template <class T, size_t N>

std::ostream & print( T ( &a )[N], std::ostream &os = std::cout )
{
    for ( auto x : a ) os << x << ' ';
    os << std::endl;

    return os;
}

template <class T>

std::ostream & print( T *a, size_t n, std::ostream &os = std::cout )
{
    for ( auto p = a; p != a + n; ++p ) os << *p << ' ';
    os << std::endl;

    return os;
}

std::ostream & print( const char *s, std::ostream &os = std::cout )
{
    return os << s << std::endl;
}

std::ostream & print( char *s, std::ostream &os = std::cout )
{
    return os << s << std::endl;
}

int main() 
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    double b[] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
    int *p = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    char s[] = "Hello zooombini";

    std::vector<int> v1( a, a + sizeof( a ) / sizeof( *a ) );
    std::vector<double> v2( b, b + sizeof( b ) / sizeof( *b ) );

    print( a );
    print( b );
    print( p, 10 ) << std::endl;

    print( v1 );
    print( v2 );
    print( s, std::strlen( s ) );
    print( s );

    delete []p;

    return 0;
}

輸出是

0 1 2 3 4 5 6 7 8 9 
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 
0 1 2 3 4 5 6 7 8 9 

0 1 2 3 4 5 6 7 8 9 
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 
H e l l o   z o o o m b i n i 
Hello zooombini

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM