简体   繁体   中英

Why this C++ code(from C++ Primer 5th 3.4.2) can't work right?

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

using namespace std;

int main(){

    vector<string> text = {"a", "v"};
    vector<string>::iterator beg = text.begin();
    vector<string>::iterator end = text.end();
    vector<string>::iterator mid = text.begin() + (end - beg) / 2;

    while (mid != end && *mid != "l") {
        if ("l" < *mid) {
            end = mid;
        } else {
            beg = mid;
        }
        mid = beg + (end - beg) / 2;
    }

    if (*mid == "l")
        cout << "Have Found!" << endl;
    else
        cout << "Not Found!" << endl;
    return 0;
}

I don't know why it go to endless loop?Please Help me!I build the code in VisulStudio 2013 and Code::Block,But result all is bad!

So OK I am scratching the solution because I think your algorithm was going wrong, which made mid to point the unidentified locations. What I have figured out is you were using C++11 .Nevertheless, this solution would work fine.

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

using namespace std;

void populateVector(vector<string> &text)
{
    for(int i=97;i<=118;i++)
    {
        text.push_back(string(1,(char)i));
    }
}

void printVector(vector<string> &text)
{
    for(int i=0;i<text.size();i++)
    {
        cout<<text[i]<<"\n";
    }
}

bool found(vector<string> &text,string &toBeFound)
{
    vector<string>::iterator beg = text.begin();
    vector<string>::iterator end = text.end();
    vector<string>::iterator mid ;

    while(beg<=end)
    {

        mid = beg + (end - beg) / 2;
        //This piece of code had to be added 
        if(mid==end)
        {
         break;
        }
        //This piece of code had to be added 
        //@DEBUG: cout<<*mid<<"\n";
        if(*mid==toBeFound)
        {
            return true;
        }
        if(*mid<toBeFound)
        {
            beg=mid+1;
        }
        else
        {
            end=mid-1;
        }
    }
    return false;

}

int main()
{

    vector<string> text;
    string toBeFound = "l";
    populateVector(text);
    printVector(text);

    if(found(text,toBeFound))
    {
        cout<<"Found\n";
    }
    else
    {
        cout<<"Not Found\n";
    }



    return 0;
}

The program is self clear. Lemme know if you have any issues. I guess this should work.

Hope this helps

It looks like mid = beg + (end - beg) / 2; is the source of your problem. If you were to make the start of you while loop something like the following it should work.

while(++mid != end && *mid != "l")

++mid begin the key.

*mid is always "a" Inside of your loop. This never meets the conditions to exit.

while (mid != end && *mid != "l") {
    if ("l" < *mid) {
        end = mid;
    } else {
        beg = mid+1; //Increment mid here to exit the loop;
    }
    mid = beg + (end - beg) / 2;
}

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