Reputation: 13
Currently trying to get a vector of names (strings) to print out in reverse. The reverse function will not be used, so it will be "manually done". So far with what I have, the vector prints forward fine, but when it comes to backwards - it is able to print the first 4 strings backwards fine, but then goes back to printing the rest forward. Have been sitting on it and just can't figure out how to fix it. Thanks.
#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 = 0, j = names.size() - 1; i < names.size(); i++, j--)
{
std::string temp = names[i];
names[i] = names[j];
names[j] = temp;
std::cout << names[i] << std::endl;
}
}
Upvotes: 1
Views: 4607
Reputation: 14392
The easiest way to iterate over a container back to front is to use the reverse iterators. This can be done with the std::for_each() function. (The forward loop can also be done with the range based for)
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<std::string> names { "Jeff", "Jim", "Jerry", "Lisa", "Terry", "Tim", "Tiff"};
std::cout << "This is the vector printing forwards\n";
std::for_each(names.begin(), names.end(), [](const std::string& name)
{
std::cout << name << std::endl;
});
std::cout << "This is the vector printing backwards\n";
std::for_each(names.rbegin(), names.rend(), [](const std::string& name)
{
std::cout << name << std::endl;
});
}
If you were planning to actually reverse the container manually, then you can swap up until half of the container as already mentioned in the other answers. You could also iterate the original container backwards and copy to a new container and replace the original container with the new one, but this is already close to using the std::reverse() function and it is probably slower.
std::copy(names.rbegin(), names.rend(), std::back_inserter(newNames));
Upvotes: 0
Reputation: 1
you have to keep swapping values till middle of the vector size.
cout << "This is the vector printing backwards\n";
for (int i = 0, j = names.size() - 1; i < names.size(); i++, j--)
{
if(i < names.size()/2)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
cout << names[i] << endl;
}
Upvotes: 0
Reputation: 5698
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;
}
}
Upvotes: 0
Reputation: 13456
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;
}
Upvotes: 4