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. std::search matches start of data before passing to function, but not within function?

std::search matches start of data before passing to function, but not within function?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsalgorithmsquestion
2 Posts 1 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.
  • M Offline
    M Offline
    Mike the Red
    wrote on last edited by
    #1

    #ifndef BYTE // For OS X -ers
    #define unsigned char BYTE
    #define unsigned char * LPBYTE
    #endif
    #include <vector>
    #include <algorithm>
    void separate(vector< pair<LPBYTE, LPBYTE> >& retVec, LPBYTE data, LPBYTE delimeter, /* out */bool *bComplete = 0) {
    retVec.clear();
    if (bComplete) *bComplete = false;

    if (!data)	// No data - leave retVec empty
    	return;
    
    LPBYTE dataEnd = data + sizeof(data);
    LPBYTE delEnd = delimeter + sizeof(delimeter);
    size\_t delSize = distance(delimeter, delEnd);
    
    LPBYTE found = std::search(data, dataEnd, delimeter, delEnd);
    
    if (found == delEnd)	// Delimeter not in data - leave retVec empty
    	return;
    
    cout << "In separate(...)\\n";
    if (data\[0\] == delimeter\[0\])
    	cout << "data\[0\] == delimeter\[0\]\\n";
    
    if (found == data)
    	cout << "Found == data.\\n(" << found\[0\] << " == " << data\[0\] << ")\\n";
    
    if (found != data)  			// Found delimeter ends first split
    	cout << "Found != data; found == data\[" << distance(data, found) << "\].\\n";
    return;
    

    };
    int main()
    {
    BYTE buffer[] = {',', '1', '2', '3', ',', '4', '5', '6', ',', '7', '8', '9', ',', 0};
    BYTE del = ',';
    bool bComp = true;

    LPBYTE found = std::search(&buffer\[0\], &buffer\[0\] + sizeof(buffer), &del, &del + sizeof(del));
    if (found == &buffer\[0\])
    	cout << "found == &buffer\[0\]\\n";
    else
    	cout << "found == &buffer\[" << distance(&buffer\[0\], found) << "\]\\n";
    
    vector< pair<LPBYTE, LPBYTE> > lpb;
    separate(lpb, buffer, &del, &bComp);
    
    return 0;
    

    }

    Output:

    found == &buffer[0]
    In separate(...)
    data[0] == delimeter[0]
    Found != data; Found == data[4]

    When I search from within main(), the leading comma is found, but once I pass the data & delimeter to separate(), it misses the leading comma and finds the next one.. obviously I'm missing something - can anyone spot what it is? Your assistance is greatly appreciated! MZR

    M 1 Reply Last reply
    0
    • M Mike the Red

      #ifndef BYTE // For OS X -ers
      #define unsigned char BYTE
      #define unsigned char * LPBYTE
      #endif
      #include <vector>
      #include <algorithm>
      void separate(vector< pair<LPBYTE, LPBYTE> >& retVec, LPBYTE data, LPBYTE delimeter, /* out */bool *bComplete = 0) {
      retVec.clear();
      if (bComplete) *bComplete = false;

      if (!data)	// No data - leave retVec empty
      	return;
      
      LPBYTE dataEnd = data + sizeof(data);
      LPBYTE delEnd = delimeter + sizeof(delimeter);
      size\_t delSize = distance(delimeter, delEnd);
      
      LPBYTE found = std::search(data, dataEnd, delimeter, delEnd);
      
      if (found == delEnd)	// Delimeter not in data - leave retVec empty
      	return;
      
      cout << "In separate(...)\\n";
      if (data\[0\] == delimeter\[0\])
      	cout << "data\[0\] == delimeter\[0\]\\n";
      
      if (found == data)
      	cout << "Found == data.\\n(" << found\[0\] << " == " << data\[0\] << ")\\n";
      
      if (found != data)  			// Found delimeter ends first split
      	cout << "Found != data; found == data\[" << distance(data, found) << "\].\\n";
      return;
      

      };
      int main()
      {
      BYTE buffer[] = {',', '1', '2', '3', ',', '4', '5', '6', ',', '7', '8', '9', ',', 0};
      BYTE del = ',';
      bool bComp = true;

      LPBYTE found = std::search(&buffer\[0\], &buffer\[0\] + sizeof(buffer), &del, &del + sizeof(del));
      if (found == &buffer\[0\])
      	cout << "found == &buffer\[0\]\\n";
      else
      	cout << "found == &buffer\[" << distance(&buffer\[0\], found) << "\]\\n";
      
      vector< pair<LPBYTE, LPBYTE> > lpb;
      separate(lpb, buffer, &del, &bComp);
      
      return 0;
      

      }

      Output:

      found == &buffer[0]
      In separate(...)
      data[0] == delimeter[0]
      Found != data; Found == data[4]

      When I search from within main(), the leading comma is found, but once I pass the data & delimeter to separate(), it misses the leading comma and finds the next one.. obviously I'm missing something - can anyone spot what it is? Your assistance is greatly appreciated! MZR

      M Offline
      M Offline
      Mike the Red
      wrote on last edited by
      #2

      In main(), del is of type BYTE and sizeof(del) is 1. Once it's passed to separate, del is of type LPBYTE and sizeof(del) is 4.

      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