3 ms·
Not the same. Your version creates a vector with 4 times the default of std::string. #include <string> #include <iostream> #include <vector> t
by WirelessGigabit 4y ago
Not the same. Your version creates a vector with 4 times the default of std::string.
#include <string>
#include <iostream>
#include <vector>
template < typename T >
std::ostream & operator<< (std::ostream & s, const std::vector < T > &v)
{
s.put ('[');
char comma[3] = { '\0', ' ', '\0' };
for (const auto & e:v)
{
s << comma << e;
comma[0] = ',';
}
return s << ']';
}
int main ()
{
std::vector < std::string > with_reservation;
with_reservation.reserve (4);
std::cout << "with_reservation: " << with_reservation << std::endl;;
std::vector < std::string > via_ctor (4);
std::cout << "via_ctor: " << via_ctor << std::endl;;
return 0;
}
Yields
with_reservation: []
via_ctor: [, , , ]
https://onlinegdb.com/GUlVHoqC5z https://onlinegdb.com/GUlVHoqC5z