Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. find the string in the std::list

find the string in the std::list

Scheduled Pinned Locked Moved C / C++ / MFC
c++
4 Posts 3 Posters 5 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.
  • I Offline
    I Offline
    itkid
    wrote on last edited by
    #1

    Hi, I am storing all the barcode into below declared list typedef std::list<char* > SERIAL_NUM_BARCODE_LIST; SERIAL_NUM_BARCODE_LIST SerialNumDescList; Now before adding to list I have to check whether barcode exist in the list or not. If barcode does not exist than only I have to add it. typedef std::list<char* >::iterator BARCODE_LIST_ITER; BARCODE_LIST_ITER BarcodeListIter; for(BarcodeListIter = SerialNumBarcodeList.begin(); BarcodeListIter != SerialNumBarcodeList.end();BarcodeListIter++) strcmp(SerialNumBarcodeList.front(),Barcode) Is there simple find method present in STL to check inside the list. Thanks:-D

    C L 2 Replies Last reply
    0
    • I itkid

      Hi, I am storing all the barcode into below declared list typedef std::list<char* > SERIAL_NUM_BARCODE_LIST; SERIAL_NUM_BARCODE_LIST SerialNumDescList; Now before adding to list I have to check whether barcode exist in the list or not. If barcode does not exist than only I have to add it. typedef std::list<char* >::iterator BARCODE_LIST_ITER; BARCODE_LIST_ITER BarcodeListIter; for(BarcodeListIter = SerialNumBarcodeList.begin(); BarcodeListIter != SerialNumBarcodeList.end();BarcodeListIter++) strcmp(SerialNumBarcodeList.front(),Barcode) Is there simple find method present in STL to check inside the list. Thanks:-D

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You can simply compare the contents of the iterator (so, the string) with the name of the new barcode. To access the string through the iterator, just use this: (*BarcodeListIter)

      1 Reply Last reply
      0
      • I itkid

        Hi, I am storing all the barcode into below declared list typedef std::list<char* > SERIAL_NUM_BARCODE_LIST; SERIAL_NUM_BARCODE_LIST SerialNumDescList; Now before adding to list I have to check whether barcode exist in the list or not. If barcode does not exist than only I have to add it. typedef std::list<char* >::iterator BARCODE_LIST_ITER; BARCODE_LIST_ITER BarcodeListIter; for(BarcodeListIter = SerialNumBarcodeList.begin(); BarcodeListIter != SerialNumBarcodeList.end();BarcodeListIter++) strcmp(SerialNumBarcodeList.front(),Barcode) Is there simple find method present in STL to check inside the list. Thanks:-D

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Ideally, you should store the barcodes as strings instead of a pointer. For example:

        std::liststd::string SerialNumBarcodeList;
        ...
        std::list<string>::const_iterator it = std::find(SerialNumBarcodeList.begin(), SerialNumBarcodeList.end(), BarCode);
        if (it != SerialNumBarcodeList.end())
        {
        // Found!
        ...
        }

        -- modified at 2:56 Monday 27th March, 2006

        I 1 Reply Last reply
        0
        • L Lost User

          Ideally, you should store the barcodes as strings instead of a pointer. For example:

          std::liststd::string SerialNumBarcodeList;
          ...
          std::list<string>::const_iterator it = std::find(SerialNumBarcodeList.begin(), SerialNumBarcodeList.end(), BarCode);
          if (it != SerialNumBarcodeList.end())
          {
          // Found!
          ...
          }

          -- modified at 2:56 Monday 27th March, 2006

          I Offline
          I Offline
          itkid
          wrote on last edited by
          #4

          Thank dear, I hope it will take care the boundary condition where it is checking the barcode with SerialNumBarcodeList.end() :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups