简体   繁体   English

在没有反向 function 的情况下反转字符串向量

[英]Reversing a vector of strings without the reverse function

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".倒过来的function就不会用了,所以要“手动完成”。 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.到目前为止,我所拥有的向量可以很好地向前打印,但是当它向后打印时 - 它能够向后打印前 4 个字符串,但随后又可以向前打印 rest。 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;
    }
}

It's because you're swapping twice for each element.这是因为您为每个元素交换了两次。

For a vector of size of 4: Swap operations:对于大小为 4 的向量:交换操作:

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. C++ 最佳实践是不编写原始循环。

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;
    }
}

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;
    }

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.这可以通过 std::for_each() function 来完成。 (The forward loop can also be done with the range based for) (前向循环也可以用基于范围的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::reverse() function 并且它可能更慢。

std::copy(names.rbegin(), names.rend(), std::back_inserter(newNames)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM