[C++] Odd Problem with "substr" | | |
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 | | | | re: [C++] Odd Problem with "substr"
entropy123 wrote:[color=blue]
> 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?[/color]
RTFM. Pay attention to the meaning of the second argument.
V | | | | re: [C++] Odd Problem with "substr"
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 | | | | re: [C++] Odd Problem with "substr"
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;
entropy123 wrote:[color=blue]
> 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[/color] | | | | re: [C++] Odd Problem with "substr"
entropy123 wrote:[color=blue]
> 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
>[/color]
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 | | | | re: [C++] Odd Problem with "substr"
Krishanu Debnath wrote:[color=blue]
> entropy123 wrote:[color=green]
> > 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
> >[/color]
>
> 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[/color]
'Doh! | | | | re: [C++] Odd Problem with "substr"
Victor Bazarov wrote:[color=blue]
> RTFM.[/color]
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. | | | | re: [C++] Odd Problem with "substr"
> The new way of[color=blue]
> thinking assumes that if users make dumb mistakes it's the designer's
> fault (beginning 21th century).[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,414 network members.
|