Aracılığıyla paylaş


vector::operator<

Nasıl kullanılacağı gösterilmiştir vector::operator < Visual C++ standart şablon kitaplığı (stl) işlevi.

template<class _TYPE, class _A> inline
   bool operator<( 
   const vector<_TYPE, _A>& _X,
   const vector<_TYPE, _A>& _Y 
);

Notlar

Not

Prototip sınıfı/parametre adları üstbilgi dosyasında sürüm eşleşmiyor.Bazıları, okumayı kolaylaştırmak için değiştirildi.

Örnek kimlik, kullanıcı tanımlı türü boş bir vektör bildirir. Başlatılır ve vektör olarak gelişigüzel sırada dört kimliklerini ekler. Bu sıralar kullanarak operator < kimliği için tanımlanan ve yeni sıralanmış vektör oluşturur. (Değil, puan sırasına göre sıralar Not adı.)

Örnek

// Opless.cpp
// compile with: /EHsc
// Illustrates the defining the < operator to sort vectors
//
// Functions:
//
// operator< - Vector comparison operator.
//
// vector::begin - Returns an iterator to start traversal of the vector.
//
// vector::end - Returns an iterator for the last element of the vector.
//
// vector::iterator - Traverses the vector.
//
// vector::push_back - Appends (inserts) an element to the end of a
// vector, allocating memory for it if necessary.
//
// sort algorithm - Sorts the vector.
//
//////////////////////////////////////////////////////////////////////

// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.
#pragma warning(disable:4786)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std ;

// The ID class is used for team scoring. It holds each player's name
// and score.
class ID
{
public:
    string Name;
    int Score;
    ID() : Name(""), Score(0) {}
    ID(string NewName, int NewScore) : Name(NewName), Score(NewScore) {}
};

// In this example, an ID is equivalent only if both name and score match.
bool operator==(const ID& x, const ID& y)
{
    return (x.Name == y.Name) && (x.Score == y.Score);
}

// IDs will be sorted by Score, not by Name.
bool operator<(const ID& x, const ID& y)
{
    return x.Score < y.Score;
}

// Define a template class for a vector of IDs.
typedef vector<ID> NAMEVECTOR;

int main()
{
    // Declare a dynamically allocated vector of IDs.
    NAMEVECTOR theVector;

    // Iterator is used to loop through the vector.
    NAMEVECTOR::iterator theIterator;

    // Create a pseudo-random vector of players and scores.
    theVector.push_back(ID("Karen Palmer", 2));
    theVector.push_back(ID("Ada Campbell", 1));
    theVector.push_back(ID("John Woloschuk", 3));
    theVector.push_back(ID("Grady Leno", 2));

    cout << "Players and scores:" << endl;
    for (theIterator = theVector.begin(); theIterator != theVector.end();
         theIterator++)
        cout << theIterator->Score  << "     "
             << theIterator->Name << endl;
    cout << endl;

    // Sort the vector of players by score.
    sort(theVector.begin(), theVector.end());
    
    // Output the contents of the vector in its new, sorted order.
    cout << "Players ranked by score:" << endl;
    for (theIterator = theVector.begin(); theIterator != theVector.end();
         theIterator++)
        cout << theIterator->Score  << "     "
             << theIterator->Name << endl;
}
  

Gereksinimler

Başlık: <vector>

Ayrıca bkz.

Kavramlar

Standart şablon kitaplığı örnekleri