Can someone clarify/correct me on why certain functions such as push_back() do not need an assignment to output the new string, while a function such as substr() needs to be assigned to a string in order for the output to be the new subs-string? For example:
string givenInput;
char addChar;
getline(cin, givenInput);
cin >> addChar;
givenInput.push_back(addChar);
cout << givenInput << endl;
This example outputs the new string without having to do givenInput = givenInput.push_back(addChar);
. However, when doing something like:
string strVal;
int beginIndex;
int selectionLen;
getline(cin, strVal);
cin >> beginIndex;
cin >> selectionLen;
strVal = strVal.substr(beginIndex, selectionLen);
cout << strVal << endl;
Why is that I need to assign strVal rather than simply having the line strVal.substr(beginIndex, selectionLen);
and then outputting cout << strVal?
I hope what I'm asking makes sense, and if anyone could clarify why this is, that would be greatly appreciated, thanks!