简体   繁体   中英

Binary Search and Sorting an Array by alphabetical order C++

I have a dynamic array which contains a contact number and name. I was wondering how to do a binary search for the name. Let's say I have 20 contacts and I want to find the number of the contact with name "John" .

Here is the data structure:

struct Contact
{
    int ContactNumber,Fax;
    string Name, Email;
    PhoneNumber Phone;
    Address anAddress;
};

I have:

Contact * ptrFirst = & arrofCont[0];
Contact * ptrLast = & arrofCont[MAX - 1];

that contains the contact name and number and address etc. I guess those can be used as a first and last but don't know where to go from there.

You don't need to sort or binary search your array to do what you want.
Just use std::find_if .

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

struct Company
{
    std::string name ;
    std::string number ;    
};

struct HasName
{
    HasName (const std::string &name) : name (name) {}
    bool operator () (const Company &company) {
        return company.name == name ;
    }

    std::string name ;
};

int main (void)
{
    std::vector <Company> companies ;
    // Fill up the vector...

    std::vector <Company>::const_iterator citer ;
    citer = std::find_if (companies.cbegin (), companies.cend (), HasName ("John")) ;

    if (citer != companies.cend ()) {
        std::cout << citer->number << "\n" ;
    }

    return 0 ;
}

Here is an example that uses a lambda expression to compare a pair of elements of the array It corresponds to your original post before you updated it.

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

struct Contact
{
   std::string name;
   int number;
};

int main()
{
    const size_t N = 3; // or N = 20 or you can use name MAX instead of N
    Contact *p = new Contact[N] { { "B", 2 }, { "A", 1 }, { "C", 3 } };

    auto less_by_name = []( const Contact &c1, const Contact &c2 ) 
    { 
        return ( c1.name < c2.name ); 
    };

    std::sort( p, p + N, less_by_name);

    auto it = std::lower_bound( p, p + N, Contact( { "B", 0 } ), less_by_name );

    if ( it != p + N ) std::cout << "The number of \"B\" is " << it->number << std::endl;

    delete []p;
}  

Or you can make the functor as a member of your class. For example

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

struct Contact
{
   std::string name;
   int number;
   static bool less_by_name( const Contact &c1, const Contact &c2 )
   {
        return ( c1.name < c2.name );
   }
};

int main()
{
    const size_t N = 3; // or N = 20 or you can use name MAX instead of N
    Contact *p = new Contact[N] { { "B", 2 }, { "A", 1 }, { "C", 3 } };

    std::sort( p, p + N, Contact::less_by_name);

    auto it = std::lower_bound( p, p + N, Contact( { "B", 0 } ), Contact::less_by_name );

    if ( it != p + N ) std::cout << "The number of \"B\" is " << it->number << std::endl;

    delete []p; 
}

As for your definitions

Contact * ptrFirst = & arrofCont[0];
Contact * ptrLast = & arrofCont[MAX - 1];

then if you will change them the following way

Contact * ptrFirst = arrofCont;
Contact * ptrLast  = arrofCont + MAX;

then they will correspond to

Contact * ptrFirst = p;
Contact * ptrLast  = p + N;

relative to my examples of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM