C++ STL 教程 在前面的章节中,我们已经学习了 C++ 模板的概念。 它会产生下列结果: vector size = 0 extended vector size = 5 value of vec [0] = 0 value of vec [1] = 1 value of vec [2] = 2 value of vec [3] = 3 value of vec [4] = 4 value of v = 0 value of v = 1 value of v = 2 value of
C++ STL 教程在前面的章节中,我们已经学习了 C++ 模板的概念。 它会产生下列结果:vector size = 0extended vector size = 5value of vec [0] = 0value of vec [1] = 1value of vec [2] = 2value of vec [3] = 3value of vec [4] = 4value of v = 0value of v = 1value of v = 2value of v = 3value
= clock(); for (auto e : arr) { arr2.insert(e); } size_t end2 = clock(); cout << "unordered_set :" << end1 - begin1 << endl; cout << "set:" << end2 - begin2 << endl; } 这里set更慢一些,因为插入的是随机值,如果是有序的 = clock(); for (auto e : arr) { arr2.insert(e); } size_t end2 = clock(); cout << "unordered_set :" << end1 - begin1 << endl; cout << "set:" << end2 - begin2 << endl; size_t begin3 = clock(); for = clock(); for (auto e : arr) { arr2.insert(e); } size_t end2 = clock(); cout << "unordered_set
> d1; //无参构造函数 for (int i = 0; i < 10; i++) { d1.push_back(i); } printDeque(d1); deque<int> d2( d1.begin(),d1.end()); printDeque(d2); deque<int>d3(10,100); printDeque(d3); deque<int>d4 = d3; d2 = d1; printDeque(d2); deque<int>d3; d3.assign(d1.begin(), d1.end()); printDeque(d3); deque ); printDeque(d); deque<int>d2; d2.push_back(1); d2.push_back(2); d2.push_back(3); d.insert(d.begin (), d2.begin(), d2.end()); printDeque(d); } //删除 void test03() { deque<int> d; d.push_back(10);
2.tie 分而取之,获取tuple中的单个元素 auto tup = std::make_tuple('l',6,2.33); char a; int b; double c; std:tie(a,b 函数就只能接受左 4.tuple_cat 用于连接tuple std::tuple<float, string> tup1(3.14, "pi"); std::tuple<int, char> tup2( 10, 'a'); auto tup3 = tuple_cat(tup1, tup2); tuple中对元素的操作 1.get < i > 获取第i个元素的值 std::tuple<float, string > tup(666, "emmmm"); cout << get<0>(tup); 输入第一个元素666. 2.tuple_element 获取tuple中特定元素数据结构 std::tuple_element 中的元素个数 std::tuple<float, string> tup(666, "emmmm"); cout << tuple_size<decltype(tup1)>::value; 输出结果为2
概念:stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口
2. 2.创建vecotr对象: (1) vector<int> v1; (2) vector<int> v2(10); 3.基本操作: v.capacity(); //容器容量 v.size 2. 创建deque对象 (1) deque<int> d1; (2) deque<int> d2(10); 3. 如果1、2两步的返回值都是false,则认为a、b是相等的,则b不会被插入set容器中; 如果1返回true而2返回false,那么认为b要排在a的后面,反之则b要排在a的前面; 如果1、2两步的返回值都是 2. 创建对象: map<T1,T2> m; map<T1,T2, op> m; //op为排序规则,默认规则是less<T> 3.
2. 队列作为容器适配器实现,容器适配器即将特定容器类封装作为其底层容器类,queue提供 一组特定的成员函数来访问其元素。元素从队尾入队列,从队头出队列。 3. 2.queue的使用 函数声明 接口说明 queue() 构造空的队列 empty() 检测队列是否为空,是返回true,否则返回false size() 返回队列中有效元素的个数 front()
v1; //无参构造 for (int i = 0; i < 10; i++) { v1.push_back(i); } printVector(v1); vector<int> v2( v1.begin(), v1.end()); printVector(v2); vector<int> v3(10, 100); printVector(v3); vector<int> v2 = v1; printVector(v2); vector<int>v3; v3.assign(v1.begin(), v1.end()); printVector(v3); vector for (int i = 10; i > 0; i--) { v2.push_back(i); } printVector(v2); //互换容器 cout << "互换后" << endl; v1.swap(v2); printVector(v1); printVector(v2); } void test02() { vector<int> v; for (int
str); //把c_string转换成了string cout << "str2 = " << s2 << endl; string s3(s2); //调用拷贝构造函数 cout << " str2 = str1; cout << "str2 = " << str2 << endl; string str3; str3 = 'a'; cout << "str3 = " << love "); str3.append("game abcde", 4); //str3.append(str2); str3.append(str2, 4, 3); // 从下标4位置开始 = "aello"; int ret = s1.compare(s2); if (ret == 0) { cout << "s1 等于 s2" << endl; } else if (ret > 0) { cout << "s1 大于 s2" << endl; } else { cout << "s1 小于 s2" << endl; } } int main() {
的下一单词排列为abdc) 所以如果是生成一个数组的全排列,先要对数组按升序排序,然后使用do-while语句循环调用next_permutation函数 1 #include<iostream> 2 1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 using namespace std; 5 int main()
resize 改变vector的size reserve 改变vector的capacity capacity的代码在vs和g++下分别运行会发现,vs下capacity是按1.5倍增长的,g++是按2 这个问题经常会考察,不要固化的认为,vector增容都是2倍,具体增长多少是 根据具体的需求定义的。vs是PJ版本STL,g++是SGI版本STL。 = v.end()) { cout<< *it << " " ; ++it; } cout<<endl; return 0; } 2. 扩容之后,vector的容量为: 100 0 2 3 4 5 409 1 2 3 4 5 // 2. erase删除任意位置代码后,linux下迭代器并没有失效 // 因为空间还是原来的空间,后序元素往前搬移了 }; // vector<int> v{1,2,3,4,5,6}; auto it = v.begin(); while(it !
C++STL 2——序列容器 一、概述 序列容器以线性序列的方式存储元素。它没有对元素进行排序,元素的顺序和存储它们的顺序相同。 2. list 和 forward_list 的函数成员。 这通常是一个很大的值,一般是 2^32 -1,所以我们很少会用到这个函数。 详情参考C语言中文网
empty() 检测stack是否为空 size() 返回stack中元素的个数 top() 返回栈顶元素的引用 push() 将元素val压入stack中 pop() 将stack中尾部的元素弹出 2.
#include<iostream> using namespace std; int func1() { return 10; } // 不能做参数 void func2(auto a) {} 比特就业课 3, 4, 5 }; // C++98的遍历 for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i) { array[i] *= 2; } "hello bit"); // 用C格式字符串构造string类对象s2 string s3(s2); // 拷贝构造s3 } 2.string类对象的容量操作 函数名称 功能说明 size 返回字符串有效字符长度 2. clear()只是将string中有效字符清空,不改变底层空间大小。 2. 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留 好。
InputIterator first, InputIterator last 用 [first, last) 区间中的元素构造 list 2. begin 与 end 为正向迭代器,对迭代器执行 ++ 操作,迭代器向后移动 2. void TestListIterator1() { int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; list<int> l(array, array+sizeof 因此it无效,在下一次使用it时,必须先给 其赋值 l.erase(it); ++it; } } // 改正 void TestListIterator() { int array[] = { 1, 2, _head; size_t _size; }; void test_list1() { list<int> lt; lt.push_back(1); lt.push_back(2)
set<int> setInt; setInt.insert(2); setInt.insert(1); setInt.insert(8); setInt.insert( vecInt.push_back(9); int count = count_if(vecInt.begin(), vecInt.end(), evenNumber);// 偶数个数是 2; iterator it1 = mypair.first; cout << "it1:" << *it1 << endl; //3 //4 set<int>::iterator it2 = mypair.second; cout << "it2:" << *it2 << endl; //4 //4 查找算法 merge() 以下是排序和通用算法:提供元素排序策略 merge 例如:vecIntA,vecIntB,vecIntC是用vector<int>声明的容器,vecIntA已包含1,3,5,7,9元素,vecIntB已包含2,4,6,8元素 vecIntC.resize
2、pair的介绍 pair是C++标准库中的一个模板类,用于存储两个不同类型的值,通常用于键值对的表示。其定义在头文件中,基本形式为std::pair<T1, T2>。 } else { //没找到就插入 m2.insert({ e,1 }); } } for (auto& e : m2) { cout << e.first < erase(m2.begin(), m2.end()); } 运行结果: 3、lower_bound和upper_bound的使用 lower_bound:返回指向第一个不小于给定键的元素的迭代器 "b"},{3,"c"},{5,"e"},{4,"d"},{6,"f"}}; auto pos1 = m.lower_bound(2); auto pos2 = m.upper_bound(4); = m.lower_bound(2); //可以删除这段区间的值 注意不能再用上面的pos1了因为pos已经改变了 m.erase(pos3, pos2); for (auto& e : m)
};//中序遍历 加上排序去重 //隐式类型转化 set<int> s2({1, 2, 3, 2, 5, 6, 7,9,10,8,12}); //拷贝构造 set<int> s3(s); / };//中序遍历 加上排序去重 //隐式类型转化 set<int> s2({1, 2, 3, 2, 5, 6, 7,9,10,8,12}); //拷贝构造 set<int> s3(s); 代码示例: void test_set4() { set<int> s2({1, 2, 3, 2, 5, 6, 7,9,10,8,12}); //========================== //lower_bound和up_bound的使用 //例如我们需要2——9这个区间 //========================== //s2.erase(2); auto it1 = s2.lower_bound(2);//如果找不到2那就找比2大一点的数 闭区间 //s2.erase(9); auto it2 = s2.upper_bound(9);//如果找不到9那就找比
#include<iostream> #include<stack> #include<queue> int main() { stack<int> a; a.push(1); a.push(2) Compare com;//仿函数 while (child > 0) { if(com(c[(child - 1) / 2] child = (child - 1) / 2; } else { } void adjust_down(size_t parent)//向下调整 { size_t left = parent * 2 / 2; i >= 0; i--)//建堆 { adjust_down(i); } }