std::vector<string> words { "one", "two", "three", "four", "five","six", "seven", "eight"}; auto iter = std::rotate(std::begin(words), std::begin(words)+3, std::end(words)); 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;这段代码对 words 中的所有元素进行了旋转。执行这段代码会生成如下内容:
four five six seven eight one two three
First element before rotation: one
std::vector<string> words { "one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten"}; auto start = std::find(std:rbegin(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;这里用 find() 算法分别获取了和"two"、"eight"匹配的元素的迭代器。它们定义了被旋转的序列,这个序列是容器元素的子集。这个序列会被旋转为使"five"成为第一个元素,输出说明它是按预期工作的:
one five six seven two three four eight nine ten
First element before rotation: two
Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有