Binary search an ArrayList by functor : ArrayList « Collections « Visual C++ .NET






Binary search an ArrayList by functor

 

#include "stdafx.h"
using namespace System;
using namespace System::Collections;

ref class myReverserClass: public IComparer
{
public:
   virtual int Compare(Object^ x, Object^ y) = IComparer::Compare
   {
       return (String::Compare((String^)y, (String^)x));
   }
};

void main()
{
    ArrayList ^alist = gcnew ArrayList(4);
    alist->Add("One");
    alist->Add("-");
    array<String^>^ morenums = gcnew array<String^> {"Four", "Five"};

    alist->AddRange(morenums);

    
    IComparer^ myComparer = gcnew myReverserClass;
    alist->Sort(myComparer);
    
    int indx = alist->BinarySearch("Four", myComparer);
    Console::WriteLine("Four found at index: {0}", indx.ToString());
    
}

   
  








Related examples in the same category

1.Sort an ArrayList
2.Adding to the end of an ArrayList
3.Adding value to ArrayList with Indexer
4.Insert value to an ArrayList at specified position
5.Using AddRange to add an array of value to ArrayList
6.Reverse the values in an ArrayList
7.Using for loop the display elements in an ArrayList
8.Get the capacity of an ArrayList
9.Set the capacity
10.Get the element count
11.Does an ArrayList contain a value
12.Sort an ArrayList by functor
13.Adding strings to ArrayList
14.Iterate ArrayList using indexing
15.Iterate ArrayList using the for each operator