What is the complexity of std::string::substr
member function? Is it defined by standard or implementation-defined?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
The C++11 standard does not define the performance characteristics of
substr
, either in 21.4.7.8 or anywhere else I could find. In practice you can almost certainly expectO(n)
performance withn
being the length of the result.A naïve implementation would be O(k) where k is the length of the resulting substring. std::string does not support copy-on-write. If you want O(1) substring operations, use a data structure such as a Rope.
This is all that standard has to say about it:
n3242, 21.4.7.8
So the answer would be no, the complexity is not defined.
EDIT: Corrected as per n3242, pos > size not pos >= size