stl

memmem() STL way?

送分小仙女□ 提交于 2020-02-03 09:28:24
问题 Is there an STL algorithm which can be used to search a sequence of bytes inside a buffer like memmem() does? 回答1: I don't know if this is good code, but the following works, using std::search: #include <cstdio> #include <string.h> #include <algorithm> int main(int argc, char **argv) { char *a = argv[0]; char *a_end = a + strlen(a); char *match = "out"; char *match_end = match+strlen(match); // If match contained nulls, you would have to know its length. char *res = std::search(a, a_end,

memmem() STL way?

有些话、适合烂在心里 提交于 2020-02-03 09:27:11
问题 Is there an STL algorithm which can be used to search a sequence of bytes inside a buffer like memmem() does? 回答1: I don't know if this is good code, but the following works, using std::search: #include <cstdio> #include <string.h> #include <algorithm> int main(int argc, char **argv) { char *a = argv[0]; char *a_end = a + strlen(a); char *match = "out"; char *match_end = match+strlen(match); // If match contained nulls, you would have to know its length. char *res = std::search(a, a_end,

hash_map: why it defines less, rather than equal_to

百般思念 提交于 2020-02-03 07:58:21
问题 C++, using Visual Studio 2010. A question about why a user-defined trait of hash_map actually requires total ordering. I have a simple structure, say FOO , which only has a number of integers. I'd like to use hash_map , which is a hash table whose keys are unordered , to store the structure of FOO . I just need a fast searching of its associated value, so this is a right choice: hash_map<FOO, int32_t> . However, I need to implement my own hash function and some compare functions for FOO .

STL源码剖析之deque

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-03 05:38:05
前面我们讲过,vector的底层是连续线性存储的,其是动态的分配内存空间的,而vector只支持尾端的插入和删除操作。有没有一种数据结构首尾都支持插入和删除的的操作呢?当然有啊~ 就是deque(双端队列)。deque是一种双向开口的连续线性空间,所谓双向开口,意思是可以在头尾两端进行插入和删除的操作。 deque和vector的最大差异在于deque允许于常数时间内对首尾两端进行插入和删除,还有deque没有所谓的容量,因为他是动态地以分段连续空间组合而成的,随时可以增加一段新的空间。deque采用一块map(中央控制器),中央控制器是一小段连续的线性空间,其中的每个元素都是指针,每个指针指向另一块内存空间,我们称为缓存区,所以说map是一个二级指针。 来源: CSDN 作者: Cry . 链接: https://blog.csdn.net/qq_43145594/article/details/104141523

C++ STL初识

假装没事ソ 提交于 2020-02-03 02:15:39
文章目录 STL的诞生 STL的基本概念 STL六大组件 STL中容器、算法、迭代器 容器: 算法 迭代器 STL的诞生 长久以来,软件界一直希望建立一种可重复利用的东西 C++的 面向对象 和 泛型编程 思想,目的就是 复用性的提升 大多数情况下,数据结构和算法都未能有一套标准,导致被迫从事大量的重复工作 为建立数据结构和算法的一套标准,诞生了 STL STL的基本概念 STL(Standard Template Library, 标准模板库 ) STL广义上分为: 容器(container)算法(algorithm)迭代器(iterator) 容器 和 算法 之间通过 迭代器 进行无缝连接 STL几乎所有的代码都采用了模板类或者模板函数 STL六大组件 容器,算法,迭代器,仿函数,适配器,空间配置器 容器:各种数据结构,如vector,list,deque,set,map等,用来存放数据 算法:各种常用的算法,如sort、find、copy、for_each等 迭代器:扮演了容器与算法之间的胶合剂 仿函数:行为类似函数,可作为算法的某种策略 适配器:一种用来修饰容器或者仿函数或迭代器接口的东西 空间配置器:负责空间的配置与管理 STL中容器、算法、迭代器 容器: STL容器就是将 运用最广泛的一些数据结构 实现出来。 如:数组、链表、树、栈、队列、集合、映射表等 这些容器分为

STL之去重函数unique()

拟墨画扇 提交于 2020-02-02 21:13:56
STL之去重函数unique() 很多同学都会遇到去重这样的问题 在没有认识到STL之前都是利用数组下标来解决 例如: # include <bits/stdc++.h> using namespace std ; int num [ 10000 ] ; //以10000以内的数据举例子 int cn [ 10000 ] ; int main ( ) { int n ; cin >> n ; int a , b , c ; for ( int i = 0 ; i < n ; i ++ ) { cin >> num [ i ] ; cn [ num [ i ] ] ++ ; //将数据以数据下标储存 } for ( int i = 1 ; i <= 10000 ; i ++ ) { if ( cn [ i ] ) cout << i << " " ; //在数据比较小的时候还是很好使用的 } return 0 ; } 这种利用数组下标的去重方法是比较不错的,但是随着数据的多样化和数据的复杂化,是很容易超时的。下面我们将引入STL中的重函数unique()函数。 我们用去重函数unique()写出与上一代码相同作用的代码 如下: # include <bits/stdc++.h> using namespace std ; int num [ 10000 ] ; int main (

实验7:Problem E: STL——括号匹配

十年热恋 提交于 2020-02-02 13:40:04
Description 给出一堆括号,看其是否匹配,例如 ()、()()、(()) 这样的括号就匹配, )(、)()) 而这样的括号就不匹配 Input 每一行代表一组测试样例,每组测试样例只包含'('和')',样例长度不超过100个字符 Output 如果所有的括号都匹配,那么输出YES,否则输出NO Sample Input () )( Sample Output YES NO HINT 使用STL的stack容易实现。 Append Code #include<iostream> #include<stack> #include<string> #include<cstdio> using namespace std; int main() { string a; int i,j,n=0; while(getline(cin,a)) { stack<int> s; for(i=0;i<a.size();i++) { if(a[i]=='(') s.push(1); else if(!s.empty()) s.pop(); else break; } cout<<(i==a.size()&&s.empty()?"YES":"NO")<<endl; } return 0; } 来源: https://www.cnblogs.com/auto1945837845/p/5408903

How can I use std::chrono::duration as a template parameter?

余生长醉 提交于 2020-02-02 02:21:32
问题 I have a template class, something like: template < typename T, size_t Seconds > class MyClass {} Now, I would like to change Seconds to be a duration, so the class can be parametrized with std::chrono::duration . For example, I'd like to be able to do this: MyClass < std::string, std::chrono::seconds(30) > object; Also, in the template, I'd like to specify a default value, something like std::chrono::seconds(30) . 回答1: You can design your template in a clever way: template < typename T,

How can I use std::chrono::duration as a template parameter?

自作多情 提交于 2020-02-02 02:21:29
问题 I have a template class, something like: template < typename T, size_t Seconds > class MyClass {} Now, I would like to change Seconds to be a duration, so the class can be parametrized with std::chrono::duration . For example, I'd like to be able to do this: MyClass < std::string, std::chrono::seconds(30) > object; Also, in the template, I'd like to specify a default value, something like std::chrono::seconds(30) . 回答1: You can design your template in a clever way: template < typename T,

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

烂漫一生 提交于 2020-02-02 00:04:31
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)很耗时间。   因此