Universal iterator both for vectors and arrays

Daer experts,
how can I make this code to work both for vectors and arrays?

Many thanks and kind regards.
- Mauro.


/*
c++ -o testAuto testAuto.cpp -std=c++11
*/

#include <iostream>
#include <string>
#include <vector>

template<typename T>
void myGenericIterator (T& myIterable)
{
for (auto ele : myIterable)
std::cout << ele << std::endl;
}

int main()
{
std::string myArray[] = {"Giulia", "Alessandro", "Pietro"};

std::vector<std::string> myVec;
myVec.push_back("Giulia");
myVec.push_back("Alessandro");
myVec.push_back("Pietro");

std::cout << std::endl;
myGenericIterator<std::vector<std::string>>(myVec);

std::cout << std::endl;
// myGenericIterator<std::string(&)[3]>((&myArray)[3]);

return 0;
}
Just let the compiler deduce the template argument type and it will work fine.

1
2
myGenericIterator(myVec);
myGenericIterator(myArray);
Topic archived. No new replies allowed.