C++ rotate()排序函数用法详解(深入了解,一文学会)

        为了理解如何旋转序列,可以将序列中的元素想象成手镯上的珠子。rotate() 操作会导致一个新元素成为开始迭代器所指向的第一个元素。在旋转之后,最后一个元素会在新的第一个元素之前。

        rotate() 的第一个参数是这个序列的开始迭代器;第二个参数是指向新的第一个元素的迭代器,它必定在序列之内。第三个参数是这个序列的结束迭代器。图 1 中的示例说明在容器 ns 上的旋转操作使值为 4 的元素成为新的第一个元素,最后一个元素的值为 3。元素的圆形序列会被维持,因此可以有效地旋转元素环,直到新的第一个元素成为序列的开始。这个算法会返回一个迭代器,它指向原始的第一个元素所在的新位置。

     本文作者原创,转载请附上文章出处与本文链接。 

C++ rotate()排序函数用法详解目录

1 rotate

2 rotate_copy


1 rotate

#include <iostream>                                      // For standard streams
#include <iterator>                                      // For stream iterators and begin() and end()
#include <algorithm>                                     // For reverse_copy() and copy_if()
#include <cctype>                                        // For toupper() and isalpha()
#include <string>
#include <vector>

using namespace std;
using std::string;

int main()
{
    std::vector<string> words{ "one", "two", "three", "four", "five","six", "seven", "eight" };
    auto iter = std::rotate(words.begin(), words.begin() + 3, words.end());
    std::copy(words.begin(), words.end(), std::ostream_iterator<string> {std::cout, " "});
    std::cout << std::endl << "First element before rotation: " << *iter << std::endl;
}

这段代码对 words 中的所有元素进行了旋转。执行这段代码会生成如下内容:

four five six seven eight one two three
First element before rotation: one

输出说明 "four" 成为新的第一个元素,而且 rotate() 返回的迭代器指向之前的第一个元素"one"。

不需要对容器中的全部元素进行旋转。例如:

#include <iostream>                                      // For standard streams
#include <iterator>                                      // For stream iterators and begin() and end()
#include <algorithm>                                     // For reverse_copy() and copy_if()
#include <cctype>                                        // For toupper() and isalpha()
#include <string>
#include <vector>

using namespace std;
using std::string;

int main()
{
    std::vector<string> words{ "one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten" };
    auto start = std::find(std::begin(words), std::end(words), "two");
    auto end_iter = std::find(std::begin(words), std::end(words), "eight");
    auto iter = std::rotate(start, std::find(std::begin(words), std::end(words), "five"), end_iter);
    std::copy(std::begin(words), std::end(words), std::ostream_iterator<string>{std::cout, " "});
    std::cout << std::endl << "First element before rotation: " << *iter << std::endl;
}

2 rotate_copy

        rotate_copy() 算法会在新序列中生成一个序列的旋转副本,并保持原序列不变。rotate_copy() 的前 3 个参数和 copy() 是相同的;第 4 个参数是一个输出迭代器,它指向目的序列的第一个元素。这个算法会返回一个目的序列的输出迭代器,它指向最后一个被复制元素的下一个位置。

#include <iostream>                                      // For standard streams
#include <iterator>                                      // For stream iterators and begin() and end()
#include <algorithm>                                     // For reverse_copy() and copy_if()
#include <cctype>                                        // For toupper() and isalpha()
#include <string>
#include <vector>

using namespace std;
using std::string;

int main()
{
    std::vector<string> words{ "one", "two", "three", "four", "five","six", "seven", "eight", "nine","ten" };
    auto start = std::find(std::begin(words), std::end(words), "two");
    auto end_iter = std::find(std::begin(words), std::end(words), "eight");
    std::vector<string> words_copy;
    std::rotate_copy(start, std::find(std::begin(words), std::end(words), "five"), end_iter, std::back_inserter(words_copy));
    std::copy(std::begin(words_copy), std::end(words_copy), std::ostream_iterator<string> {std::cout, " "});
    std::cout << std::endl;
}

这段代码会对 word 中从 "two" 到 "seven" 的元素生成一个旋转副本。通过使用 back_insert_iterator 将复制的元素追加到 words_copy 容器中,back_insert_iterator 会调用 words_copy 容器的成员函数 push_back() 来插入每个元素。 

这里 rotate_copy() 返回的迭代器是 words_copy 中元素的结束迭代器。在这段代码中,并没有保存和使用它,但它却很有用。例如:

#include <iostream>                                      // For standard streams
#include <iterator>                                      // For stream iterators and begin() and end()
#include <algorithm>                                     // For reverse_copy() and copy_if()
#include <cctype>                                        // For toupper() and isalpha()
#include <string>
#include <vector>

using namespace std;
using std::string;

int main()
{


        std::vector<string> words{"one", "two" , "three", "four", "five","six", "seven", "eight", "nine", "ten"};
        auto start = std::find(std::begin(words) , std::end(words) ,"two");
        auto end_iter = std::find(std::begin(words) , std::end(words),"eight"); std::vector<string> words_copy {20}; // vector with 20 default elements
        auto end_copy_iter = std::rotate_copy(start,std::find(std::begin(words), std::end(words), "five"), end_iter, std::begin(words_copy));
        std::copy(std::begin(words_copy),end_copy_iter, std::ostream_iterator<string>{std::cout," "});
        std::cout << std::endl;
}

生成的 words_copy 容器默认有 20 个元素。rotate_copy() 算法现在会将现有元素的旋转序列保存到 words_copy 中。在输出时,这个算法返回的迭代器可以用来确定 words_copy 的尾部边界;如果没有它,就必须通过源序列的元素个数来计算出尾部边界。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双子座断点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值