forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101-template_functions1.cpp
66 lines (54 loc) · 1.38 KB
/
101-template_functions1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <vector>
// template <typename type, int K>
template <typename type>
void print_vector(std::vector<type> const &V)
{
for (const auto &x : V)
std::cout << x << " ";
std::cout << std::endl;
}
// void print_vector(std::vector<int> const &V)
// {
// for (auto x : V)
// std::cout << x << " ";
// std::cout << std::endl;
// }
// void print_vector(std::vector<float> const &V)
// {
// for (auto x : V)
// std::cout << x << " ";
// std::cout << std::endl;
// }
// void print_vector(std::vector<std::string> const &V)
// {
// for (auto x : V)
// std::cout << x << " ";
// std::cout << std::endl;
// }
/*
write new function copying this by replacing T with your type
void print_vector(std::vector<T> const &V)
{
for (auto x : V)
std::cout << x << " ";
std::cout << std::endl;
}
int x = 6;
std::array<int, x>
*/
int main()
{
std::vector<int> V1{-6, 10, 17};
std::vector<float> V2{10.6, 11.6, 10.17};
std::vector<std::string> V3{"Pear", "Raspberry", "Pineapple"};
// Task: Write a new version of the functions below which can handle V3
print_vector<int>(V1);
print_vector<float>(V2);
print_vector<std::string>(V3); // TO DO
// print_vector<int, 4>(V1);
// print_vector<float, 1>(V2);
// print_vector<std::string, 40>(V3); // TO DO
return 0;
}