代码之家  ›  专栏  ›  技术社区  ›  Bill the Lizard

C++有顺序搜索功能吗?

  •  1
  • Bill the Lizard  · 技术社区  · 16 年前

    我有一个未排序的小数组,我想找到

    我特别使用了C样式的数组,如:

    std::string arr[5] = { "EVEN", "ODD", "NONE", "MARK", "SPACE" };
    

    我需要用户提供的值的索引。

    5 回复  |  直到 16 年前
        1
  •  15
  •   Patrick Glandien    12 年前

    使用 std::find() 算法 -图书馆,还是图书馆 find() -特定容器的方法。

        2
  •  10
  •   Michael Burr    16 年前

    std::find() 应:

    #include <stdio.h>
    #include <algorithm>
    #include <string>
    
    using std::string;
    
    std::string arr[5] = { "EVEN", "ODD", "NONE", "MARK", "SPACE" };
    
    
    int main() {
    
        string* pArrEnd = arr + sizeof( arr)/sizeof(arr[0]);
    
        string* pFound = std::find( arr, pArrEnd, "MARK");
    
        if (pFound == pArrEnd) {
            printf( "not found\n");
        }
        else {
            printf( "%s was found at index %d\n", pFound->c_str(), pFound - arr);
            printf( "or using STL: %d\n", std::distance( arr, pFound));
        }
    
        return 0;
    }
    
        3
  •  5
  •   John Dibling    16 年前

    您可以在容器上使用STL algos,而不仅仅是STL容器。例如,可以在C样式数组中使用std::find()

    // alloc the array
    static const size_t numItems = 100000;
    int * items = new int[numItems];
    
    // fill the array
    for( size_t n = 0; n < numItems; ++n )
        items[n] = n;
    
    // find 42 using std::find()
    int* found = std::find(&items[0], &items[numItems], 42);
    if( found == &items[numItems] )
    {
        // this is one past the end, so 42 was not found
        items[0] = 42;
    }
    else
    {
        // we found the first instance of 42 at this location
        // change it to 43
        *found = 43;
    }
    
        4
  •  4
  •   Mykola Golubyev    16 年前

    我想你需要的是索引,而不是迭代器。

    int main()
    {
        // for c++ vector
        typedef int Element;
        typedef std::vector<Element> CppVector;
    
        CppVector v;
        v.push_back( 2 );
        v.push_back( 4 );
        v.push_back( 8 );
        v.push_back( 6 );
    
        const Element el = 4;
    
        CppVector::const_iterator it = std::find( v.begin(), v.end(), el );
        if ( it == v.end() )
        {
            std::cout << "there is no such element" << std::endl;
        }
        else
        {
            const CppVector::size_type index = it - v.begin();
            std::cout << "index = " << index << std::endl;
        }
    
        // for C array
        typedef Element CVector[4];
        CVector cv;
        cv[0] = 2;
        cv[1] = 4;
        cv[2] = 8;
        cv[3] = 6;
    
        const std::size_t cvSize = sizeof( cv ) / sizeof( Element );
    
        std::cout << "c vector size = " << cvSize << std::endl;
    
        const Element* cit = std::find( cv, cv + cvSize, el );
        const std::size_t index = cit - cv;
    
        if ( index >= cvSize )
            std::cout << "there is no such element" << std::endl;
        else
            std::cout << "index = " << index << std::endl;
    }
    
        5
  •  3
  •   dmckee --- ex-moderator kitten    16 年前

    除了STL的可能性之外( std::find )前面已经提到了POSIX函数 lsearch