c – 迭代对的向量
发布时间:2020-12-16 10:34:57 所属栏目:百科 来源:网络整理
导读:我写了下面的代码片段,但它似乎没有工作. int main(){ int VCount,v1,v2; pairfloat,pairint,int edge; vector pairfloat,int edges; float w; cin VCount; while( cin v1 ){ cin v2 w; edge.first = w; edge.second.first = v1; edge.second.second = v2; e
我写了下面的代码片段,但它似乎没有工作.
int main(){ int VCount,v1,v2; pair<float,pair<int,int> > edge; vector< pair<float,int> > > edges; float w; cin >> VCount; while( cin >> v1 ){ cin >> v2 >> w; edge.first = w; edge.second.first = v1; edge.second.second = v2; edges.push_back(edge); } sort(edges.begin(),edges.end()); for ( vector < pair<float,int>> >::const_iterator it = edges.begin() ; itt != edges.end; it++){ cout >> it.first; } return 0; } 它在包含for循环的行中抛出错误.错误是: error: no match for ‘operator<’ in ‘it < edges.std::vector<_Tp,_Alloc>::end [with _Tp = std::pair<float,std::pair<int,int> >,_Alloc = std::allocator<std::pair<float,int> > >,std::vector<_Tp,_Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const std::pair<float,int> >*,std::vector<std::pair<float,int> > > >,typename std::_Vector_base<_Tp,_Alloc>::_Tp_alloc_type::const_pointer = const std::pair<float,int> >*] 谁能帮我吗? 解决方法
循环中至少有三个错误.
for ( vector < pair<float,int>> >::const_iterator it = edges.begin() ; itt != edges.end; it++){ cout >> it.first; } 首先,你必须使用edges.end()而不是edges.end.身体内部必须有 cout << it->first; 代替 cout >> it.first; 要逃避这些错误,你可以简单地写一下 for ( const pair<float,int> > &edge : edges ) { std::cout << edge.first; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |