2014-09-26 35 views
1

我有一個非常簡單的功能,將vector<double>打印到cout。我有相同的功能,取而代之的是vector<int>。我可以用一個函數替換這些函數嗎,如果可能的話,可以使用任何類型的向量?C++在不同類型的向量上具有相同的功能

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; 
} 

,以防有人提出一個解決方案,專門用於打印任何類型的載體,我有同樣的問題用一個函數來向量保存到一個文件中,這樣的點是普遍的問題,而不是專門做與印刷。

在此先感謝!

回答

6

肯定。這就是C++的意義所在。

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

只要T是「output-streamable」就會工作。

注意:我已將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; 
} 

使其成爲所有標準集裝箱,而不僅僅是向量工作。

+0

副本可能無論如何都會被刪除 – 4pie0 2014-09-26 11:21:10

+4

@ 0d0a爲什麼依靠可能?在很多情況下它不會。 (事實上​​,我不能認爲任何情況下,它不會複製) – 2014-09-26 11:21:38

+0

不依賴於可能,只是知道它可能會被刪除,這只是信息 – 4pie0 2014-09-26 11:22:29

2

是通過使用模板:

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; 
} 

Alternativelly你可以定義模板重載operator<<std::vector象下面這樣:

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; 
} 

LIVE DEMO

-2

請嘗試以下

#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 
+0

你可以使用'begin/end'而不是'a + sizeof(a)/ sizeof(* a)',這看起來非常糟糕。 – 2014-09-26 11:30:01

+0

@Bartek Banachewicz對於這樣一個簡單的代碼,沒有必要包含頭文件。 – 2014-09-26 11:30:46

+0

因此,你寧願使用一個晦澀的hacky解決方案,而不是包含標準庫頭文件? *來吧。* – 2014-09-26 11:32:11

相關問題