Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why qBinaryFind is giving compilation errors for std::list?
Forum Updated to NodeBB v4.3 + New Features

Why qBinaryFind is giving compilation errors for std::list?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 897 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kranti.reddy2001
    wrote on last edited by
    #1

    Why qBinaryFind is giving compilation errors for std::list?

    Please go through the following examples?
    for example following program does not compile.
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    std::list<int> sl;
    int num ;
    for ( int i = 0 ; i < 20 ; i ++ )
    {
    sl.push_back ( i ) ;
    }
    std::cout <<"Enter the integer :: " ;
    std::cin >> num ;
    std::list<int>::const_iterator slItr = qBinaryFind ( sl , num );
    if ( slItr != sl.end () )
    std::cout <<"Num " << *slItr << " found " << std::endl;
    else
    std::cout <<"Num " << num << " not found " << std::endl;
    return a.exec();
    }

    http://www.boostprogramming.com/forum/index.php/topic,748.0.html

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      std::list does not have random access, it's a linked list not a vector, and qBinarySearch needs a container that has random access to the elements.

      So you can use qBinaryFind with a std::vector, but you will need the overload that takes two iterators to specify a range (not the overload that takes a container, because of the stl that has a cbegin() and cend() to return const iterators):

      @
      std::vector<int> sl; int num ;
      for ( int i = 0 ; i < 20 ; i ++ )
      { sl.push_back ( i ) ; }

      std::cout <<"Enter the integer ::";
      std::cin >> num ;
      
      std::vector<int>::const_iterator slItr = qBinaryFind( sl.cbegin(), sl.cend() , num );
      
      if ( slItr != sl.end () )
          std::cout <<"Num " << *slItr << " found" << std::endl;
      else
          std::cout <<"Num "<< num << " not found" << std::endl;
      

      @

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved