C++ C++;不同类型向量上的相同函数
我有一个非常简单的函数,可以将C++ C++;不同类型向量上的相同函数,c++,function,vector,types,C++,Function,Vector,Types,我有一个非常简单的函数,可以将向量打印到cout。我有相同的函数,取而代之的是vector。如果可能的话,我可以用一个函数来替换这些向量吗 void printv(vector<double> vec) { copy(vec.begin(),vec.end(), ostream_iterator<double>(cout," ")); cout << endl; } void printv(vector<int> vec) {
向量打印到cout。我有相同的函数,取而代之的是vector
。如果可能的话,我可以用一个函数来替换这些向量吗
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;
}
void printv(向量向量)
{
复制(vec.begin(),vec.end(),ostream_迭代器(cout,“”);
使用模板时不能是:
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;
}
使用模板选择“是”:
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;
}
< P>当然,C++就是这样的。
template<typename T>
void printv(vector<T> const& vec)
{
copy(vec.begin(), vec.end(), ostream_iterator<T>(cout," "));
cout << endl;
}
<> P>使它适用于所有标准容器,而不仅仅是向量。 < P>当然。C++就是这样的。
template<typename T>
void printv(vector<T> const& vec)
{
copy(vec.begin(), vec.end(), ostream_iterator<T>(cout," "));
cout << 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;
}
甚至可以再添加一个重载函数
#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;
}
试试下面的方法
#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;
}
甚至可以再添加一个重载函数
#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;
}
复制品可以省略anyway@0d0a为什么要依赖强权?在很多情况下,它不会(事实上,我认为任何情况下它都不会复制)不要依赖力量,只知道它可能会被忽略,这只是information@0d0a您能否给出一个省略副本的示例?在函数体中添加使用std::begin;使用std::end;
,添加功能:C样式数组支持和更容易扩展到自定义范围支持对象。(直接std::begin
调用更糟糕)在我看来,这是一种最佳做法。复制可能会被忽略anyway@0d0a为什么要依赖强权?在很多情况下,它不会(事实上,我认为任何情况下它都不会复制)不要依赖力量,只知道它可能会被忽略,这只是information@0d0a您能否给出一个省略副本的示例?在函数体中添加使用std::begin;使用std::end;
,添加功能:C样式数组支持和更容易扩展到自定义范围支持对象。(直接std::begin
调用更糟糕)在我看来,这是一种最佳做法。您可以使用begin/end
而不是a+sizeof(a)/sizeof(*a)
,这看起来很糟糕。@Bartek Banachewicz对于这样一个简单的代码,没有必要包含标题。所以你宁愿使用一个模糊的黑客解决方案,而不是包含一个标准的库标题?来吧。你可以使用开始/结束
而不是a+sizeof(a)/sizeof(*a)
,这看起来非常糟糕。@Bartek Banachewicz对于这样一个简单的代码,没有必要包含头。所以你宁愿使用一个晦涩难懂的黑客解决方案,而不是包含一个标准的库头?来吧。