3

I have a vector of strings and I want to reverse the vector and print it, or simply put, print the vector in reverse order. How should I go about doing that?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hoomanium
  • 49
  • 1
  • 4
  • 1
    This is pretty basic stuff. Why didn't you Google this? – Eitan T Jun 13 '12 at 17:02
  • 2
    @EitanT: this is pretty basic stuff. Why doesn't SO cover it? ;-) – Steve Jessop Jun 13 '12 at 17:02
  • @SteveJessop not sure I'm following you :) – Eitan T Jun 13 '12 at 17:03
  • 2
    @EitanT: StackOverflow's ultimate mission is that whenever you Google any on-topic problem, the top result is a StackOverflow question for your problem, with a good answer. So it's fine for people to ask Google-able questions, just as long as they aren't dupes (and actually it's fine to ask dupes too, if for some reason SO's search hasn't thrown the dupe at you already when you typed in the question). – Steve Jessop Jun 13 '12 at 17:05
  • 1
    I'd argue that this is not a dupe. In the linked-to question, the goal is clear: to reverse a vector. In this question, however, the goal is unclear: does he want to reverse a vector, or print a vector in reverse order? – Robᵩ Jun 13 '12 at 17:09
  • @SteveJessop I see what you mean. But still _this_ question doesn't show any research effort, does it? Why bother putting any effort then? – Eitan T Jun 13 '12 at 17:10
  • 1
    @Robᵩ: Then it's two seperate questions, in which case, it needs to be seperated into two different posts. The first question, which I would consider the primary question (since it is first, and since it is in the title) is definitely a duplicate. – Benjamin Lindley Jun 13 '12 at 17:12
  • i thought the reason the type is specified as strings is because the strings should be reversed too. – braks Jan 20 '17 at 02:00

1 Answers1

13

If you want to print the vector in reverse order:

#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
#include <string>

std::copy(v.rbegin(), v.rend(), 
  std::ostream_iterator<std::string>(std::cout, "\n"));

If you want to reverse the vector, and then print it:

std::reverse(v.begin(), v.end());
std::copy(v.begin(), v.end(),
  std::ostream_iterator<std::string>(std::cout, "\n"));

If you want to create a reversed copy of the vector and print that:

std::vector<std::string> r(v.rbegin(), v.rend());
std::copy(r.begin(), r.end(),
  std::ostream_iterator<std::string>(std::cout, "\n"));

Finally, if you prefer to write your own loops instead of using <algorithm>:

void print_vector_in_reverse(const std::vector<std::string>& v){
  int vec_size = v.size(); 
  for (int i=0; i < vec_size; i++){ 
    cout << v.at(vec_size - i - 1) << " ";
  }
}

Or,

void print_vector_in_reverse(std::vector<std::string> v) {
  std::reverse(v.begin(), v.end());
  int vec_size = v.size();
  for(int i=0; i < vec_size; i++) {
    std::cout << v.at(i) << " ";
  }
} 

References:

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • I get these errors, do I have to include any specific directives? a3p2.cc: In function ‘void print_vector(std::vector >)’: a3p2.cc:73:6: error: ‘ostream_iterator’ is not a member of ‘std’ a3p2.cc:73:39: error: expected primary-expression before ‘>’ token – Hoomanium Jun 13 '12 at 17:08
  • @Hoomanium: You have to include ``, `` and `` for the functions mentioned in the answer. – KillianDS Jun 13 '12 at 17:10
  • I had but not . Adding it now... – Hoomanium Jun 13 '12 at 17:13
  • This is my current code: void print_vector(vector v){ int vec_size = v.size(); for (int i=0; i < vec_size; i++){ cout << v.at(i) << " "; } } It should print the elements in the vector in the order they appear. Whereas I'm looking for a code to print the element in the reverse order of what the above code is doing. I tried your solution, but the order of output was no different than my code. It wasn't reverse. Could you tell me where I'm wrong? – Hoomanium Jun 13 '12 at 17:21
  • @Hoomanium - No, I can't tell you where you're wrong without seeing a complete sample program (see http://sscce.org for an explanation). But I have edited my answer for your benefit. – Robᵩ Jun 13 '12 at 17:34