Let's assume that I have a function that prints a set of numbers: 1, 2, 3, 4, 5
and these numbers can either be stored as an array, or, as a vector. In my current system I therefore have two functions that accept either of these parameters.
void printNumbers(std::vector<double> &printNumbers)
{
//code
//....
}
And therefore one that accepts an array..
void printNumbers(int* numbers)
{
//code
//...
}
This seems a waste of code, and, I was thinking that I could better take advantage of code re-use which got me thinking to this: Can I use a template to determine which type of input is being passed to the function? For example, whether it's a vector
or an array
or just a single integer value?
Here is the prototype below:
#include <iostream>
using namespace std;
template<class T>
void printNumbers(T numbers)
{
// code
// code
}
int main(int argc, char *argv[]) {
int numbers[] = {1, 2, 3, 4, 5};
printNumbers<array> (numbers);
}
Any help would be greatly appreciated.
The usual idiom is to pass iterators, one for the first element of the range, and one corresponding to "one past the end":
template<class Iterator>
void printNumbers(Iterator begin, Iterator end)
{
for (Iterator i = begin; i != end; ++i)
std::cout << *i << " ";
std::cout << "\n";
}
int main()
{
int numbers[] = {1, 2, 3, 4, 5};
printNumbers(numbers, numbers + 5);
printNumbers(std::begin(numbers), std::end(numbers); // C++11 version
std::vector<int> v{1,2,3,4,5};
printNumbers(v.begin(), v.end());
}
You could follow the example of the STL algorithms and accept an iterator range. Containers have their iterator types, and pointers can be used to iterate over arrays:
template <typename InputIterator>
void printNumbers(InputIterator start, InputIterator end) {
// print "*start", and iterate up to "end"
}
For convenience, you can overload this to accept containers and arrays directly:
template <typename Container>
void printNumbers(Container const & c) {
printNumbers(c.begin(), c.end());
}
template <typename T, size_t N>
void printNumbers(T (const & a)[N]) {
printNumbers(a, a+N);
}
In C++11 (or with your own begin
and end
functions) you can combine these:
template <typename Container>
void printNumbers(Container const & c) {
printNumbers(std::begin(c), std::end(c));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.