按地址

时间:2017-09-11 15:59:40

标签: c++ stl iterator

我定义了矢量:

std::vector<int> Numbers;

另外,我定义了一个迭代器,并将它初始化为向量的开头:

std::vector<int>::iterator numIt=Numbers.begin();

我使用该迭代器通过for循环传递向量:

for(; numIt!=Numbers.end(); ++numIt) {
    if(func(Numbers,numIt)==true){
       return SUCCESS;
    }
}

...其中func是以下函数:

bool func(std::vector<int> Numbers.end, std::vector<int>::iterator curElement){
  std::vector<int>::iterator nextElement=curElement;
  ++nextElement;
  if(nextElement== Numbers.end()){
    return false;
  }
  int ICurElement=*curElement;
  int InextElement=*nextElement;
  return ICurElement>InextElement ? true : false;
}

为什么我永远不会进入if中的func

完整代码

bool CurGreaterNext(std::vector<int>& set,std::vector<int>::iterator& curElement ,int size){
std::vector<int>::iterator nextElement=curElement;
  if(size == 1){
    return false;
  }
  ++nextElement;
  if(nextElement==set.end()){
    std::cout<< " im hereee "<<std::endl;
  }

  int ICurElement=*curElement;
  int InextElement=*nextElement;
  return ICurElement>InextElement ? true : false;
}

template<class TheSet , class Predicate >
void funkiFun(TheSet set , Predicate func){
  typename TheSet::iterator end=set.end();
  int count=0;
  for(typename TheSet::iterator it = set.begin(); it != end ; it++){
    if(func(set,it,set.size()-count)==true){
      std::cout << "Wo-Ho the number is "<< *it << std::endl;
      break;
    }
    count++;
  }
  std::cout <<  std::endl;
};

主要

 int main{
    int a1 = 1;
      int a2 = 2;
      int a3 = 3;
      int a4 = 4;
      int a5 = 5;
    std::vector<int> Numbers;
    Numbers.push_back(a1 );
      Numbers.push_back(a2 );
      Numbers.push_back(a3 );
      Numbers.push_back(a4 );
     Numbers.push_back(a5 );
    funkiFun<std::vector<int> >(numbers5,CurGreaterNext);
    }

3 个答案:

答案 0 :(得分:2)

我正在回答显示一种调试方式。我希望你能学到它。

我做了一些修改,所以你的代码会编译。我还添加了一些“std :: cout”来查看最新发生的事情(您可以使用的最简单的调试工具)。

这是代码:

#include <iostream>
#include <vector>

bool CurGreaterNext(std::vector<int>& set,std::vector<int>::iterator& curElement ,int size){
std::vector<int>::iterator nextElement=curElement;
  std::cout <<"CurGreaterNext() with size= " <<size <<" (set.size()= " <<set.size() <<") nextElement *it= " <<*nextElement <<"\n";
  if(size == 1){
    std::cout <<"in CurGreaterNext() and size=1\n";
    return false;
  }
  ++nextElement;
  if(nextElement==set.end()){
    std::cout<< " im hereee "<<std::endl;
  }
  int ICurElement=*curElement;
  int InextElement=*nextElement;
  std::cout <<"ICurElement= " << ICurElement <<"  InextElement= " << InextElement <<"\n";
  return ICurElement>InextElement ? true : false;
};

template<class TheSet , class Predicate >
void funkiFun(TheSet set , Predicate func){
  typename TheSet::iterator end=set.end();
  int count=0;
  std::cout <<"at funkiFun\n";
  for(typename TheSet::iterator it = set.begin(); it != end ; it++){
    std::cout << "inside loop with count= " << count <<"\n";
    if(func(set,it,set.size()-count)==true){
      std::cout << "Wo-Ho the number is "<< *it << std::endl;
      break;
    }
    count++;
  }
  std::cout <<  std::endl;
}

int main(){
    int a1 = 1;
      int a2 = 2;
      int a3 = 3;
      int a4 = 4;
      int a5 = 5;
    std::vector<int> Numbers;
    Numbers.push_back(a1 );
      Numbers.push_back(a2 );
      Numbers.push_back(a3 );
      Numbers.push_back(a4 );
     Numbers.push_back(a5 );
    funkiFun<std::vector<int>>(Numbers,CurGreaterNext);
    }

这是输出:

at funkiFun
inside loop with count= 0
CurGreaterNext() with size= 5 (set.size()= 5) nextElement *it= 1
ICurElement= 1  InextElement= 2
inside loop with count= 1
CurGreaterNext() with size= 4 (set.size()= 5) nextElement *it= 2
ICurElement= 2  InextElement= 3
inside loop with count= 2
CurGreaterNext() with size= 3 (set.size()= 5) nextElement *it= 3
ICurElement= 3  InextElement= 4
inside loop with count= 3
CurGreaterNext() with size= 2 (set.size()= 5) nextElement *it= 4
ICurElement= 4  InextElement= 5
inside loop with count= 4
CurGreaterNext() with size= 1 (set.size()= 5) nextElement *it= 5
in CurGreaterNext() and size=1

如您所见,您的if(nextElement==set.end())永远不会被调用,因为先前的比较if(size == 1)之前会返回。

答案 1 :(得分:0)

原因是你的代码在外循环中递增计数,并从size()中减去它,并将其传递给你的函数。当你在最后一个项目上时,大小(在你的谓词中)是1,并且它在到达你希望达到的代码之前返回。

为什么你首先要考虑到这种奇怪的大小和数量呢?

代码建议: 从谓词中删除count,size()和参数。 使用auto而不是从向量中声明迭代器 简化您的代码:

int ICurElement=*curElement;
int InextElement=*nextElement;
return ICurElement>InextElement ? true : false;

可能是:

return *curElement > *nextElement;

在你的谓词中,在测试nextElement是否结束之后,如果是,你不能在函数中稍后取消引用它。也许应该是返回false而不是使用&#34; size&#34;争论,这很奇怪。

答案 2 :(得分:-1)

dtms = []
temps = []

for row in reader:
    # split row into its 4 parts
    month, day, hour, temperature = row

    # concatenate columns into string
    # assuming the current year is the correct year, you may need to add additional logic to get the correct year
    # also assumes dateparts are properly zero-padded
    dtm = "{}-{}-{} {}".format(datetime.now().year, month, day, hour)

    # convert created string into datetime object
    dtm = datetime.strptime(dtm, "%Y-%m-%d %H:%M")

    # add datetime to list
    dtms.append(dtm)

    # add temperature to list
    temps.append(float(temperature))

在for循环中,您将(for (; numIt != set.end(); ++numIt) { if (func(Numbers, numIt) == true) { return SUCCESS; } } =设置为numIt,而不是比较(Numbers::end())。