M
mathieu
Hello there,
Could anyone comment on the following code(*). I would like to
optimize the following line:
is.str( std::string(s,4) );
Seems to me that allocation+initialization of the std::string is a
bit complex for such simple task. Looks to me that I should be able to
manipulate the rdbuf internals.
Comments welcome, thanks.
Mathieu
(*)
#include <iostream>
#include <sstream>
int main()
{
// 9728
const char s[] = "\0&\0\0";
uint32_t n = 0;
memcpy(&n, s, 4);
std::cout << n << std::endl;
uint32_t nn = 0;
std::istringstream is;
is.str( std::string(s,4) ); // <----- this line
is.read( reinterpret_cast<char*>(&nn), 4);
std::cout << is.str() << "->" << nn << std::endl;
std:stringstream os;
os.write( reinterpret_cast<char*>(&n), 4);
std::cout << n << "->" << os.str() << std::endl;
return 0;
}
Could anyone comment on the following code(*). I would like to
optimize the following line:
is.str( std::string(s,4) );
Seems to me that allocation+initialization of the std::string is a
bit complex for such simple task. Looks to me that I should be able to
manipulate the rdbuf internals.
Comments welcome, thanks.
Mathieu
(*)
#include <iostream>
#include <sstream>
int main()
{
// 9728
const char s[] = "\0&\0\0";
uint32_t n = 0;
memcpy(&n, s, 4);
std::cout << n << std::endl;
uint32_t nn = 0;
std::istringstream is;
is.str( std::string(s,4) ); // <----- this line
is.read( reinterpret_cast<char*>(&nn), 4);
std::cout << is.str() << "->" << nn << std::endl;
std:stringstream os;
os.write( reinterpret_cast<char*>(&n), 4);
std::cout << n << "->" << os.str() << std::endl;
return 0;
}