It's because you're swapping twice for each element.
For a vector of size of 4: Swap operations:
0 3
1 2
2 1
3 0
Loop over the half size of the vector.
for (int i = 0, j = names.size() - 1; i < names.size()/2; i++, j--)
{
std::string temp = names[i];
names[i] = names[j];
names[j] = temp;
}
Print the vector using another loop.
for (int i = 0; i < names.size(); i++)
{
cout<<names[i]<<endl;
}
Answer from Kamol Hasan on Stack OverflowIt's because you're swapping twice for each element.
For a vector of size of 4: Swap operations:
0 3
1 2
2 1
3 0
Loop over the half size of the vector.
for (int i = 0, j = names.size() - 1; i < names.size()/2; i++, j--)
{
std::string temp = names[i];
names[i] = names[j];
names[j] = temp;
}
Print the vector using another loop.
for (int i = 0; i < names.size(); i++)
{
cout<<names[i]<<endl;
}
A C++ best practice is to NOT write raw loops.
Nevertheless, here is a version how you can achive it with the help of an index.
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> names{"Jeff", "Jim", "Jerry", "Lisa",
"Terry", "Tim", "Tiff"};
std::cout << "This is the vector printing forwards\n";
for (int i = 0; i < names.size(); i++) {
std::cout << names[i] << std::endl;
}
std::cout << "This is the vector printing backwards\n";
for (int i = names.size() - 1; i >= 0; i--) {
std::cout << names[i] << std::endl;
}
}
How do I reverse a C++ vector? - Stack Overflow
string reverse help
Vectors-- reversing an array - C++ Forum
Reverse an array without a temp variable?
Videos
There's a function std::reverse in the algorithm header for this purpose.
#include <vector>
#include <algorithm>
int main() {
std::vector<int> a;
std::reverse(a.begin(), a.end());
return 0;
}
All containers offer a reversed view of their content with rbegin() and rend(). These two functions return so-calles reverse iterators, which can be used like normal ones, but it will look like the container is actually reversed.
#include <vector>
#include <iostream>
template<class InIt>
void print_range(InIt first, InIt last, char const* delim = "\n"){
--last;
for(; first != last; ++first){
std::cout << *first << delim;
}
std::cout << *first;
}
int main(){
int a[] = { 1, 2, 3, 4, 5 };
std::vector<int> v(a, a+5);
print_range(v.begin(), v.end(), "->");
std::cout << "\n=============\n";
print_range(v.rbegin(), v.rend(), "<-");
}
Live example on Ideone. Output:
1->2->3->4->5
=============
5<-4<-3<-2<-1
void restring()// ask user for string, then reverse it and output
{
std::string instr, outstr; cout << "give me your favorite word: "; std::getline(std::cin, instr); std::vector<std::string>outstr = instr; std::reverse(outstr.begin(), outstr.end()); cout << endl << "your word forwards: " << instr << endl; cout << "your word backwards: " << outstr << endl;
}
This is one of a few functions in a code. I'm trying to get the user to input a string so I can copy it, then reverse the copy, then output both strings. the line "std::vector<std::string>outstr = instr;" is the only one throwing an error in the code before I run it. I don't know why, please help. Thanks.
Hey r/prog,
This is more of a trivia thing, I remember seeing how to reverse/sort an array with out using a temp var awhile back and saying "ohhh, why didn't I think of that before?" Well... now I'm back to that, except I can't find an example on how to it. I remember it being relatively simple, but I can't remember how to it.
Anyone care to enlighten me and anyone else who might fine themselves asking the same question? I don't care what language you do it in... although it might require pointer manipulation, I don't remember!