[C++] Odd Problem with "substr"

E

entropy123

My problem is with the following test code:

#include<string>;

std::string TestBuff;
TestBuff.append("A + B <==> C + D");
std::cout << "TestBuff.length():" << TestBuff.length() << endl;
std::cout << TestBuff.substr(0,5) << endl;
std::cout << TestBuff.substr(6,10) << endl;

The output:
TestBuff.length():16
A + B
<==> C + D

The last line of output is what seems like a mistake to me. The output
should be: " <==>" but instead the entire remaining line is printed
out. I would expect the output if 10 were greater than the buffer
length, but its not.

???

Any advice here?

Thanks,
ent
 
V

Victor Bazarov

entropy123 said:
My problem is with the following test code:

#include<string>;

std::string TestBuff;
TestBuff.append("A + B <==> C + D");
std::cout << "TestBuff.length():" << TestBuff.length() << endl;
std::cout << TestBuff.substr(0,5) << endl;
std::cout << TestBuff.substr(6,10) << endl;

The output:
TestBuff.length():16
A + B
<==> C + D

The last line of output is what seems like a mistake to me. The output
should be: " <==>" but instead the entire remaining line is printed
out. I would expect the output if 10 were greater than the buffer
length, but its not.

???

Any advice here?

RTFM. Pay attention to the meaning of the second argument.

V
 
M

Marcelo Pinto

The substr member function take tow parameters, the first is the
initial position and the second is the maximum number of characters to
copy. Thus you have said to TestBuff that you want the substring
starting at position 6 with at most 10 characters.

The result is correct.

Regards,

Marcelo Pinto
 
J

jamie.peabody

The substr copies n characters from a position. In your case, it will
copy 10 characters from position 6. If you want only "<==>", then the
line is:

std::cout << TestBuff.substr(6,4) << endl;
 
K

Krishanu Debnath

entropy123 said:
My problem is with the following test code:

#include<string>;

std::string TestBuff;
TestBuff.append("A + B <==> C + D");
std::cout << "TestBuff.length():" << TestBuff.length() << endl;
std::cout << TestBuff.substr(0,5) << endl;
std::cout << TestBuff.substr(6,10) << endl;

The output:
TestBuff.length():16
A + B
<==> C + D

The last line of output is what seems like a mistake to me. The output
should be: " <==>" but instead the entire remaining line is printed
out. I would expect the output if 10 were greater than the buffer
length, but its not.

???

Any advice here?

Thanks,
ent

Specifying postions for string member functions are not similar like other containers.
It is generally specified as (pos, range) manner. So you can think of you are accessing
elements of [pos, pos + range).

Krishanu
 
E

entropy123

Krishanu said:
entropy123 said:
My problem is with the following test code:

#include<string>;

std::string TestBuff;
TestBuff.append("A + B <==> C + D");
std::cout << "TestBuff.length():" << TestBuff.length() << endl;
std::cout << TestBuff.substr(0,5) << endl;
std::cout << TestBuff.substr(6,10) << endl;

The output:
TestBuff.length():16
A + B
<==> C + D

The last line of output is what seems like a mistake to me. The output
should be: " <==>" but instead the entire remaining line is printed
out. I would expect the output if 10 were greater than the buffer
length, but its not.

???

Any advice here?

Thanks,
ent

Specifying postions for string member functions are not similar like other containers.
It is generally specified as (pos, range) manner. So you can think of you are accessing
elements of [pos, pos + range).

Krishanu

'Doh!
 
R

Rapscallion

Victor said:

This is the old way of thinking (late '80s, I suppose). The new way of
thinking assumes that if users make dumb mistakes it's the designer's
fault (beginning 21th century). Because it's his/her dumb interface
that causes dumb user errors. But obviously not all have yet arrived in
the new millenium.
 
S

Stephen Howe

The new way of
thinking assumes that if users make dumb mistakes it's the designer's
fault (beginning 21th century).

A false assumption.
There is a level of complexity of interfaces beyond which they cannot be
simplified.

There is nothing whatsoever complex about std::string's interface.
It is the programmer's fault if he/she did not study the documentation of
std::string's interface.

Stephen Howe
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top