We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In C++, String need contiguous memory to store sequence of character. When you need to expand string by insert or append, If current capacity of string is not enough, it need to allocation memmory with proper capacity, and copy old string to this position before expand this string. So it need many time to do all this action and make time out in this challenges because you append string many times. ostringstream don't need contiguous memory. It store list of string object. In addition, If you know exactly maximum size of string, you can using reserve function to avoid it allocate memory many times.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
In C++, String need contiguous memory to store sequence of character. When you need to expand string by insert or append, If current capacity of string is not enough, it need to allocation memmory with proper capacity, and copy old string to this position before expand this string. So it need many time to do all this action and make time out in this challenges because you append string many times. ostringstream don't need contiguous memory. It store list of string object. In addition, If you know exactly maximum size of string, you can using reserve function to avoid it allocate memory many times.