问题遇到的现象和发生背景
编程题目:
编写一个程序,命名为findKthStr
,找出输入的字符串中所有长度从小到大排第k个的的字符串,按出现顺序输出所有满足条件的字符串(用换行符分隔),若没有则什么也不输出。
第一行输入为即将输入的字符串个数n
第二行输入为参数k
之后n行为输入的字符串
示例
输入:
5
2
Hello
World
OOP
xs
oh
输出:
OOP
问题相关代码,请勿粘贴截图
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(string a, string b)//用于从小到大对str_vec排序
{
return a.length()<b.length();
}
typedef vector<string> StrVec;
//找出输入的字符串中所有长度从小到大排第k个的的字符串,按出现顺序输出所有满足条件的字符串(字符串长度相同时k不变)
StrVec FindKthStr(StrVec str_vec, int k)
{
StrVec::size_type size = str_vec.size();
sort(str_vec[0],str_vec[size-1],compare);
int flag=1,count=0;
int i;
for(int i=0;i<size-1;i++)
{
if(str_vec[i].size()<str_vec[i+1].size())
flag++;
if(flag==k)
{
cout<<str_vec[i]<<endl;
count=i;
}
}
if(count)//count+1用于输出最后一个排第k个的字符串
cout<<str_vec[count+1];
}
int main()
{
StrVec str_vec;
int n,k;//n为输入字符串的个数,k为输出第k个字符串
string s;
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>s;
str_vec.push_back(s);
}
FindKthStr(str_vec,k);
return 0;
}
运行结果及报错内容
进行编译时会报错error: no match for 'operator-' (operand types are 'std::__cxx11::basic_string' and 'std::__cxx11::basic_string')
std::__lg(__last - __first) * 2
请问如何解决