E
Eric Lilja
Hello, consider the following program:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
const char *foo = "hello";
std::string s;
/* Prints olleh */
std::reverse_copy(foo, foo + std::strlen(foo),
std:stream_iterator<char>(std::cout));
std::reverse_copy(foo, foo + std::strlen(foo), s.begin());
/* Prints nothing, why? */
std::cout << std::endl << s << std::endl;
}
The second call to reverse_copy doesn't work as expected because when I
print s I get nothing. Using a debugger, I inspect s just before and
right after the call to reverse_copy:
(gdb) p s
$1 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
{<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
0x44212c ""}}
(gdb) n
(gdb) p s
$2 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
{<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
0x71254c "olleh"}}
(gdb)
So it seems to contain the reversed string, but it won't output?!
What's going on?
- Eric
#include <algorithm>
#include <cstring>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
const char *foo = "hello";
std::string s;
/* Prints olleh */
std::reverse_copy(foo, foo + std::strlen(foo),
std:stream_iterator<char>(std::cout));
std::reverse_copy(foo, foo + std::strlen(foo), s.begin());
/* Prints nothing, why? */
std::cout << std::endl << s << std::endl;
}
The second call to reverse_copy doesn't work as expected because when I
print s I get nothing. Using a debugger, I inspect s just before and
right after the call to reverse_copy:
(gdb) p s
$1 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
{<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
0x44212c ""}}
(gdb) n
(gdb) p s
$2 = {static npos = 4294967295, _M_dataplus = {<allocator<char>> =
{<new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p =
0x71254c "olleh"}}
(gdb)
So it seems to contain the reversed string, but it won't output?!
What's going on?
- Eric