Problem with string parameters

G

Gaijinco

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.
 
I

int2str

Gaijinco said:
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
 
P

pasalic.zaharije

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
 
T

thomas0033

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 ?
 
L

Lyell Haynes

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top