C++ C++;迭代器做什么?

C++ C++;迭代器做什么?,c++,iterator,C++,Iterator,代码: vector&res; 向量::迭代器it=下限(w.begin(),w.end(),queryweight); while(it!=w.end()){ 重量*w=&(*it); 如果(w->重量>=60)断裂; res.推回(w); it++; } 我认为 LoeLyLange做了二进制搜索(?),所以C++代码最终会得到想要的权重吗?它从哪里开始和停止?在这种情况下,while循环做什么?谢谢 下限返回不小于第三个参数的元素的最低迭代器(即向量中的位置)-此处为查询权重。然后,wh

代码:

vector&res;
向量::迭代器it=下限(w.begin(),w.end(),queryweight);
while(it!=w.end()){
重量*w=&(*it);
如果(w->重量>=60)断裂;
res.推回(w);
it++;
}

我认为 LoeLyLange做了二进制搜索(?),所以C++代码最终会得到想要的权重吗?它从哪里开始和停止?在这种情况下,

while
循环做什么?谢谢

下限
返回不小于第三个参数的元素的最低迭代器(即向量中的位置)-此处为
查询权重
。然后,
while
循环遍历其余元素,直到到达
wight
大于或等于60的元素,将它们添加到向量
res
。我假设输入向量
w
已排序,否则此函数将没有多大意义

逐行:

  vector<weight *> &res;
  vector<weight>::iterator it = lower_bound(w.begin(), w.end(), queryweight);
  while(it != w.end()) {
      weight *w  = &(*it);
      if(w->weight >= 60) break;
      res.push_back(w);
      it++;
  }
//声明指向“weight”对象的指针向量,称为res。
//(我假设原问题中的“&”是一个错误。)
向量res;
//查找指向向量“w”中最低元素的迭代器
//使元素>=查询权重。
向量::迭代器it=下限(w.begin(),w.end(),queryweight);
//从该元素向前直到向量“w”的结尾
while(it!=w.end()){
//获取指向元素的指针。
重量*w=&(*it);
//如果此元素的“wight”属性大于等于60,请停止。
如果(w->wight>=60)断裂;
//将元素推到“res”向量上。
res.推回(w);
//移动到下一个元素。
it++;
}

@Stephen:排序的向量是从最低到最高的?或者相反?请看sbi对这个问题的评论。@Stephen,我认为最好是“weight”属性,拼写错误sorry@StephenJFTR,我不认为你对《代码》和矢量和Res;<代码>不会编译,因为需要初始化引用。@ SBI,看,为什么C++有这样一堆陌生的注释?所以messy@ladyfafa:请参阅我最近关于C++复杂性的咆哮。真的,相信什么,拿起初学者的C++书。SBI:谢谢你的评论,我真的做了一些谷歌搜索,检查C++参考,给我一些时间。plesae@ladyfafa
vector
是对对象进行加权的向量<代码>矢量是权重对象的矢量。
// Declare a vector of pointers to 'weight' objects, called res.
// (I assume here that the "&" in the original question was a mistake.)
vector<weight *> res;

// Find the iterator that points to the lowest element in vector 'w'
// such that the element is >= queryweight.
vector<weight>::iterator it = lower_bound(w.begin(), w.end(), queryweight);

// From this element forwards until the end of vector 'w'
while(it != w.end()) {
    // Get a pointer to the element.
    weight *w  = &(*it);
    // If the 'wight' property of this element is >= 60, stop.
    if(w->wight >= 60) break;
    // Push the element onto the 'res' vector.
    res.push_back(w);
    // Move to the next element.
    it++;
}