i_am_eating_wa_and_tle's blog

By i_am_eating_wa_and_tle, history, 10 months ago, In English

As far as I know stack.push() have a O(1) time complexity but is it actually correct if I use a stack of string.

stack<string> stringStack;
string inputString;
while(cin >> inputString) {
    stringStack.push(inputString); // what is the time complexity of this line
}
»
10 months ago, # |
  Vote: I like it -23 Vote: I do not like it

It's constant, O(1).

The operation involves updating the reference or pointer to the new element, and this process is independent of the size of the string.