473,782 Members | 2,454 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with string parameters

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(stri ng 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("eou io","vltn","evo lution"); // 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(stri ng 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_stri ng<char,struct std::char_trait s<char>,class
std::allocator< char> > &'

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

Thanks.

Apr 24 '06 #1
4 2173

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

bool areSubComp(stri ng 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(cons t 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

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(stri ng 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("eou io","vltn","evo lution"); // 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(stri ng 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_stri ng<char,struct std::char_trait s<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(stri ng 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
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
Your function is defined as taking non-const references to std::string:

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

You are passing in const char*:

cout << areSubComp("eou io","vltn","evo lution");

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(stri ng c1, const string& c2, const string& c3)

Apr 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8307
by: Jason Huang | last post by:
Hi, The SqlParameter myPM =new SqlParameter("@Address", txtAddress.Text) is working for update, but SqlParameter myPM =new SqlParameter ("@Address",SqlDbType.NVarChar,90,txtAddress.Text) is not working. The Address column is DataType NVarChar 90, no problem. Any idea?
1
3552
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say black for example). I am dynamically creating the LinkButton controls and adding them into the table cell and I've also hooked up an event handler for each LinkButton's Click event. This all works fine. Now in the Link Button's Click event...
1
3431
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return simple type, I cannot consume methods that return complex objects. I have tried experimenting, with this, but am at a bit of loss on where the problem lies. When I call the services from a browser, I do get back the response that contains valid...
4
1978
by: leslie_tighe | last post by:
Hello, I have a webservice running on a J2EE server created with Axis 1.2.. I have a client that I am building in .net that needs to consume this webserivce and am having a bit of trouble. I have pasted the wsdl below and have a created a class in VB.net by adding a web refrence to my project. What is odd is that I can successfully call methods that return a simple value like a string. I call also call methods that add an object....
0
1445
by: leslie_tighe | last post by:
Hello, I have a web service that is running in a java server using axis that I want to use from .net. In VS.net 2003 I setup a project with a web reference. Using generated code I am able to invoke the web service. The Soapscape tells me that an xml reponse is coming back and its pasted below, along with the wsdl file. However, the object on the ..net side is not getting populated. Any ideas as to why that may be? I am using...
0
2373
by: ryan | last post by:
I've been tasked with consuming a Perl web service that was written by a person in a different department of my company. The problem is it's the guy's first attempt at web services and he doesn't know how to create the WDSL. I created web service with Visual Studio that mimics his service and after some tweaking of the soapAction in the WSDL I'm able to call his service successfully. When I use SOAPScope to look the messages being sent and...
2
1694
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and later, when i check for emptiness, if (product==null)
3
1527
by: Constantine | last post by:
Hi, I have developed one class called CProductInfo providing == and != operator features. I have one problem. When I use this class in my program, say CProductInfo product = null; and later, when i check for emptiness, if (product==null)
5
2179
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can change depending how many columns are in a Table. I add a new column with this code: Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Try...
12
2261
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my office. The original code's backend is using the SQL Server 2000 and I am testing to use it on the Express edition. And I run into the following problem.
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.