C++高性能并行编程与优化 - 课件 - 15 C++ 系列课:字符与字符串+ len) 。 append 追加一段字符串 • string s = “hello”; • s += “world”; • 最后 s 会得到 “ helloworld” 。 • 这里的 += 也可以换做 append 函数,基本一样,没什么好说的。 • 例如 s.append(“world”) 和 s += “world” 等价。 • 区别在于 append 还可以指定第二个参数,限定字符串长度,用于要追加的字符串已 经确定长度,或者是个切片的情况( string_view )。 • 例如 s.append(“world”, 3) 和 s += string(“world”, 3) 和 s += “wor” 等价。 • 性能如何? append 的扩容方式和 vector 的 push_back 一样,每次超过 capacity 就预留两倍空间,所以重复调用 append 的复杂度其实是 amortized O(n) 的。 • • string &append(string const &str); // str 是 C++ 字符串类 string 的对象 • string &append(const char *s); // s 是长度为 strlen(s) 的 0 结尾字符串 • string &append(string const0 码力 | 162 页 | 40.20 MB | 1 年前3
C++23: An Overview of Almost All New and Updated Featurescritical Effect: If count <= size(), erase the last size() - count elements If count > size(), append (count - size()) default-initialized elements Invokes r = op(data(), count) Invokes erase(begin() 'count' nulls // - Updates size and checks for potential resize 'count' times result.append(pattern); } return result; }59 basic_string::resize_and_overwrite() E.g.: std::string0 码力 | 105 页 | 759.96 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 13 C++ STL 容器全解之 vectorinsert 函数,插入位置是倒数第 2 个 • a.begin() 可以插入到开头位置。 • a.begin() + 1 可以插入到第二个元素位置。 • a.end() 可以插入到最末尾( append )。 • a.end() - 1 则是插入到倒数第一个元素前。 • end() 迭代器的减法和是 Python 中负数作为 下标的情况很像的,不过 C++ 更加明确是从 end 开始往前数的。0 码力 | 90 页 | 4.93 MB | 1 年前3
共 3 条
- 1













