我想使用 C++20 中将向量向右移动的新功能std::shift_right。
我不确定的是如何在移位后将最后一个元素置于向量的开头?
就像上面的例子一样......我将最后一个元素保存在一个临时变量中,然后在移动之后我将第一个元素设置为临时变量。
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> seq = { 5, 4, 3, 2, 1 };
std::vector<int>::iterator it;
std::vector<int>::iterator temp;
temp = seq.end() - 1;
std::shift_right(seq.begin(), seq.end(), 1);
std::replace(seq.begin(), seq.end(), seq.at(0), *temp);
for (it = seq.begin(); it != seq.end(); it = std::next(it))
std::cout << *it << " ";
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个基类,其中有一个纯虚函数,并且使用这个函数,我想在其他派生类中覆盖它(如果可能,在某些派生类中使用不同数量的参数)。
所以在 MergeSort 子类中,我有 MSort 方法,它需要不同数量的参数,因为它是递归完成的。
因此,目前使用这些参数使用此函数时,我收到此错误“MergeSort:”无法实例化抽象类。但是,如果我覆盖基类中的 Sort 方法工作正常,但我不需要一个参数。
我还尝试用不同数量的参数声明另一个虚函数,并在 MergeSort 类中定义它,我得到了同样的结果。
我还想澄清一下,对于不同的算法(冒泡排序、插入排序等),我还有其他子类,它们的实现类似于 MergeSort(一个构造函数和一个排序函数),但排序函数具有相同的参数数量(只有一个用于图形界面)就像在上面的基类中一样。
那么是否有可能有一个具有不同数量参数的覆盖方法?或者我上面所说的任何其他解决方案?
// BASE CLASS
// Forward declaration
class Interface;
/**
* Base class from which the sorting algorithms classes will inherit (Polymorphic class)
* The base class will allow us to create a sequence with n elements
*/
class SortingAlgorithms
{
protected:
std::vector<sf::RectangleShape> sequence; // vector which will contain a randomized sequence
std::vector<sf::RectangleShape> sequenceCpy; // a copy of sequence used for interaction features
sf::RenderWindow& window; // …Run Code Online (Sandbox Code Playgroud) 例如,我有一个二维向量和一个一维向量,我想使用算法库中的std::copy_if从每一行复制第一项:
std::vector<std::vector<std::string>> names{ { "Allan", "Daniel", "Maria" }, {"John", "Louis", "Will"}, {"Bill", "Joe", "Nick"}};
Run Code Online (Sandbox Code Playgroud)
因此,我们必须将“Allan”、“John”和“Bill”复制到目标向量中,即 dst
std::vector<std::string> dst;
Run Code Online (Sandbox Code Playgroud)