如何修复我的C++代码?(操作员无法匹配)



如何修复C++代码?(操作员无法匹配(我得到了一个错误:没有匹配";运算符-";。问题出在哪里?我该如何解决?有人能帮我修吗?

错误:与"operator-"不匹配(操作数类型为"std::set::迭代器"{aka"std::Rb_tree_const_iterator"}和"std:;set::iterator"{又名"std:"_Rb_tree_const _iterator"}(|

#include <iostream>
#include <set>
using namespace std;
int main()
{
int O;
int N;
int M;
cin >> O >> N >> M;
int tanarsorszama[O];
int tantargysorszama[O];
int nap [O];
int ora [O];
for(int i=0; i<O; i++)
{
cin >> tanarsorszama[i] >> tantargysorszama[i] >> nap[i] >> ora[i];
}
int dblyukas[N]={0};
set<int>orai; 
for(int i=0; i<N; i++)
{
for(int k=1; k<6; k++)
{
orai.clear();  
for(int j=0; j<9; j++)
{
if(tanarsorszama[i]!=0 && ora!=0 && nap[k]!=0)
{
orai.insert(ora[j]);   
}
}
if (orai.size() > 1) 
{
dblyukas[i] += orai.end() - orai.begin() +1 - orai.size();  // There is the error
}
}
}
return 0;
}

std::set没有随机访问迭代器。这就是为什么你会出错。

要访问集合的第一个和最后一个元素,请使用orai.begin()std::prev(orai.end())。这些返回迭代器,必须使用运算符*对其进行反引用。因此,纠正我认为你打算做的事情,会导致以下结果:

if (orai.size() > 1) 
{
dblyukas[i] += *std::prev(orai.end()) - *orai.begin() +1 - orai.size();  // There is the error
}

最新更新