你能帮我理解为什么这段代码不能编译?我想了解C++模板。功能模板
#include <iostream>
#include <algorithm>
#include <vector>
template <class myT>
void myfunction (myT i)
{
std::cout << ' ' << i;
}
int main()
{
double array1[] = {1.0, 4.6, 3.5, 7.8};
std::vector<double> haystack(array1, array1 + 4);
std::sort(haystack.begin(), haystack.end());
std::cout << "myvector contains:";
for_each (haystack.begin(), haystack.end(), myfunction);
std::cout << '\n';
return 0;
}
如果不能编译,至少出错。 – chris 2013-03-09 03:17:33
另外,您可能只想包含“using namespace std;”所以你不必继续写“std ::” – jrubins 2013-03-09 03:19:09
@jrubins,这通常被认为是[坏习惯](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-考虑 - 一个坏实践,在-C)。 – chris 2013-03-09 03:20:33