Click here to Skip to main content
14,382,360 members

C / C++ / MFC

 
AnswerRe: Help with Resizing Bitmap loaded with LoadImage() and Attach to CImagelist object Pin
Richard MacCutchan18-Nov-19 22:26
protectorRichard MacCutchan18-Nov-19 22:26 
AnswerRe: Help with Resizing Bitmap loaded with LoadImage() and Attach to CImagelist object Pin
leon de boer19-Nov-19 0:14
memberleon de boer19-Nov-19 0:14 
QuestionRe: Help with Resizing Bitmap loaded with LoadImage() and Attach to CImagelist object Pin
EternalWyrm20-Nov-19 7:58
memberEternalWyrm20-Nov-19 7:58 
QuestionVS2008 Map files, this can't be this hard! Pin
charlieg18-Nov-19 13:48
membercharlieg18-Nov-19 13:48 
AnswerRe: VS2008 Map files, this can't be this hard! Pin
Randor 24-Nov-19 17:04
professional Randor 24-Nov-19 17:04 
GeneralRe: VS2008 Map files, this can't be this hard! Pin
charlieg25-Nov-19 6:45
membercharlieg25-Nov-19 6:45 
GeneralRe: VS2008 Map files, this can't be this hard! Pin
Randor 25-Nov-19 8:08
professional Randor 25-Nov-19 8:08 
Questionc++ string_view Pin
T Bones Jones14-Nov-19 12:38
memberT Bones Jones14-Nov-19 12:38 
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(test);

cout << slicecomma2(test) << endl;
cout << slicecomma2(test) << endl;
cout << slicecomma2(test) << endl;
cout << slicecomma2(test) << endl;
cout << slicecomma2(test) << endl;
cout << slicecomma2(test) << endl;

return 1;
}



Now I get the output of:
01234, 5678, 90, 78902, 999999, 333333
5678,
90,
78
99999
333333
33333


A few things wrong here.
First, the first item 01234 is skipped.
Second, a comma appears with the second and third printed items, when it should not.
Third, the fourth and fifth items are truncated to 78 when it should be 78902 and 99999 when it should be 999999
Fourth the last item, 333333, should not be repeated and should not appear truncated with a leading space.

So, it is not working as I expected it to work.

I would say that there is something here that I do not understand about string_view, but since I do not understand it I do not know what that is. Can anyone enlighten me?

Thank you.
AnswerRe: c++ string_view Pin
Graham Breach14-Nov-19 13:10
memberGraham Breach14-Nov-19 13:10 
GeneralRe: c++ string_view Pin
T Bones Jones15-Nov-19 4:44
memberT Bones Jones15-Nov-19 4:44 
QuestionPROGRAMMER'S MOM- Needs your help =) Pin
Member 1465597214-Nov-19 8:57
memberMember 1465597214-Nov-19 8:57 
AnswerRe: PROGRAMMER'S MOM- Needs your help =) Pin
OriginalGriff14-Nov-19 8:59
protectorOriginalGriff14-Nov-19 8:59 
Questionpassword in star form Pin
Member 1461560312-Nov-19 0:34
memberMember 1461560312-Nov-19 0:34 
AnswerRe: password in star form Pin
CPallini12-Nov-19 1:33
mveCPallini12-Nov-19 1:33 
SuggestionRe: password in star form Pin
David Crow12-Nov-19 6:48
memberDavid Crow12-Nov-19 6:48 
AnswerRe: password in star form Pin
Stefan_Lang12-Nov-19 22:07
memberStefan_Lang12-Nov-19 22:07 
QuestionHow to make the heap address(by new) have the same every time the program runs? Pin
Member 1308136910-Nov-19 16:36
memberMember 1308136910-Nov-19 16:36 
AnswerRe: How to make the heap address(by new) have the same every time the program runs? Pin
k505410-Nov-19 17:19
memberk505410-Nov-19 17:19 
GeneralRe: How to make the heap address(by new) have the same every time the program runs? Pin
Member 1308136920-Nov-19 16:48
memberMember 1308136920-Nov-19 16:48 
AnswerRe: How to make the heap address(by new) have the same every time the program runs? Pin
CPallini11-Nov-19 23:10
mveCPallini11-Nov-19 23:10 
GeneralRe: How to make the heap address(by new) have the same every time the program runs? Pin
Member 1308136920-Nov-19 16:49
memberMember 1308136920-Nov-19 16:49 
QuestionRe: How to make the heap address(by new) have the same every time the program runs? Pin
David Crow12-Nov-19 6:46
memberDavid Crow12-Nov-19 6:46 
AnswerRe: How to make the heap address(by new) have the same every time the program runs? Pin
Member 1308136920-Nov-19 16:56
memberMember 1308136920-Nov-19 16:56 
AnswerRe: How to make the heap address(by new) have the same every time the program runs? Pin
leon de boer13-Nov-19 3:34
memberleon de boer13-Nov-19 3:34 
GeneralRe: How to make the heap address(by new) have the same every time the program runs? Pin
Richard MacCutchan13-Nov-19 6:01
protectorRichard MacCutchan13-Nov-19 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.