string class newbie questions?

S

sandwich_eater

I am getting compiler errors using g++ in cygwin due to my lack of
knowledge of C++.

How do each of the following with std::string?
1. write a header for a function that takes a string (most efficient
way e.g. by reference), an integer and returns a string.
2. return a null std::string
3. see a documented list of functions implemented in std::string
4. make the function part of a class so that it extends the std::string
class

This is my first stab a LeftStr for starters as a test (i am not
passing by reference)

....
using namespace std;

string LeftStr(string s, int k)
{
if (k < 1)
{
return string "/0";
}
else if (s.length < k)
{
return s;
}
else
{
return s.substr (1, k);
}
}

compiler output

basic_str.cpp: In function `std::string LeftStr(std::string, int)':
basic_str.cpp:15: error: expected primary-expression before string
constant
basic_str.cpp:15: error: expected `;' before string constant
basic_str.cpp:17: error: invalid use of member (did you forget the `&'
?)
basic_str.cpp:32:2: warning: no newline at end of file


Thanks.
 
S

sandwich_eater

your answer begs the question: does that not create a new instance in
the memory? If so if my function were called repeatedly there would be
multiple null strings in memory, I cannot be doing with that. Compiler
output is now...

basic_str.cpp: In function `std::string LeftStr(std::string, int)':
basic_str.cpp:15: error: conversion from `std::string*' to non-scalar
type `std:
:string' requested
basic_str.cpp:17: error: invalid use of member (did you forget the `&'
?)
basic_str.cpp:32:2: warning: no newline at end of file
 
P

Pelle Beckman

(e-mail address removed) skrev:
your answer begs the question: does that not create a new instance in
the memory? If so if my function were called repeatedly there would be
multiple null strings in memory, I cannot be doing with that. Compiler
output is now...

You have a point - read Rolfs answer.
He knows C++.
 
R

Rolf Magnus

I am getting compiler errors using g++ in cygwin due to my lack of
knowledge of C++.

How do each of the following with std::string?
1. write a header for a function that takes a string (most efficient
way e.g. by reference), an integer and returns a string.
2. return a null std::string

Actually, there is no such thing as a "null std::string". What you could do
is return an empty string.
3. see a documented list of functions implemented in std::string
4. make the function part of a class so that it extends the std::string
class

This is my first stab a LeftStr for starters as a test (i am not
passing by reference)

...
using namespace std;

string LeftStr(string s, int k)

This does not fulfill the first requirement. It is not most efficient,
because it does not take the string by reference.
{
if (k < 1)
{
return string "/0";

What is the above line supposed to do? It doesn't really make much sense.
Did you want to convert "/0" into a string? Or did you want to concatenate
"/0" to the string s? Or is this supposed to be the "null std::string" from
the second requirement?
If you want to return an empty string, try:

return string();
}
else if (s.length < k)

std::string::length is a function. You need to call it. Try:

else if (s.length() < k)
 
D

Donovan Rebbechi

I am getting compiler errors using g++ in cygwin due to my lack of
knowledge of C++.

How do each of the following with std::string?
1. write a header for a function that takes a string (most efficient
way e.g. by reference), an integer and returns a string.

you should use an unsigned type, since a negative number doesn't make sense
in this context. In fact size_t is probably most appropriate (sonce the
argument represents the length of something)

Also, use lower case for the first letter of a function name. Fully qualify
namespaces, especially in header files. Putting using in a header file is
bad.

std::string leftStr(const std::string& s, size_t k);

std::string leftStr(const std::string& s, size_t k) {
if (k==0)
return std::string();
else if (k<s.length())
return s;
else
return s.substr(1,k);
}
2. return a null std::string

Use the default constructor to create an empty (not "NULL" string)
3. see a documented list of functions implemented in std::string

C++ reference book. You could dig into the header on your implementation but
it may or may not be well documented.
4. make the function part of a class so that it extends the std::string
class

Not a good idea. You already are extending functionality, and the interface to
std::string is already "big" enough without anyone "extending" it. In C++ (as
opposed to Java), not everything has to be in a class. If you don't need it
to be a member function, then don't make it one.

Cheers,
 
D

Donovan Rebbechi

Thanks, that compiled nicely. I found this web page with many
std::string functions...

http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html

I have tutorials on my website also (see below), but this is no substitute
for a good reference, or even a good tutorial book.

I don't think the tutorial you cite is that good, it talks about differences
between implementations without discussing which implementation is correct,
or whether (as is the case with capacity()) both implementations are doing the
right thing even though the output is different.

Get yourself a copy of "Accelerated C++" if you don't already have one.

Cheers,
 
L

Larry I Smith

I am getting compiler errors using g++ in cygwin due to my lack of
knowledge of C++.

How do each of the following with std::string?
1. write a header for a function that takes a string (most efficient
way e.g. by reference), an integer and returns a string.
2. return a null std::string
3. see a documented list of functions implemented in std::string
4. make the function part of a class so that it extends the std::string
class

This is my first stab a LeftStr for starters as a test (i am not
passing by reference)

...
using namespace std;

string LeftStr(string s, int k)
{
if (k < 1)
{
return string "/0";
}
else if (s.length < k)
{
return s;
}
else
{
return s.substr (1, k);
}
}

compiler output

basic_str.cpp: In function `std::string LeftStr(std::string, int)':
basic_str.cpp:15: error: expected primary-expression before string
constant
basic_str.cpp:15: error: expected `;' before string constant
basic_str.cpp:17: error: invalid use of member (did you forget the `&'
?)
basic_str.cpp:32:2: warning: no newline at end of file


Thanks.

Note that std::string does not use a nul-terminator ('\0')
as does a C string. In fact, arbitrary binary data can
be stored in a std::string.

std::string is usually a typedef for std::basic_string
that holds a sequence of char. Here's one reference:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/HTML/STRING2.asp

Most of the other methods are found by clicking the "basic_string"
link on the above page.

#include <string>

std::string LeftStr(const std::string& s, int k)
{
if (k < 1)
{
return std::string(); // return an empty string
}
else if (s.length() < k)
{
return s;
}
else
{
return s.substr (0, k); // zero based indexes
}
}


Actually

return s.substr(0, k);

works even when 'k' is greater than s.length().

So, you might experiment with this:

#include <iostream>
#include <string>

std::string LeftStr(const std::string& s, int k)
{
if (k > 0)
return s.substr(0, k);

return std::string(); // return an empty string
}

int main()
{
std::string s3 = LeftStr("hello there", -1);
std::string s4 = LeftStr("hello there", 5);
std::string s5 = LeftStr("hello there", 50);

std::cout << "'" << s3 << "'" << std::endl
<< "'" << s4 << "'" << std::endl
<< "'" << s5 << "'" << std::endl;

return 0;
}

The above program should output:

''
'hello'
'hello there'


Larry
 
S

sandwich_eater

OK, using "size_t k" produces a different result because it cannot
store negative values, but I can't think of a practical situation where
a negative value would occur.
So a more minimal version is...

std::string leftstr(const std::string& s, size_t k)
{
return s.substr(0, k);
}

which makes the function seem somewhat unneccessary, but then it is
just a learning exercise.
 

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
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top