Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to print a vector using template in C++

1 Answer

0 votes
#include <vector>
#include <string>
#include <iostream>

template <class T>
void print_vector(const std::vector<T>& v) {
    for (T n : v) {
        std::cout << n << "\n";
    }
    
    std::cout << "\n";
}
 
int main()
{
    std::vector<int> vecint = { 2, 8, 9, 12, 5, 17, 189 };
    std::vector<double> vecdouble = { 2.3, 0.04, 3.8, 12.01, 15.0 };
    std::vector<std::string> vecstring = { "c++", "vector", "template" };

    print_vector(vecint);
    print_vector(vecdouble);
    print_vector(vecstring);
}
  
      
      
/*
run:
      
2
8
9
12
5
17
189

2.3
0.04
3.8
12.01
15

c++
vector
template

*/

 



answered Dec 26, 2024 by avibootz

Related questions

3 answers 212 views
1 answer 121 views
1 answer 117 views
2 answers 157 views
1 answer 152 views
1 answer 148 views
1 answer 147 views
...