2017-11-01 4 views
0

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; 
} 
+2

イテレータに追加するには、[ランダムアクセス反復子](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator)が必要です。 'std :: list'はこの種のイテレータを提供しません。代わりに、[双方向イテレータ](http://en.cppreference.com/w/cpp/concept/BidirectionalIterator)のみです。また、最後の要素に注意してください。 – user4581301

答えて

1

あなたは次のイテレータに

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"; 

EDITを取得するためにstd::advance()を使用することができます。利用可能c++ 11場合は、std::next

+0

Ohh申し訳ありませんが、C++の以前のバージョンを使用していますが、C++ではありません。11 – Programmerzzz

+0

@Programmerzzz Bestを質問に追加して、誰もがそれを見ることができるようにしてください。さもなければ、このような答えが得られます。 – user4581301

+0

私は答えを編集しました。 –

1

使用の増分を見ますオペレータはイテレータを移動します。

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

これは、リスト内の最初の要素と残りの要素の間に違いがあると思いますが、連続した要素間では違いはありません。 – Programmerzzz

+0

いいえ、このコードをforループに入れてください。 –

+0

@Programmerzzzこの回答はあなたに速いものを引っ張っています。 Mayurは、「x」と「y」を再利用し、再タイピングしました。これは動作しますが、説明が必要です。 – user4581301

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)次のイテレータ を取得し、リストの最後の要素の前の取得するためのループでstd::prev(myList.end())を使用します。

また、あなたはループのためにあなたを変更し、どのようにadjacent_differenceの使用について

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"; 

} 
3

std::nextstd::prevを使用せずに、次のイテレータを取得するためにstd::advance()を使用することができますか?

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

See Here


あなたは、実装に興味を持っている場合はその添付リンクでPossible Implementationセクションで実装方法を見て。あなたがしなければならないのは、リストイテレータに置き換えて、画面上に出力するだけです。

+0

これは余分な最初の要素を持ちます。 –

+0

@ GauravSehgalそうですね、アルゴリズムも参照してください。_some check_ – P0W

1

イテレータをコピーして2つ実行することはできますが、ループ外の最初の値を取得してループを使用して2番目の値を反復し、次。このような

何か:

#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; 
} 
1

もう一つの方法は、単純に前のリストの要素の値ではなく、イテレータを追跡するために、そして反復子が現在指している要素からその前の値を減算することであろう。私はこの質問 を解決するために、2つのイテレータが必要だと思う

#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; 
} 
1

ここで私はVC++ 2005でそれを構築するが、それはC++
希望でこれを正常に動作する必要があります私のコード

#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; 
} 

だ:そうは次のようにあなたに役立つでしょう:)

+0

の後に2番目のインデックスを印刷することができます。これらのコードの結果は[this](https://imgur.com/nAa54Eu) –

関連する問題