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. Difficulty in getting iterator upon using find algorithm of STL

Difficulty in getting iterator upon using find algorithm of STL

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpalgorithmsgraphicsdata-structures
8 Posts 2 Posters 0 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.
  • U Offline
    U Offline
    User 12320037
    wrote on last edited by
    #1

    Hello, Mine is a project in Visual C++, in which I am using STL, and trying to use the "find" algorithm with vectors. But it's giving me the error, error C2678: binary '==' : no operator found which takes a left-hand operand of type 'xyz' (or there is no acceptable conversion), because I have an app class in MFC which has a structure 'xyz' whose only member is a BYTE array. I am trying to get the iterator position by finding a value in a vector of a user-defined type and in return I am getting the above error. Hence a problem. Please help. OR someone can also suggest, which algorithm should I use to get the iterator position of the value I am searching for,from a vector of a user-defined data type (you can suggest that algorithm from the boost library also)? Please help. Thanks in advance. This is urgent. Here is the relevant code snippet to understand the problem: In the header, typedef struct xyz { BYTE byHex[16]; }XYZ; class CMyApp : public CWinApp { ----- ------ public: vector m_vec; ------- -------- DECLARE_MESSAGE_MAP() }; And in the implementation, in .cpp, BYTE byBuff[16] = {0}; vector ::iterator viter; viter = find(theApp.m_vec.begin(), theApp.m_vec.end(), byBuff); And, here this "find" algorithm is giving me problems.

    J 2 Replies Last reply
    0
    • U User 12320037

      Hello, Mine is a project in Visual C++, in which I am using STL, and trying to use the "find" algorithm with vectors. But it's giving me the error, error C2678: binary '==' : no operator found which takes a left-hand operand of type 'xyz' (or there is no acceptable conversion), because I have an app class in MFC which has a structure 'xyz' whose only member is a BYTE array. I am trying to get the iterator position by finding a value in a vector of a user-defined type and in return I am getting the above error. Hence a problem. Please help. OR someone can also suggest, which algorithm should I use to get the iterator position of the value I am searching for,from a vector of a user-defined data type (you can suggest that algorithm from the boost library also)? Please help. Thanks in advance. This is urgent. Here is the relevant code snippet to understand the problem: In the header, typedef struct xyz { BYTE byHex[16]; }XYZ; class CMyApp : public CWinApp { ----- ------ public: vector m_vec; ------- -------- DECLARE_MESSAGE_MAP() }; And in the implementation, in .cpp, BYTE byBuff[16] = {0}; vector ::iterator viter; viter = find(theApp.m_vec.begin(), theApp.m_vec.end(), byBuff); And, here this "find" algorithm is giving me problems.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Implement the equal (==) operator for your xyz class and the type on the right side. To get more help you should show us the relevant code (the operation, the structure, and the right side type). To do this edit your initial question to add this information.

      U 1 Reply Last reply
      0
      • J Jochen Arndt

        Implement the equal (==) operator for your xyz class and the type on the right side. To get more help you should show us the relevant code (the operation, the structure, and the right side type). To do this edit your initial question to add this information.

        U Offline
        U Offline
        User 12320037
        wrote on last edited by
        #3

        Here is the relevant code snippet: In the header, typedef struct xyz { BYTE byHex[16]; }XYZ; class CMyApp : public CWinApp { ----- ------ public: vector m_vec; ------- -------- DECLARE_MESSAGE_MAP() }; And in the implementation, in .cpp, BYTE byBuff[16] = {0}; vector ::iterator viter; viter = find(theApp.m_vec.begin(), theApp.m_vec.end(), byBuff); And, here this "find" algorithm is giving me problems.

        1 Reply Last reply
        0
        • U User 12320037

          Hello, Mine is a project in Visual C++, in which I am using STL, and trying to use the "find" algorithm with vectors. But it's giving me the error, error C2678: binary '==' : no operator found which takes a left-hand operand of type 'xyz' (or there is no acceptable conversion), because I have an app class in MFC which has a structure 'xyz' whose only member is a BYTE array. I am trying to get the iterator position by finding a value in a vector of a user-defined type and in return I am getting the above error. Hence a problem. Please help. OR someone can also suggest, which algorithm should I use to get the iterator position of the value I am searching for,from a vector of a user-defined data type (you can suggest that algorithm from the boost library also)? Please help. Thanks in advance. This is urgent. Here is the relevant code snippet to understand the problem: In the header, typedef struct xyz { BYTE byHex[16]; }XYZ; class CMyApp : public CWinApp { ----- ------ public: vector m_vec; ------- -------- DECLARE_MESSAGE_MAP() }; And in the implementation, in .cpp, BYTE byBuff[16] = {0}; vector ::iterator viter; viter = find(theApp.m_vec.begin(), theApp.m_vec.end(), byBuff); And, here this "find" algorithm is giving me problems.

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          As already suggested implement the equal operator. This requires changing the typedef struct to a struct (in fact a class):

          struct xyz
          {
          BYTE byHex[16];
          bool operator==(const xyz& other)
          {
          return 0 == memcmp(other.byHex, this->byHex, sizeof(this->byHex));
          }
          bool operator==(const BYTE buf[16])
          {
          return 0 == memcmp(buf, this->byHex, sizeof(this->byHex));
          }
          };

          Then use it as before:

          BYTE byBuff[16] = {0};
          vector<xyz> m_vec;
          vector<xyz> ::iterator viter;
          viter = std::find(m_vec.begin(), m_vec.end(), byBuff);

          U 1 Reply Last reply
          0
          • J Jochen Arndt

            As already suggested implement the equal operator. This requires changing the typedef struct to a struct (in fact a class):

            struct xyz
            {
            BYTE byHex[16];
            bool operator==(const xyz& other)
            {
            return 0 == memcmp(other.byHex, this->byHex, sizeof(this->byHex));
            }
            bool operator==(const BYTE buf[16])
            {
            return 0 == memcmp(buf, this->byHex, sizeof(this->byHex));
            }
            };

            Then use it as before:

            BYTE byBuff[16] = {0};
            vector<xyz> m_vec;
            vector<xyz> ::iterator viter;
            viter = std::find(m_vec.begin(), m_vec.end(), byBuff);

            U Offline
            U Offline
            User 12320037
            wrote on last edited by
            #5

            Many thanks for this solution, I fully appreciate it. Coincidentally, I had already implemented this, but since this was taking time in checking a database which is huge in size, I was looking for some other algorithm which will give a quicker result. Could you please suggest a better algorithm which will save me from using memcmp?

            J 1 Reply Last reply
            0
            • U User 12320037

              Many thanks for this solution, I fully appreciate it. Coincidentally, I had already implemented this, but since this was taking time in checking a database which is huge in size, I was looking for some other algorithm which will give a quicker result. Could you please suggest a better algorithm which will save me from using memcmp?

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              Quote:

              Could you please suggest a better algorithm which will save me from using memcmp?

              Here it is:

              struct xyz
              {
              BYTE byHex[16];
              bool operator==(const xyz& other) const { return other.byHex == this->byHex; }
              bool operator==(const BYTE buf[16]) const { return buf == this->byHex; }
              };

              But this won't be faster because the compiler will usually generate code that calls memcmp internally (or in most cases uses an inline version of memcmp). NOTE: In my initial answer I forgot to make the functions const.

              U 1 Reply Last reply
              0
              • J Jochen Arndt

                Quote:

                Could you please suggest a better algorithm which will save me from using memcmp?

                Here it is:

                struct xyz
                {
                BYTE byHex[16];
                bool operator==(const xyz& other) const { return other.byHex == this->byHex; }
                bool operator==(const BYTE buf[16]) const { return buf == this->byHex; }
                };

                But this won't be faster because the compiler will usually generate code that calls memcmp internally (or in most cases uses an inline version of memcmp). NOTE: In my initial answer I forgot to make the functions const.

                U Offline
                U Offline
                User 12320037
                wrote on last edited by
                #7

                Hi Jochen, First of all, many thanks for your solution. Although it has solved my operator == overloading question, but I have one more difficulty, may be a strange experience. The find algorithm of STL is not giving the iterator position, even if the item to be searched is located in the BYTE array. Does it have anything to do with the size of the arrays as well? because one BYTE array is of 16 bytes, and the one it is to be compared with is of 33 bytes? In such a case, how can I make the find algorithm give me the correct answer? Please help. Thanks in advance. - Sanjay

                J 1 Reply Last reply
                0
                • U User 12320037

                  Hi Jochen, First of all, many thanks for your solution. Although it has solved my operator == overloading question, but I have one more difficulty, may be a strange experience. The find algorithm of STL is not giving the iterator position, even if the item to be searched is located in the BYTE array. Does it have anything to do with the size of the arrays as well? because one BYTE array is of 16 bytes, and the one it is to be compared with is of 33 bytes? In such a case, how can I make the find algorithm give me the correct answer? Please help. Thanks in advance. - Sanjay

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #8

                  std::find is a function to search for an identical value of a specific type. If you have different types it won't work. This includes arrays of same type but different length. What you are probably looking for is some kind of 'contains' (like strstr) or 'begins with' (like memcmp and strncmp with length of searching value) function which you have to implement yourself. Example:

                  for (int i = 0; i < m_vec.size(); i++)
                  {
                  if (MySearch(m_vec.at(i), searchValue))
                  {
                  // found
                  break;
                  }
                  }

                  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