【发布时间】:2018-10-29 16:36:00
【问题描述】:
std::sort 函数无法对向量的特定部分进行排序。将要排序的向量部分的上限和下限与向量的大小和向量元素一起作为输入输入。这是我尝试过的:
#include<iostream>
#include <vector>
#include<algorithm>
using namespace std;
int main()
{
long long int N,L,R;
cin>>N;
vector <long long int> V;
while(N--)
{
long long int input;
cin>>input;
V.push_back(input);
}
cin>>L>>R;
sort(V.begin()+L-1,V.begin()+R-1);
vector<long long int>::iterator I = V.begin();
while(I<V.end())
{
cout << *I << " ";
I++;
}
cout << endl;
}
My input:
5
3 -1 4 2 -1
3 4
Expected output:
3 -1 2 4 -1
Actual output:
3 -1 4 2 -1 (unchanged)
请告知此方法中哪些不正确/可以应用哪些其他方法。
【问题讨论】:
-
欢迎来到 Stack Overflow!请edit您的问题与minimal reproducible example 或SSCCE (Short, Self Contained, Correct Example)
-
您好,请发布您的实际代码,而不是图片。要了解它有什么问题,我们必须复制它并进行测试。这是图片无法做到的。
-
@OP -- 创建图像、将其保存为 png、链接到它比简单地获取文本并将其复制到编辑窗口要多得多。你为什么要做所有这些不必要的工作来发布你的程序?
-
对于您的程序,您根本不检查
L和R是什么,但期望std::sort可以使用这些值。std::sort没有任何问题如果你提供了正确的参数。 -
尊敬的先生,L和R是用户自己输入的,恐怕我看不懂。这也是我第一次提问,给您带来的不便,我深表歉意。