std :: listの連続した要素の違いを調べようとしています。私は以下の解決策を試みましたが、thisスレッドが言うように、私はイテレータのコピーを作成し、それを増やす必要があります。イテレータに数値を追加すると意味が分からないので、エラーになります。私はここで何が欠けているのですか?私はCの以前のバージョンを使用しています++ではなくC++ 11イテレータを使用してstd :: listの次の要素を取得する
#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;
}
イテレータに追加するには、[ランダムアクセス反復子](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator)が必要です。 'std :: list'はこの種のイテレータを提供しません。代わりに、[双方向イテレータ](http://en.cppreference.com/w/cpp/concept/BidirectionalIterator)のみです。また、最後の要素に注意してください。 – user4581301