1

For example, the following code reverses a string, but what actually goes on when I'm adding each character to a string? Is it efficient? Does it create a new char array every time, or does it double the size every time it fills up, or?:

#include <iostream>
#include <stack>
using namespace std;

int main()
{
    string test = "testing";

    stack<char> first;

    for(int i = 0; test[i]; i++)
    {
        first.push(test[i]);
    }
    string reversed;
    int i=0;
    while(!first.empty())
    {
        reversed += first.top();
        first.pop();
    }
    cout << test << reversed;
}
user120920
  • 89
  • 2
  • 9
  • 1
    Why don't you use [std::reverse](http://en.cppreference.com/w/cpp/algorithm/reverse)? – Mohit Jain Jan 14 '15 at 07:08
  • 1
    Read this SO post to know how `string` works http://stackoverflow.com/questions/1466073/how-is-stdstring-implemented – Ankur Jan 14 '15 at 07:10
  • 2
    The use of the stack is unnecessary and adds an unneeded inefficiency. A more efficient way would be `string reversed(test.rbegin(), test.rend());`. – juanchopanza Jan 14 '15 at 07:11
  • 1
    @all The question is about using string as a efficient source not about reverse – Ankur Jan 14 '15 at 07:14
  • @Shan Yet the most efficient way to create a string which is a reversed version of another one is probably the one I showed. – juanchopanza Jan 14 '15 at 07:36
  • @juanchopanza sorry if I wasn't clear in my question but Shan is correct. I do not care about the efficiency of reversing a string in my example, I only included that as an example to show what I was asking. My question is only about what happens when adding a character to a string. – user120920 Jan 14 '15 at 07:50
  • You could print `reversed.capacity()` in every iteration and see how many times string buffer is resized. – el.pescado - нет войне Jan 14 '15 at 09:14

2 Answers2

1

Yes the code is non-efficient. You should use std::reverse. But if you still want to stick with your approach, use std::string::reserve to increase the capacity of string.

string reversed;
reversed.capacity(test.size());

When you add character by character to std::vector and std::string, they resize when the capacity is exhausted by a constant factor. (typically 2, but may be different)

This exponential growth makes sure that cost of insertion is constant asymptotically.

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • 1
    I guess he is asking about `string` library not about reverse. – Ankur Jan 14 '15 at 07:12
  • Sorry, I knew that the stack was inefficient, but I was primarily wondering about the actual string concatenation part of it and what happens when you add a character to the string that way. I understand there are better implementations of reverse and even one in the standard library, but just wanted to give an easy to follow example for adding characters to a string. – user120920 Jan 14 '15 at 07:12
1

When a string is added to another string, in the case of + operator, the resultant string is move constructed.

String a("hello"); a+="goodmorning";

A string uses a char buffer underneath, and the size is set to default value at construction time.You can alter it using reserve.But during concatenation, if the reserved size is exceeded, then the buffer has to be extended.

basav
  • 1,475
  • 12
  • 20