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,845 questions

51,766 answers

573 users

How to print a vector in reverse order with C++

3 Answers

0 votes
#include <iostream>
#include <vector>
#include <iterator>
 
void print(std::vector<char> const &v) {
    std::copy(v.rbegin(),
            v.rend(),
            std::ostream_iterator<char>(std::cout, " "));
}
 
int main()
{
    std::vector<char> v = {'a', 'b', 'c', 'd'};

    print(v);
 
    return 0;
}




/*
run:

d c b a 

*/

 



answered Feb 19, 2021 by avibootz
0 votes
#include <iostream>
#include <vector>

void print(std::vector<char> const &v) {
    for (auto it = v.crbegin(); it != v.crend(); it++) {
        std::cout << *it << ' ';
    }
}
 
int main()
{
    std::vector<char> v = {'a', 'b', 'c', 'd'};

    print(v);
 
    return 0;
}




/*
run:

d c b a 

*/

 



answered Feb 19, 2021 by avibootz
0 votes
#include <iostream>
#include <vector>

void print(std::vector<char> const &v) {
    for (int i = v.size() - 1; i >= 0; i--) {
        std::cout << v.at(i) << " ";
    }
}
  
int main()
{
    std::vector<char> v = {'a', 'b', 'c', 'd'};
 
    print(v);
  
    return 0;
}
 
 
 
 
/*
run:
 
d c b a 
 
*/

 



answered Feb 19, 2021 by avibootz

Related questions

2 answers 248 views
1 answer 171 views
1 answer 110 views
1 answer 182 views
182 views asked Jul 22, 2020 by avibootz
1 answer 164 views
...