다음을 통해 공유


adjacent_difference 및 vector::push_back

사용 하는 방법을 보여 줍니다 있는 adjacent_differencevector::push_back STL (표준 템플릿 라이브러리) 함수를 Visual C++.

template<class InputIterator, class OutputIterator> inline
   OutputIterator adjacent_difference(
      InputIterator First, 
      InputIterator Last, 
      OutputIterator Result
   )
template<class InputIterator, class OutputIterator, class BinaryOperator> inline
   OutputIterator adjacent_difference(
      InputIterator First, 
      InputIterator Last, 
      OutputIterator Result,
      BinaryOperator Binary_Op
   )

설명

[!참고]

있는 프로토타입 클래스/매개 변수 이름은 헤더 파일에서 버전이 일치 하지 않습니다.일부 가독성을 높이기 위해 수정 되었습니다.

모든 요소를 지정 합니다. 반복기가 참조 i 범위에 [결과 + 1, 결과 + (Last - First도구 모음 같은 값) * (첫 번째 + (i - Result)) - *(First + (i - Result) - 1) or Binary_Op (*(First + (i - Result)), *(First + (i - Result) - 1)).결과 의 값을 가져옵니다 *First.

예제

// adj_diff.cpp
// compile with: /EHsc
// 
// Description of adjacent_difference(first,last,result)
//                adjacent_difference(first,last,result,binary_op):
//
//    Assigns to every element referred to by iterator i in the range
//    [result + 1, result + (last - first))
//    a value correspondingly equal to
//    *(first + (i - result)) - *(first + (i - result) - 1)
//    or
//    binary_op(*(first + (i - result)), *(first + (i - result) - 1)).
//    Result gets the value of *first.

#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>

using namespace std;


typedef vector < int > IntegerArray;
typedef ostream_iterator < int, char, char_traits<char> > IntOstreamIt;

int main ()
{
    // an ostream iterator that outputs an int to cout terminated
    // by a space
    IntOstreamIt itOstream(cout," ");

    // Initialize the array
    // Suppose that you are taking a trip and can measure
    // the miles traveled from your city of origin
    // to the city you are traveling through
    IntegerArray rgIA;
    rgIA.push_back(5661); // San Francisco to Berlin
    rgIA.push_back(7456); // to Cairo
    rgIA.push_back(10995); // to Kolkata
    rgIA.push_back(17019); // to Cape Town
    rgIA.push_back(24394); // to Hong Kong SAR
    rgIA.push_back(30376); // to London
    rgIA.push_back(35758); // to Los Angeles

    // Print the array
    copy(rgIA.begin(),rgIA.end(),itOstream);
    cout << endl;

    // Suppose that you now want the distance between each
    // of the cities that you traveled to. You can easily
    // find it with adjacent_difference()
    IntegerArray rgDifferences(7);
    IntegerArray::iterator itDifferences = rgDifferences.begin();
    adjacent_difference(rgIA.begin(),rgIA.end(),itDifferences);

    // Print the differences
    // Remember that the first item in the differences array is
    // not a difference, but is unused space
    cout << "The adjacent differences are: ";
    copy(rgDifferences.begin()+1,rgDifferences.end(),itOstream);
    cout << endl;

    // Suppose that you now want to know which adjacent differences
    // are greater. If you have [a,b,c], you would like [1,0] if a>b
    // and b<=c.
    // You are using less() rather than greater() because
    // adjacent_difference() reverses the parameters. For example,
    // if a and b are adjacent, adjacent_difference() calls
    // less(b,a). See the explanation at the top of this file
    // for a more exact description.
    IntegerArray rgGT(6);
    IntegerArray::iterator itGT = rgGT.begin();
    adjacent_difference(rgDifferences.begin()+1,
                        rgDifferences.end(),
                        itGT,
                        less<int>());

    // Print the greater thans
    // Remember that the first item in the differences array is
    // not a difference, but is unused space
    cout << "Which adjacent distances are greater:" << endl
         << "(If you have [a,b,c], then you have [1,0] if a>b and b<=c)"
         << endl;
    copy(rgGT.begin()+1,rgGT.end(),itOstream);
    cout << endl;
}
  

요구 사항

헤더: <numeric>

참고 항목

개념

표준 템플릿 라이브러리 샘플