stl

Using an std::string as a key for an std::map

岁酱吖の 提交于 2020-01-28 06:21:06
问题 I would like to have an std::map (int .NET 4.0). We of course know that a map is a tree and requires an operator< that string does not define for us. Error 24 error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer So I put my google-foo to work and found this solution: struct StringComparerForMap { public: bool

STL之remove算法详解

前提是你 提交于 2020-01-28 04:04:44
算法描述 接口 template<class ForwardIterator, class Type> ForwardIterator remove( ForwardIterator _First, ForwardIterator _Last, const Type& _Val ); 功能 将一个指定的值从指定的区间中[first,last)“删除”,这里的“删除”是指:把区间内”指定值”的位置腾出,用copy的方式把后面”非指定值”逐一的往前移动。 比如: 序列(1234)remove 2后,返回的序列是 1344(3被复制到2的位置,4被复制到3的位置)。 原理 第一层算法: template<class _FwdIt, class _Ty> inline _FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) { // remove each matching _Val _First = find(_First, _Last, _Val); if (_First == _Last) { return (_First);// empty sequence, all done } else { // nonempty sequence, worth doing _FwdIt _First1 = _First;

STL internals: deque implementation

你离开我真会死。 提交于 2020-01-27 23:54:55
问题 I am using a std::deque for storing a large collection of items. I know that deques is implemented as a list of vectors. The size of those vectors cannot be set but I wander what is the algorithm for choosing that size. 回答1: deque is implemented as a vector of vectors (a list of vectors would impede the constant time random access). The size of the secondary vectors is implementation dependent, a common algorithm is to use a constant size in bytes. 回答2: My deque implementation, the one from

STL internals: deque implementation

£可爱£侵袭症+ 提交于 2020-01-27 23:51:08
问题 I am using a std::deque for storing a large collection of items. I know that deques is implemented as a list of vectors. The size of those vectors cannot be set but I wander what is the algorithm for choosing that size. 回答1: deque is implemented as a vector of vectors (a list of vectors would impede the constant time random access). The size of the secondary vectors is implementation dependent, a common algorithm is to use a constant size in bytes. 回答2: My deque implementation, the one from

C++ STL vector 不想初始化长度怎么办

…衆ロ難τιáo~ 提交于 2020-01-27 20:20:39
vector方便的地方就在于不用在意长度的限制,但是如果一开始为vector读取输入的时候,我们没有初始化vector大小的话,很容易出现下标越界的情况,然后程序不跑了。。。 有时候未知输入的长度时,vector开小了怕越界,开大了怕浪费 解决方法 使用 vector 的 insert 方法,这样一来vector会自动地增加自己的长度了,防止越界 // 在尾部直接插入元素 x v . insert ( v . end ( ) , x ) ; # include <iostream> # include <vector> using namespace std ; int main ( ) { vector < int > v ; int x ; for ( int i = 0 ; i < 4 ; i ++ ) { cin >> x ; v . insert ( v . end ( ) , x ) ; } for ( int i = 0 ; i < 4 ; i ++ ) { cout << v [ i ] << endl ; } return 0 ; } 运行结果: 来源: CSDN 作者: AkagiSenpai 链接: https://blog.csdn.net/weixin_44176696/article/details/104094252

将stl文件中vertex顶点由顺时针(clockwise)排列改为逆时针(counterclockwise)

ⅰ亾dé卋堺 提交于 2020-01-27 02:14:21
最近需要读取 solidWork 软件生成的复杂几何体(stl格式),用于识别一系列点位于复杂几何体内部还是外部,发现识别函数需要stl文件符合 右手规则 ,即点的排布要逆时针顺序,而且法线方向朝外,但是solidWork生成的文件不符合这个特征,所以写了一个 c++ 的小程序,用来实现该过程,具体内容如下: 1. stl文件格式: facet normal - 0.996195 0.0871562 - 0 outer loop vertex - 7.500000e-002 - 1.836970e-017 2.220404e+000 //p1 vertex - 7.500000e-002 - 1.836970e-017 9.964039e-001 //p2 vertex - 7.386058e-002 1.302361e-002 2.220404e+000 //p3 endloop endfacet 上述代码块即为stl文件的格式,主要由面法向量,以及3个点构成,3个点连线顺序不符合 右手规则 ,我们可以调 p2,p3 的顺序,并重新求解facet normal即可,求解方法可以参考该 链接 2. 具体实现 1.对原有stl文件进行分割: # include <vector> # include <math.h> # include <iostream> # include

STL之pair

蹲街弑〆低调 提交于 2020-01-27 00:11:55
pair的简介 pair是将2个数据合成一组数据,如STL中的map就是将key和value放在一起来保存。另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。 pair的实现是一个结构体,主要的两个成员变量是first,second 因为pair使用的是struct不是class,所以无需申明头文件便可以直接使用pair pair的构造 创建一个空的pair对象,它的两个元素分别是T1和T2类型,采用值初始化 pair<T1, T2> p; 创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,second成员初始化为v2 pair<T1, T2> p1(v1, v2); 以v1和v2的值创建一个新的pair对象,其元素类型分别是v1和v2的类型 make_pair(v1, v2); pair对象的操作 访问两个元素操作可以通过first和sencond访问 pair < int , int > p ; p . first = 1 ; p . second = 2 ; cout << p . first << ' ' << p . second << endl ; //输出结果:1 2 pair的比较是先比较first,当first相等时再比较second 通过tie获取pair元素值 在某些清况函数会以pair对象作为返回值时

vector::at vs. vector::operator[]

蓝咒 提交于 2020-01-26 21:56:36
问题 I know that at() is slower than [] because of its boundary checking, which is also discussed in similar questions like C++ Vector at/[] operator speed or ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster!. I just don't understand what the at() method is good for. If I have a simple vector like this one: std::vector<int> v(10); and I decide to access its elements by using at() instead of [] in situation when I have a index i and I'm not sure if its in

vector::at vs. vector::operator[]

妖精的绣舞 提交于 2020-01-26 21:54:20
问题 I know that at() is slower than [] because of its boundary checking, which is also discussed in similar questions like C++ Vector at/[] operator speed or ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster!. I just don't understand what the at() method is good for. If I have a simple vector like this one: std::vector<int> v(10); and I decide to access its elements by using at() instead of [] in situation when I have a index i and I'm not sure if its in

STL常用序列容器学习笔记

孤人 提交于 2020-01-26 21:35:13
这里简要的记述一下STL常用容器的实现原理,要点等内容。 vector vector 是比较常用的stl容器,用法与数组是非类似,其内部实现是连续空间分配,与数组的不同之处在于可弹性增加空间,而 array 是静态空间,分配后不能动态扩展。 vecotr 的实现较为简单,主要的关键点在于当空间不足时,会新分配当前空间2倍的空间,将旧空间数据拷贝到新空间,然后删除旧空间。 struct _Vector_impl: public _Tp_alloc_type { pointer _M_start; // 元素头 pointer _M_finish; // 元素尾 pointer _M_end_of_storage; // 可用空间尾, // 省略部分代码... }; 这个是向尾部添加元素的代码实现,可以看到如果当前还有剩余空间的话,直接在尾部添加,如果没有剩余空间,则会动态扩展。 void push_back(const value_type& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) { _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, __x); ++this->_M_impl._M_finish; }