使用迭代器在std :: list中获取Next Element

时间:2017-11-01 05:34:15

标签: c++ stl

我试图在std :: list中的连续元素之间找到区别。我尝试了以下解决方案,但正如this线程所说,我需要复制迭代器并递增它。不确定它是什么意思,因为向迭代器添加数字也会导致错误。我在这里缺少什么我使用的是以前版本的C ++但不是C ++ 11

#include "stdafx.h"
#include <list>
#include <iostream>
using namespace std;

int main()
{
    std::list<int> myList;
    //for (int i = 10;i < 15;i++)
    myList.push_back(12);
    myList.push_back(15);
    myList.push_back(18);
    myList.push_back(19);
    myList.push_back(25);

    for (std::list<int>::const_iterator itr = myList.begin();itr != myList.end();++itr)
    {
        int x = *itr;
        int y = *(itr + 1);
        int diff = std::abs(x - y);
        cout << diff << "\n";
    }

    return 0;
}

7 个答案:

答案 0 :(得分:3)

如何使用adjacent_difference

std::adjacent_difference(myList.begin(), myList.end(), tempList );

See Here

如果您对实施感兴趣,请查看其在附件链接的Possible Implementation部分中的实施情况。您所要做的就是用list iterator替换,输出只显示在屏幕上,或者存储它。

答案 1 :(得分:1)

您可以使用std::advance()转到下一个迭代器

int x = *itr;
std::list<int>::const_iterator itr2 = itr;
std::advance(itr2,1);
if(itr2==myList.end())
    break;
int y = *(itr2);
int diff = std::abs(x - y);
cout << diff << "\n";

编辑:如果c++ 11可用,请参阅std::next

答案 2 :(得分:1)

使用increment运算符移动迭代器。

auto x = itr;
auto y = itr;
if(++y!=myList.end())
{
    int diff = std::abs(*x - *y);
    cout << diff << "\n";
}

答案 3 :(得分:1)

#include <list>
#include <iostream>

int main()
{
    std::list<int> myList;
    //for (int i = 10;i < 15;i++)
    myList.push_back(12);
    myList.push_back(15);
    myList.push_back(18);
    myList.push_back(19);
    myList.push_back(25);
    for (std::list<int>::const_iterator itr = myList.begin();
                              itr != std::prev(myList.end());
                                                      ++itr)
    {
        std::list<int>::const_iterator nextIt = std::next(itr);
        int diff = *itr - *nextIt;
        std::cout << diff << "\n";
    }

    return 0;
}

使用std::next(itr)获取下一个迭代器 并在for循环中使用std::prev(myList.end())来获取列表中最后一个元素的前一个。

此外,您可以更改for循环并使用std::advance()获取下一个迭代器,而不使用std::nextstd::prev

std::list<int>::const_iterator itr = myList.begin();
while (true)
{
    int prevValue= *itr;
    std::advance(itr, 1);
    if (itr == myList.end())
    {
        break;
    }
    int diff = prevValue - *itr;
    std::cout << diff << "\n";

}

答案 4 :(得分:1)

你可以复制迭代器并运行两个,但我认为在循环之外获取第一个值更容易然后使用循环迭代第二个值,在继续下一个值之前用第二个值重置第一个值

这样的事情:

#include <list>
#include <iostream>

int main()
{
    std::list<int> myList;
    myList.push_back(12);
    myList.push_back(15);
    myList.push_back(18);
    myList.push_back(19);
    myList.push_back(25);
    std::list<int>::const_iterator itr = myList.begin();
    if(itr != myList.end()) // is a first value
    {
        int last = *itr; // cache it
        for (itr++; itr != myList.end(); itr++) // get next value
        { 
            int current = *itr; //cache it
            int diff = std::abs(last - current);
            std::cout << diff << "\n";
            last = current; // update last value
        }
    }
    return 0;
}

答案 5 :(得分:1)

另一种方法是简单地跟踪前面的列表元素值,而不是迭代器,并从迭代器当前指向的元素中减去前一个值;像这样:

#include <list>
#include <cmath> // need cmath for std::abs
#include <iostream>
using namespace std;

int main()
{
  std::list<int> myList;
  //for (int i = 10;i < 15;i++)
  myList.push_back(12);
  myList.push_back(15);
  myList.push_back(18);
  myList.push_back(19);
  myList.push_back(25);
  int PreviousElement = 0;
  bool Start = false;
  for (std::list<int>::const_iterator itr = myList.begin(); itr != myList.end(); ++itr)
  {
    if (Start)
    {
      int diff = std::abs(*itr - PreviousElement);
      cout << diff << "\n";
    }
    else
    {
      Start = true;
    }
    PreviousElement = *itr;
  }

  return 0;
}

答案 6 :(得分:1)

我认为你需要两个迭代器才能解决这个问题 这是我的代码

#include "stdafx.h"
#include <list>

#include <iterator>
#include <iostream>

using namespace std;

int main()
{
    std::list<int> myList;
    //for (int i = 10;i < 15;i++)
    myList.push_back(12);
    myList.push_back(15);
    myList.push_back(18);
    myList.push_back(19);
    myList.push_back(25);
    int x = 0;
    int y = 0;
    std::list<int>::iterator itr;
    std::list<int>::iterator it_next;

    for (itr = myList.begin(), it_next = ++myList.begin();it_next != myList.end();itr++, it_next++)
    {

        x = *itr;
        y = *it_next;
        int diff = abs(x - y);
        cout << diff << "\n";
    }

    system("PAUSE");
    return 0;
}

我在VC ++ 2005中构建它但它在C ++中应该可以正常工作 希望这会对你有所帮助:)。