C++ 使用指针和向量

C++ 使用指针和向量,c++,class,vector,C++,Class,Vector,我必须写一个代码通过向量搜索数字。我对如何调用和使用搜索功能感到困惑 布尔线性搜索(常量向量&v,整数x): 一种线性搜索算法,其中x是向量v中的搜索项。它只是从向量v的开始到结束搜索x,但当存在匹配时,它停止搜索。如果搜索成功,则返回true;否则,它将返回false。要实现此例程,只需调用find( )来自STL的函数 •布尔二进制搜索(常量向量&v,int x): 一种二进制搜索算法,其中x是向量v中的搜索项。如果搜索成功,则返回true;否则,它将返回false。要实现这个例程,只需从S

我必须写一个代码通过向量搜索数字。我对如何调用和使用搜索功能感到困惑

布尔线性搜索(常量向量&v,整数x): 一种线性搜索算法,其中x是向量v中的搜索项。它只是从向量v的开始到结束搜索x,但当存在匹配时,它停止搜索。如果搜索成功,则返回true;否则,它将返回false。要实现此例程,只需调用find( )来自STL的函数

•布尔二进制搜索(常量向量&v,int x): 一种二进制搜索算法,其中x是向量v中的搜索项。如果搜索成功,则返回true;否则,它将返回false。要实现这个例程,只需从STL调用binary_search()函数

•整数搜索(常数向量&v1,常数向量&v2,布尔(*p)(常数向量&int) ):通用搜索算法–获取指向搜索例程p()的指针,然后为向量v1中向量v2的每个元素调用p()。它计算成功搜索的总数,并将该值作为打印例程printStat( ),用于打印搜索算法的最终统计信息

#include<iostream>
#include <vector>
#include <stdlib.h>     
#include <time.h>
#include<cstdlib>
#include<cmath>
#include <algorithm>

using namespace std;

void genRndNums( vector<int>& v, int vec_size, int seed ) {
    srand(seed);
    for (int i = 0; i < vec_size; i++)
    {

        int a = (rand() % 1000) + 1;//getting a random number
        v.push_back(a);//adding it to the vector 

    }
}

bool linearSearch(const vector<int>& v, int x) {

    find(v.begin(), v.end(), x);
}

bool binarySearch( const vector<int>& v, int x) {
    if (binary_search(v.begin(), v.end(), x))
        return true;
    else
        return false;

}

int search( const vector<int>& container, const vector<int>& searchNums,
            bool (*p)( const vector<int>&, int) ) {

    int count;
    while ( )
    {
        if ((*p)(const vector<int> & container, int)) == true)
            count= count + 1;

    }

}

void sortVec (vector<int>& v) {
    sort(v.begin(), v.end());//sorting the vector
}

void printStat (int totalSucCnt, int vec_size) {

}

int main() {
    const int TOBE_SEARCHED = 5000;
    vector<int> container, tobeSearched;
    genRndNums(container, 10000, 9);
    genRndNums(tobeSearched, TOBE_SEARCHED, 3);

    cout << "\nConducting linear search ..." << endl;
    int linear_search_count = search( container, tobeSearched, linearSearch );
    printStat ( linear_search_count, TOBE_SEARCHED );

    cout << "\nConducting binary search on unsorted container ..." << endl;

    int binary_search_count = search( container, tobeSearched, binarySearch );
    printStat ( binary_search_count, TOBE_SEARCHED );

    sortVec( container );

    cout << "\nConducting binary search on sorted container ..." << endl;
    binary_search_count = search( container, tobeSearched, binarySearch );
   // printStat ( binary_search_count, TOBE_SEARCHED );

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void genRndNums(向量和v、整数向量大小、整数种子){
srand(种子);
对于(int i=0;icout下面是您的代码的修改版本,它同时适用于线性搜索和二进制搜索。代码还说明了如何传递函数并调用函数来搜索数字

#include<iostream>
#include <vector>
#include <stdlib.h>     
#include <time.h>
#include<cstdlib>
#include<cmath>
#include <algorithm>

using namespace std;

void genRndNums( vector<int>& v, int vec_size, int seed ) {
    srand(seed);
    for (int i = 0; i < vec_size; i++)
    {

        int a = (rand() % 1000) + 1;//getting a random number
        v.push_back(a);//adding it to the vector 

    }
}

bool linearSearch(const vector<int>& v, int x) {
    return find(v.begin(), v.end(), x) != v.end();
}

/* Works only when the vector is sorted*/
bool binarySearch( const vector<int>& v, int x) {
    return binary_search(v.begin(), v.end(), x);
}

int search( const vector<int>& container, const vector<int>& searchNums,
            bool (*p)( const vector<int>&, int) ) {

    int count = 0;
    for(int i=0; i<searchNums.size(); i++)
    {
        if (p(container, searchNums[i]) == true)
            count= count + 1;
    }
    return count;

}

void sortVec (vector<int>& v) {
    sort(v.begin(), v.end());//sorting the vector
}

void printStat (int totalSucCnt, int vec_size) {
    cout<<"found " << totalSucCnt << "out of " << vec_size<<endl;
}

void printVec(vector<int> & v){
    for(int i=0; i<v.size(); i++)
        cout<<v[i]<<" ";
    cout<<endl;

}
int main() {
    const int TOBE_SEARCHED = 50;
    vector<int> container, tobeSearched;
    genRndNums(container, 100, 9);
    genRndNums(tobeSearched, TOBE_SEARCHED, 3);
    //cout<<"vector 1:"<<endl;
    //printVec(container);

    //cout<<"vector 1:"<<endl;
    //printVec(tobeSearched);

    cout << "\nConducting linear search ..." << endl;
    int linear_search_count = search( container, tobeSearched, linearSearch );
    printStat ( linear_search_count, TOBE_SEARCHED );

    cout << "\nConducting binary search on unsorted container ..." << endl;

    int binary_search_count = search( container, tobeSearched, binarySearch );
    printStat ( binary_search_count, TOBE_SEARCHED );

    sortVec( container );

    cout << "\nConducting binary search on sorted container ..." << endl;
    binary_search_count = search( container, tobeSearched, binarySearch );
    printStat ( binary_search_count, TOBE_SEARCHED );

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void genRndNums(向量和v、整数向量大小、整数种子){
srand(种子);
对于(int i=0;i对于(inti=0;iSooo),您的问题是什么?您对什么感到困惑?请参阅以询问更多问题correctly@h4ckthepl4net
[ask]
扩展到.Other@user4581301哦,好的,我不知道,谢谢)不解释你改变了什么以及为什么不是写答案的好方法。@t.niese谢谢你的建议,我会更新答案并在将来的答案中处理好它。非常感谢。我仍然不知道*p是如何传递的?而且,每次输出都是相同的值?非常感谢你的帮助。