function prob

closed account (28poGNh0)
I implemented two function that upper a string
I am wondering which of this two functions is better?

1
2
3
4
5
6
7
void uprStr(string &str)
{
    for(unsigned int i=0;i<str.size();i++)
    {
        str[i] = toupper(str[i]);
    }
}


1
2
3
4
5
6
7
string uprStr(string str)
{
    for(unsigned int i=0;i<str.size();i++)
    {
        str[i] = toupper(str[i]);
    }return str;
}


Thanks for reading
As far as memory (which would be negligible in this instance) goes, number 1!

Any other votes?
Yes number 1 by far. Number 1 you are passing the memory address of the string then you are directly modifying that string at the memory address. Number two you are passing a variable then copying it to a local variable, modify the copied variable, then return another copy of that variable. So altogether you created 2 new strings and then now you have to assign that to your old string.
What is the expected behavior calling it? I would vote that number 2 is better, since you're returned a copy of a string. After calling it, you can still get to the original string.

1
2
3
4
5
string str1("abcde");
string strWithCaps;
strWithCaps = uprStr(str1);
cout << str1 << endl;
cout << strWithCaps << endl;
booradley60 makes a point. It depends on whether or not you'll need to access the original string again, or if you want/need the string to always be capitalized.
When they said "better" I assumed faster and less memory being used. That would mean number 1. Also booradley can you explain your reasoning on being "better" because it is returning a copy? Just an fyi strings are complex objects and generally speaking you want to pass/return complex objects are references. Anyways it is always faster to pass by reference/return by reference versus passing/returning by copy. When ever you pass/call by copy you are calling the class constructor. So instead of calling 1 constructor ( when the object is created ) you are now calling the constructor 3 times. Then afterwards you now have to assign that value to the old string.

*I see he interpreted it as keeping the original and assigning to another variable.
Last edited on
closed account (28poGNh0)
thanks for the fast replies ,I am still opened for more if you want
If you only NEED the string to be capitalized, go with 1.

If you NEED the original string AND the capitalized version of the same string, go with 2. (Make sure to assign the functions return-value to another string besides the original one.)
Last edited on
It depends on what you need it to do ('you' being in the general sense the function that will call this uprStr one). If you call it expecting to get a new string (and not overwriting the one you started with) then the pass-by-value semantics is desirable. Pass-by-value also allows you to call the method and pass a string literal as the parameter. If best is only defined by speed of execution, then you're right. I was looking at it from a sense of how useable is it if it destroys every original string you send to it?
Yeah I noticed that afterwards sorry. I was just looking at the ops examples and assumed he was trying to just modify one string not assign to a separate string.
Topic archived. No new replies allowed.