stl

C++ stl 标准程序库实例源代码

送分小仙女□ 提交于 2020-02-24 19:30:22
#include <iostream> #include <stdio.h> using namespace std; int main() { cout<<"Welcome to the C ++ Standard Template Library!!!"<<endl; getchar(); //等待输入任意键,之后程序退出; return 0; } 执行结果: Welcome to the C ++ Standard Template Library!!! #include <iostream> #include <cmath> #include <stdio.h> using namespace std; int main() { float pi=3.1415916; float radius=0; float area=0.0; float girth=0.0; cout<<"circle->radius:"<<endl; cin>>radius; area=pi*pow(radius,2); //使用数学函数pow(),乘方 girth=2*pi*radius; //周长 cout<<"area="<<area<<";girth="<<girth<<endl; getchar(); getchar(); //等待程序退出 return 0; } 执行结果: circle

C++ 使用STL string 实现的split,trim,replace

戏子无情 提交于 2020-02-24 18:13:12
实现string 的去除两边空格,按指定字符截取,替换 #include <iostream> #include <vector> using namespace std; namespace strtool { string trim(const string& str) { string : :size_type pos = str.find_first_not_of(' ') ; if (pos == string : :npos) { return str ; } string::size_type pos2 = str.find_last_not_of(' '); if (pos2 != string::npos) { return str.substr(pos, pos2 - pos + 1); } return str.substr(pos); } int split(const string& str, vector<string>& ret_, string sep = ",") { if (str.empty()) { return 0; } string tmp; string::size_type pos_begin = str.find_first_not_of(sep); string::size_type comma_pos = 0; while

uninitialized_copy memcpy/memmove optimization

 ̄綄美尐妖づ 提交于 2020-02-24 06:51:48
问题 I've recently started to examine the STL in the MSVC's implementation. There are some nice tricks there, however I don't know why the following criteria is used. The std::uninitialized_copy is optimized to a simple memcpy/memmove if some conditions are met. As my understanding the input range can be memcpy 'd to the uninitialized area if the target type U is_trivially_copy_constructible from source type T. However the MSVC implementation checks a hell lot of thing before choosing the memcpy

uninitialized_copy memcpy/memmove optimization

时光毁灭记忆、已成空白 提交于 2020-02-24 06:51:15
问题 I've recently started to examine the STL in the MSVC's implementation. There are some nice tricks there, however I don't know why the following criteria is used. The std::uninitialized_copy is optimized to a simple memcpy/memmove if some conditions are met. As my understanding the input range can be memcpy 'd to the uninitialized area if the target type U is_trivially_copy_constructible from source type T. However the MSVC implementation checks a hell lot of thing before choosing the memcpy

转:STL:string 大小(Size)和容量(Capacity)

五迷三道 提交于 2020-02-23 10:51:45
strings存在三种“大小”: 1、 size ()和length()  返回string中现在的字符个数。上述两个函数等效。 成员函数 empty ()用来检验字符数是否 为0 ,亦即字符串是否为空。你应该优先使用该函数,因为它比length()或 size ()来得快。 也就是说,使用if(s. empty () == true)而不使用if(s. size () == 0) (笔者注) 2、max_ size ()   此函数返回一个string最多能够包含的字符数。一个string通常包含一块单独内存区块内的所有字符,所以可能跟PC机器本省的限制有关系。返回值一般而言是索引型别的最大值减1。之所以“减1”有两个原因: (a)最大值本身是npos;(b)在具体实现中,可因此轻易在内部缓冲区之后添加一个'\0',以便将这个string当做C-string使用(例如透过c_str()) 。一旦某个操作函数使用一个长度大于max_ size ()的string,length_error异常就会被抛出来。 3、capacity() 重新分配内存之前,string所能包含的最大字符数。 让string拥有足够的容量是很重要的,原因有二: 1、重新分配会造成所有指向 string的 references,pointer和iterators失效。 2、重新分配(reallocation

C++ STL介绍——简介

守給你的承諾、 提交于 2020-02-22 22:48:07
目录 1、什么是STL 2、STL中六大组件 2.1 容器(Container) 2.2 迭代器(Iterator) 2.3 算法(Algorithm) 2.4 仿函数(Functor) 2.5 适配器(Adaptor) 3、其他部分链接 @目录 1、什么是STL STL Standard Template Library ,即标准模板库,是一个具有工业强度的,高效的C++ 程序库。它被容纳于C++ 标准程序库 C++ Standard Library 中,是 ANSI/ISO C++ 标准中最新的也是极具革命性的一部分。该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法。为广大C++程序员们提供了一个可扩展的应用框架,高度体现了软件的可复用性。 STL的一个重要特点是数据结构和算法的分离。尽管这是个简单的概念,但这种分离确实使得STL变得非常通用。例如,由于STL的 sort() 函数是完全通用的,你可以用它来操作几乎任何数据集合,包括链表,容器和数组; STL另一个重要特性是它不是面向对象的。为了具有足够通用性,STL主要依赖于模板而不是封装,继承和虚函数(多态性)——OOP的三个要素。你在STL中找不到任何明显的类继承关系。这好像是一种倒退,但这正好是使得STL的组件具有广泛通用性的底层特征。另外,由于STL是基于模板,内联函数的使用使得生成的代码短小高效;

STL容器迭代过程中删除元素技巧

本秂侑毒 提交于 2020-02-22 22:32:56
1. 连续内存序列容器 (vector,string,deque) 序列容器的 erase 方法返回值是指向紧接在被删除元素之后的元素的有效迭代器,可以根据这个返回值来安全删除元素。 vector<int> c; for(vector<int>::iterator it = c.begin(); it != c.end();) { if(need_delete()) it = c.erase(it); else ++it; } 2. 关联容器 (set,multiset,map,multimap) 关联容器的 erase 方法没有返回值,被删除的迭代器失效,所以删除前必须确保能得到下一个迭代器,可以用 “ 后置递增迭代器 ” 技术。 map<int,int> m; for(map<int,int>::iterator it = m.begin(); it != m.end();) { if(need_delete()) m.erase(it++); else ++it; } m.erase 得到 it 的一个副本,在 erase 真正开始之前 it 已经递增了。 所以 erase 得到了当前的迭代器,在 erase 内部工作开始之前 it 已经 ++ 了,正好满足我们的需要。 3. 非连续内存序列容器 (list) 只所以单独列出来是因为以上两种方法对 list 都适用

STL简介

送分小仙女□ 提交于 2020-02-22 21:49:35
1、简介 (1)STL 是 C++ 标准程序库的核心。 STL 内所有组件都由模板构成,其元素可以是任意类别。 (2)STL译为标准模板库。STL从根本上讲是“容器”的集合,也是组件的集合。 容器包括:list、vector、set、map等,组件包括迭代器、算法等。 (3) STL的算法是标准算法 ,可以把STL已经定义的算法应用在容器的对象上。 2、组成构件 STL 的组件中最主要的是容器、迭代器、算法和仿函数。 (1)容器:用来管理某类对象的集合 (2)迭代器:用来在一个对象群集的元素上进行遍历动作 (3)算法:用来处理群集内的元素 3、基本结构 STL 是 C++ 通用库,由容器、算法、迭代器、仿函数和内存配置器组成。 (1)容器 Vector<T> ,是一种向量 List<T> ,是一种双向链表容器 Queue<T>,是一种队列容器 Stack<T>,是一种栈容器 Deque<T>,是双端队列容器 Set<T> ,是一种集合容器 Map<key,val> ,是一种关联数组容器 (2)算法 STL提供了非常多的数据结构算法。 这些算法在命名空间std的范围内定义,通过包含头文件<algorithm>来获得使用权。 STL中所有的算法都是基于模板实现的。 (3)迭代器 通俗的讲,迭代器就是指示器,能够使程序反复对数据进行访问,为访问数据提供了通用的接口,类似于C++的指针

Why is is it not possible to pass a const set<Derived*> as const set<Base*> to a function?

亡梦爱人 提交于 2020-02-22 12:20:22
问题 Before this is marked as duplicate, I'm aware of this question, but in my case we are talking about const containers. I have 2 classes: class Base { }; class Derived : public Base { }; And a function: void register_objects(const std::set<Base*> &objects) {} I would like to invoke this function as: std::set<Derived*> objs; register_objects(objs); The compiler does not accept this. Why not? The set is not modifiable so there is no risk of non-Derived objects being inserted into it. How can I do

Why is is it not possible to pass a const set<Derived*> as const set<Base*> to a function?

隐身守侯 提交于 2020-02-22 12:20:12
问题 Before this is marked as duplicate, I'm aware of this question, but in my case we are talking about const containers. I have 2 classes: class Base { }; class Derived : public Base { }; And a function: void register_objects(const std::set<Base*> &objects) {} I would like to invoke this function as: std::set<Derived*> objs; register_objects(objs); The compiler does not accept this. Why not? The set is not modifiable so there is no risk of non-Derived objects being inserted into it. How can I do