Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, is the amortized complexity of std::string::push_back() O(1)?

I know the standard specifies that it is for vectors, but what about strings?

like image 373
Ari Avatar asked Dec 15 '12 14:12

Ari


People also ask

What is the time complexity of Push_back in vector C++?

So time complexity of push_back() is O(1).

Does Push_back work on strings C++?

std::string::push_back() in C++The push_back() member function is provided to append characters. Appends character c to the end of the string, increasing its length by one. Syntax : void string:: push_back (char c) Parameters: Character which to be appended.

Is vector Push_back constant time?

The amortized time of vector::push_back is constant.

What is .append in C++?

Append is a special function in the string library of C++ which is used to append string of characters to another string and returns * this operator. This is similar to push_back or += operator, but it allows multiple characters to append at the same time.


1 Answers

Yes, it is amortized constant time. See table 101 on page 716 of this document:

Table 101 lists operations that are provided for some types of sequence containers but not others. An implementation shall provide these operations for all container types shown in the “container” column, and shall implement them so as to take amortized constant time.

Operation      | Description          | Container
---------------+----------------------+----------------------------------
a.push_back(t) | Appends a copy of t. | basic_string, deque, list, vector
like image 140
Sergey Kalinichenko Avatar answered Sep 22 '22 06:09

Sergey Kalinichenko