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. c++ string_view

c++ string_view

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialannouncement
3 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.
  • T Offline
    T Offline
    T Bones Jones
    wrote on last edited by
    #1

    I am new to C++17, and am trying to figure out how to use string_view. I have three functions that parse a string of comma separated values: beforecomma reports the string before the first comma, aftercomma reports the string after the first comma, and slicecomma returns the string before the first comma and updates the original string to that part after the first comma. These functions all work properly in their current forms using just string. Now, I am trying to update them as beforecomma2, aftercomma2, and slicecomma2, to take advantage of string_view. However, I am getting some unexpected results. I am hoping someone out there can tell me why this is happening and what I am doing wrong. First, the functions:

    std::string_view beforecomma2 (std::string_view line)
    {
    auto found = line.find(',');
    if (found!=std::string::npos)
    {return line.substr(0,found);}
    else return line;
    }

    string_view aftercomma2 (string_view line)
    {
    auto found = line.find(',');
    if ((found==std::string::npos) || (found==line.size()-1)) return "";
    else
    {
    std::string_view out=line.substr(found+1,line.size());
    while ((out[0]==' ') && (out.size()>0)) out=out.substr(1,out.size());
    return out;
    }
    }

    string_view slicecomma2 (string& line)
    {
    auto found=line.find(',');
    if (found==std::string::npos)
    {
    return line;
    }
    else
    {
    string_view out=beforecomma2(line);
    line=aftercomma2(line);
    return out;
    }

    }

    I run these functions with the following main, that just calls the functions beforecomma2 and aftercomma2.

    int main (int argc, char* argv[])
    {
    string test="01234, 5678, 90, 78902, 999999, 333333";

    cout << test << endl;

    cout << beforecomma2(test) << endl; test=aftercomma2(test);
    cout << beforecomma2(test) << endl; test=aftercomma2(test);
    cout << beforecomma2(test) << endl; test=aftercomma2(test);
    cout << beforecomma2(test) << endl; test=aftercomma2(test);
    cout << beforecomma2(test) << endl; test=aftercomma2(test);
    cout << beforecomma2(test) << endl; test=aftercomma2(test);

    return 1;
    }

    I get the expected output of: 01234, 5678, 90, 78902, 999999, 333333 01234 5678 90 78902 999999 333333 So far, so good. Now I try to combine these two functions into a single function, slicecomma2, implemented as:

    int main (int argc, char* argv[])
    {
    string test="01234, 5678, 90, 78902, 999999, 333333";

    PrintString(t

    G 1 Reply Last reply
    0
    • T T Bones Jones

      I am new to C++17, and am trying to figure out how to use string_view. I have three functions that parse a string of comma separated values: beforecomma reports the string before the first comma, aftercomma reports the string after the first comma, and slicecomma returns the string before the first comma and updates the original string to that part after the first comma. These functions all work properly in their current forms using just string. Now, I am trying to update them as beforecomma2, aftercomma2, and slicecomma2, to take advantage of string_view. However, I am getting some unexpected results. I am hoping someone out there can tell me why this is happening and what I am doing wrong. First, the functions:

      std::string_view beforecomma2 (std::string_view line)
      {
      auto found = line.find(',');
      if (found!=std::string::npos)
      {return line.substr(0,found);}
      else return line;
      }

      string_view aftercomma2 (string_view line)
      {
      auto found = line.find(',');
      if ((found==std::string::npos) || (found==line.size()-1)) return "";
      else
      {
      std::string_view out=line.substr(found+1,line.size());
      while ((out[0]==' ') && (out.size()>0)) out=out.substr(1,out.size());
      return out;
      }
      }

      string_view slicecomma2 (string& line)
      {
      auto found=line.find(',');
      if (found==std::string::npos)
      {
      return line;
      }
      else
      {
      string_view out=beforecomma2(line);
      line=aftercomma2(line);
      return out;
      }

      }

      I run these functions with the following main, that just calls the functions beforecomma2 and aftercomma2.

      int main (int argc, char* argv[])
      {
      string test="01234, 5678, 90, 78902, 999999, 333333";

      cout << test << endl;

      cout << beforecomma2(test) << endl; test=aftercomma2(test);
      cout << beforecomma2(test) << endl; test=aftercomma2(test);
      cout << beforecomma2(test) << endl; test=aftercomma2(test);
      cout << beforecomma2(test) << endl; test=aftercomma2(test);
      cout << beforecomma2(test) << endl; test=aftercomma2(test);
      cout << beforecomma2(test) << endl; test=aftercomma2(test);

      return 1;
      }

      I get the expected output of: 01234, 5678, 90, 78902, 999999, 333333 01234 5678 90 78902 999999 333333 So far, so good. Now I try to combine these two functions into a single function, slicecomma2, implemented as:

      int main (int argc, char* argv[])
      {
      string test="01234, 5678, 90, 78902, 999999, 333333";

      PrintString(t

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      The string_view is a view into an existing string. The string it is looking at must still be available as long as you want to use the string_view. Your slicecomma2 function is creating a string_view from the line string but then it is changing the line string, making the contents of the string_view invalid.

      T 1 Reply Last reply
      0
      • G Graham Breach

        The string_view is a view into an existing string. The string it is looking at must still be available as long as you want to use the string_view. Your slicecomma2 function is creating a string_view from the line string but then it is changing the line string, making the contents of the string_view invalid.

        T Offline
        T Offline
        T Bones Jones
        wrote on last edited by
        #3

        Of course, I see that now. I knew I was making some type of stupid mistake, but those are always the hardest for me to catch and correct. Thank you for your response.

        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