By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,500 Members | 2,809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,500 IT Pros & Developers. It's quick & easy.

Problem with string parameters

P: n/a
I made this function which given two strings c1 and c2 test if with the
letters of both you can exactly form the string c3, so for exaple c1 =
"evol" c2= "ution" and c3 = "evolution" will yield true.

The function looks like this:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool areSubComp(string c1, string c2, string c3)
{
c1+=c2;
sort(c1.begin(),c1.end());
sort(c3.begin(),c3.end());
if(c1==c3)
return true;
else
return false;
}

int main()
{
cout << areSubComp("eouio","vltn","evolution"); // line 19

return 0;
}

I was trying to pass the last two arguments as reference to avoid the
copy of both strings using:

bool areSubComp(string c1, string& c2, string& c3)

but the compiler gives me this error:

line 19: cannot convert parameter 2 from 'char [5]' to 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > &'

What's wrong and how can I accomplish what I wanted to?

Thanks.

Apr 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Gaijinco wrote:
I was trying to pass the last two arguments as reference to avoid the
copy of both strings using:

bool areSubComp(string c1, string& c2, string& c3)


You are sorting c3. It seems like passing it as a (non-const) reference
is a bad idea. Also, you cannot initialize a (non-const) reference from
a char*.

Try this:

bool areSubComp(const string& c1, const string& c2, string ci)
{
string cp(c1 + c2);

sort(cp.begin(),cp.end());
sort(ci.begin(),ci.end());

return (cp.compare(ci) == 0);
}

Cheers,
Andre

Apr 24 '06 #2

P: n/a

Gaijinco je napisao:
I made this function which given two strings c1 and c2 test if with the
letters of both you can exactly form the string c3, so for exaple c1 =
"evol" c2= "ution" and c3 = "evolution" will yield true.

The function looks like this:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

bool areSubComp(string c1, string c2, string c3)
{
c1+=c2;
sort(c1.begin(),c1.end());
sort(c3.begin(),c3.end());
if(c1==c3)
return true;
else
return false;
}

int main()
{
cout << areSubComp("eouio","vltn","evolution"); // line 19

return 0;
}

I was trying to pass the last two arguments as reference to avoid the
copy of both strings using:

bool areSubComp(string c1, string& c2, string& c3)

but the compiler gives me this error:

line 19: cannot convert parameter 2 from 'char [5]' to 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > &'

What's wrong and how can I accomplish what I wanted to?

Thanks.


Note that "eouio" is constant string - you can not legaly change
values.

One solution is to make params const:

bool areSubComp(string c1, const string&c2, const string& c3);

Than, compiler will make temprary string. This solution raise new
problem - you can not sort const data! So, you can make new copy into
temp variables, but this will end like starting code (you do not wanna
copy).

If your code do not need speed just leave it - string copy is not so
expensive.

If speed metters: you can use char* whit your or std::qsort sorting

Best,
Zahaije Pasalic

Apr 24 '06 #3

P: n/a
Andre's suggestion is a good way.

because of various library functions, it's better to avoid "+" or
"==".

but to Andre, I have another question,about why 'const'.. like
this(const string& c1)
in order to protect c1 from changing ?

Apr 25 '06 #4

P: n/a
Your function is defined as taking non-const references to std::string:

bool areSubComp(string c1, string& c2, string& c3)

You are passing in const char*:

cout << areSubComp("eouio","vltn","evolution");

std::string is implicitly convertable from const char*, i.e. it has a
non-explicit constructor that takes as it's only parameter a const
char*. This allows you to automagically create a std::string from a
const char*. So, in your case, the compiler basically creates a string
object on the stack from the const char* you are using and passes
_that_ to the function.

However, this std::string object is _temporary_ and that causes a
problem with the non-const reference, because you cannot use a
non-const reference to hold a temporary object. You _can_ use a const
reference, however. So, the simple solution to your problem is to
change your function definition to take const references to
std::string, like so:

bool areSubComp(string c1, const string& c2, const string& c3)

Apr 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.