stl

C++ STL priority_queue

大憨熊 提交于 2019-12-23 10:26:23
C++ STL priority_queue 标签: c++ vector 2012-10-02 18:29 17801人阅读 评论 (3) 收藏 举报 分类: ACM(9) priority_queue 对于基本类型的使用方法相对简单。他的模板声明带有三个参数,priority_queue<Type, Container, Functional> Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。 Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list. STL里面容器默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个参数 缺省的话,优先队列就是大顶堆,队头元素最大。 看例子 [cpp] view plain copy #include <iostream> #include <queue> using namespace std; int main(){ priority_queue< int ,vector< int >,less< int > >q; //使用priority_queue<int> q1;一样 for ( int i=0;i<10;i++) q1.push(i); while (!q1.empty()){ cout<<q1.top

STLifying C++ classes

久未见 提交于 2019-12-23 10:19:36
问题 I'm trying to write a class which contains several std::vectors as data members, and provides a subset of vector's interface to access them: class Mesh { public: private: std::vector<Vector3> positions; std::vector<Vector3> normals; // Several other members along the same lines }; The main thing you can do with a mesh is add positions, normals and other stuff to it. In order to allow an STL-like way of accessing a Mesh (add from arrays, other containers, etc.), I'm toying with the idea of

C++ stringstream inline

不羁的心 提交于 2019-12-23 10:07:41
问题 I would like to use std::stringstream to create formatted strings, but use the class inline so I don't have stringstream local variables flying around. What I mean is this: #include <iostream> #include <ostream> #include <string> #include <sstream> int main(int argc, char* argv[]) { std::string test = ((std::ostringstream&) (std::ostringstream("") << "This is a test: " << 50.1 << "abc") ).str(); std::cout << test << std::endl; return 0; } This compiles fine in GCC, however the output is the

C++: Return type of std::tie with std::ignore

我们两清 提交于 2019-12-23 09:55:51
问题 I am wondering if the C++11 standard gives any requirement about the type of the std::tuple returned by std::tie when some arguments are std::ignore . More specifically, can I assume that: decltype(std::tie(42, std::ignore)) is not the same as decltype(std::tie(std::ignore, 42)) decltype(std::tie(42, std::ignore)) is not the same as decltype(std::tie(42)) decltype(std::tie(std::ignore, 42)) is not the same as decltype(std::tie(42)) decltype(std::tie(std::ignore, std::ignore)) is not the same

Converting STL container<T *> to container<T const *>

余生长醉 提交于 2019-12-23 09:49:46
问题 I'm looking for a way to formulate a class having: an interface using STL containers of pointers with maximum 'constness' but which internally mutates the pointed-to objects with no extra run-time overhead compared to a non-const analog Ideally, the solution would compile to no extra code compared to the non-const version since const/non-const-ness is just an aid to programmers here. Here's what I've tried so far: #include <list> #include <algorithm> using namespace std; typedef int T; class

Can I use an stl map if I plan to use arbitrary class objects as the key?

泄露秘密 提交于 2019-12-23 09:49:33
问题 I'm new to STL. The thing stumping me about using a map to store arbitrary objects: std::map<MyClassObj, MyDataObject> MyMap; is how I find objects. How would MyMap.find (MyClassObjInstance) work for instance? Do I need to implement my own iterator and provide some standard functions which would include some equivalence function? Any examples would be appreciated. Is there another method to store an associated list of arbitrary objects using standard libraries? I'm already using stl to

Do values with same hash go in same bucket of std::unordered_map?

。_饼干妹妹 提交于 2019-12-23 09:30:37
问题 If two keys for a std::unordered_map have the same hash value, is it guaranteed by the standard that they will go into the same bucket? We are assuming that the keys are not equal according to the template equality predicate, they only have the same hash value. Bonus question: If same hash does not imply same bucket, then what is the purpose of being able to traverse buckets individually? 回答1: Objects with the same hash are put into the same bucket by unordered associative containers.

C++: Vector of vectors

折月煮酒 提交于 2019-12-23 09:29:45
问题 I need to create a vector of vectors (Vector of 3 vectors to be precise). Each constituent vector is of different datatype (String, double, user-defined datatype). Is this possible in C++? If not, is there any other elegant way of realizing my requirement? 回答1: If you know there's three of them, and you know their types, why not just write a class? class Data { std::vector<std::string> _strings; std::vector<double> _doubles; std::vector<UserDefined> _userDefined; public: // ... }; This would

STL iterator before std::map::begin()

☆樱花仙子☆ 提交于 2019-12-23 09:21:48
问题 In C++11's std::map , is there some valid iterator x such that ++ x is guaranteed to equal map::begin() ? I would like to detect if a function I just called (mine) has walked an iterator off the front of a function. The function will move the iterator exactly one position backward. Does the answer hold for the rest of the library? 回答1: No, iterators before the beginning in std containers are all UB (except for reverse iterators, which will probably not solve your problem). You probably need

STL顺序容器vector、deque、list

梦想的初衷 提交于 2019-12-23 09:08:39
1.可变长动态数组 vector vector 支持随机访问迭代器,所有 STL 算法都能对 vector 进行操作,使用 vector,需要包含头文件vector vector 容器中,根据下标随机访问某个元素的时间是常数,假设a是某个vector类型的对象,可以用a[n]或者a.at(n)访问随机位置。 在尾部添加一个元素的时间大多数情况下也是常数,在遇到空间不足需要重新分配内存空间时,把原有内容复制过去后再添加新的元素。碰到这种情况,添加新元素所花的时间就不是常数,而是和数组中的元素个数成正比。 遇到在中间插入或删除元素时,因为要移动多个元素,平均花费的时间和容器中的元素个数成正比 vector常用的成员函数 成员函数 作 用 vector() 无参构造函数,将容器初始化为空 vector(int n) 将容器初始化为有 n 个元素 vector(int n, const T & val) 假定元素的类型是 T,此构造函数将容器初始化为有 n 个元素,每 个元素的值都是 val vector(iterator first, iterator last) first 和 last 可以是其他容器的迭代器。一般来说,本构造函数初始化的结果就是将 vector 容器的内容变成与其他容器上的区间 [first, last) —致 void clear() 删除所有元素 bool