C++高性能并行编程与优化 - 课件 - 13 C++ STL 容器全解之 vectorvector 容器: resize • 除了可以在构造函数中指定数组的大小,还可以 之后再通过 resize 函数设置大小。 • 这在无法一开始就指定大小的情况下非常方便。 • vectora(4); • 等价于: • vector a; • a.resize(4); • void resize(size_t n); vector 容器: resize • 当然, 当然, resize 也有一个接受第二参数的重载 ,他会用这个参数的值填充所有新建的元素。 • vector a(4, 233); • 等价于: • vector a; • a.resize(4, 233); • void resize(size_t n, int const &val); vector 容器: resize • 调用 resize(n) 的时候,如果数组里面不足 个元素,前 m 个元素会保持不变。 • vector a = {1, 2}; • a.resize(4); • 等价于: • vector a = {1, 2, 0, 0}; • void resize(size_t n); vector 容器: resize • 调用 resize(n) 的时候,如果数组已有超过 n 个元素,假设是 m 个,则他会删除多出来的 m 0 码力 | 90 页 | 4.93 MB | 1 年前3
MACRO-FREE TESTING WITH C++20std::vectorv(5); expect((5_ul == std::size(v)) >> fatal); should("resize bigger") = [v] { // section (2.1) mut(v).resize(10); expect(10_ul == std::size(v)); }; }; 5 / 14SECTIONS - SECTIONS std::vector v(5); expect((5_ul == std::size(v)) >> fatal); should("resize bigger") = [v] { // section (2.1) mut(v).resize(10); expect(10_ul == std::size(v)); }; expect((5_ul == std::size(v) fatal); should("resize bigger") = [v] { // section (2.1) mut(v).resize(10); expect(10_ul == std::size(v)); }; expect((5_ul == std::size(v) >> fatal); // (3) should("resize smaller") = [v] 0 码力 | 53 页 | 1.98 MB | 6 月前3
C++23: An Overview of Almost All New and Updated Featuresstd::generator basic_string(_view)::contains() Construct string(_view) From nullptr basic_string::resize_and_overwrite() Monadic Operations for std::optional Stacktrace Library Changes to Ranges std::generator basic_string(_view)::contains() Construct string(_view) From nullptr basic_string::resize_and_overwrite() Monadic Operations for std::optional Stacktrace Library Changes to Ranges std::generator basic_string(_view)::contains() Construct string(_view) From nullptr basic_string::resize_and_overwrite() Monadic Operations for std::optional Stacktrace Library Changes to Ranges0 码力 | 105 页 | 759.96 KB | 6 月前3
Cache-Friendly Design in Robot Path Planningvoid reset(G&& graph, VertexID s, VertexID g) 16 { 17 // Clear visited set 18 visited_.resize(graph.vertex_count()); 19 visited_.assign(graph.vertex_count(), graph.vertex_count()); 20 void reset(G&& graph, VertexID s, VertexID g) 16 { 17 // Clear visited set 18 visited_.resize(graph.vertex_count()); 19 visited_.assign(graph.vertex_count(), graph.vertex_count()); 20 while (!queue_.empty()) { queue_.pop(); } 23 // Set current goal 24 goal = g; 25 visited_.resize(graph.vertex_count()); visited_.assign(graph.vertex_count(), graph.vertex_count()); private:0 码力 | 216 页 | 10.68 MB | 6 月前3
Better Code: Exploring Validitysize() == us.size() public: PairSequence(const PairSequence &); void push_back(pair); void resize(size_t x); ~PairSequence(); PairSequence& operator=(const PairSequence&); }; Unless a function’s size() == us.size() public: PairSequence(const PairSequence &); void push_back(pair ); void resize(size_t x); ~PairSequence(); PairSequence& operator=(const PairSequence&); }; Unless a function’s size() == us.size() public: PairSequence(const PairSequence &); void push_back(pair ); void resize(size_t x); ~PairSequence(); PairSequence& operator=(const PairSequence&); }; Unless a function’s 0 码力 | 117 页 | 6.03 MB | 6 月前3
Leveraging a Functional Approach for More Testable and Maintainable ROS 2 Codenew_end = std::remove_if(input.begin(), input.end() , less_than_5); // input = {6, 9, 12, 9, 12} input.resize(std::distance(input.begin(), new_end)); // input = {6, 9, 12} const auto result = std::accumulate(input new_end = std::remove_if(input.begin(), input.end() , less_than_5); // input = {6, 9, 12, 9, 12} input.resize(std::distance(input.begin(), new_end)); // input = {6, 9, 12} const auto result = std::accumulate(input new_end = std::remove_if(input.begin(), input.end() , less_than_5); // input = {6, 9, 12, 9, 12} input.resize(std::distance(input.begin(), new_end)); // input = {6, 9, 12} const auto result = std::accumulate(input0 码力 | 200 页 | 1.77 MB | 6 月前3
C++高性能并行编程与优化 - 课件 - 15 C++ 系列课:字符与字符串string(“hello”) + string(“world”) == string(“helloworld”) • string 符合 vector 的接口,例如 begin/end/size/resize…… • string 有一系列成员函数,例如 find/replace/substr…… • string 可以通过 s.c_str() 重新转换回古板的 const char * 。 replace(pos, len, str) : • 如果 pos ≥ s.size() 则抛出 out_of_range 异常。 • 如果 pos + len > s.size() 则会扩容字符串 s.resize(pos + len) 。 append 追加一段字符串 • string s = “hello”; • s += “world”; • 最后 s 会得到 “ helloworld” 。 为什么是这样?小彭老师也无从得知,可能是历史原因。 • 猜想是因为 const char * 指针可以自身进行 += 操作来去除开头的 任意部分,所以要让 len 控制尾部的部分;而 string 类型可以自 身进行 resize 操作来去除尾部的任意部分,所以用 len 控制开头 的部分。 • 为了一点点破性能,弄这么多重载,不过这些都已经无所谓了,因 为 C++17 中有更为直观的 string_view ,要切片只需0 码力 | 162 页 | 40.20 MB | 1 年前3
Contracts for C++https://timur.audio // vector.h T& operator[] (size_t i); // can't put assert macro here :( void resize(size_t n); // can't put assert macro here :( P2900 pre / post can go on declarations79 Copyright can't put assert macro here :( void resize(size_t n); // can't put assert macro here :( // vector.h T& operator[] (size_t i) pre (i < size()); void resize(size_t n) post (size() == n); can't put assert macro here :( void resize(size_t n); // can't put assert macro here :( // vector.h T& operator[] (size_t i) pre (i < size()); void resize(size_t n) post (size() == n);0 码力 | 181 页 | 4.44 MB | 6 月前3
Design patterns for error handling in C++ programs using parallel algorithms and executorsstatus & SOLVE_FAILED) { useSlowerSolver = true; } else if(result.status & OUT_OF_MEM) { pool.resize(result.bytesNeeded); retry = true; }Parallel domain decomposition Reduce over error info • Each status & SOLVE_FAILED) { useSlowerSolver = true; } else if(result.status & OUT_OF_MEM) { pool.resize(result.bytesNeeded); retry = true; }Parallel domain decomposition Reduce over error info • Each status & SOLVE_FAILED) { useSlowerSolver = true; } else if(result.status & OUT_OF_MEM) { pool.resize(result.bytesNeeded); retry = true; }Parallel domain decomposition Reduce over error info • Each0 码力 | 32 页 | 883.27 KB | 6 月前3
MoonBit月兔编程语言 现代编程思想 第十课 哈希表与闭包
17. } } } } 18. if map.size.to_double() / map.length.to_double() >= load { // 根据负载重新分配 19. resize() 20. } 21. } 8 哈希表:直接寻址 虽然不存在数组⽤尽的问题,但仍需要扩容重新分配 负载:键值对数量与数组⻓度的⽐值 当负载上升,哈希/索引冲突变多,链表增⻓,增查改删操作时间增⻓ // 确认负载是否需要扩容 11. if map.size.to_double() / map.length.to_double() >= 0.75 { 12. resize(map) // fn resize(map) -> Unit 13. } 14. } 15 哈希表:开放寻址 删除操作需考虑不变性:键值对应当存放位置与实际存放位置之间不存在空位 0, 5 1 3 initial_length, 8. } 9. fn initialize() { ... } // 需要对数组进⾏挨个初始化 10. initialize() 11. 12. fn resize() { ... } 13. 14. fn get(key : K) -> Option[V] { ... } 15. fn put(key : K, value : V) -> Unit0 码力 | 27 页 | 448.83 KB | 1 年前3
共 381 条
- 1
- 2
- 3
- 4
- 5
- 6
- 39
相关搜索词
C++高性性能高性能并行编程优化课件13MACROFREETESTINGWITH2023AnOverviewofAlmostAllNewandUpdatedFeaturesCacheFriendlyDesigninRobotPathPlanningBetterCodeExploringValidityLeveragingFunctionalApproachforMoreTestableMaintainableROS15ContractspatternserrorhandlingprogramsusingparallelalgorithmsexecutorsMoonBit语言编程语言现代思想第十十课第十课哈希表与闭包













