stl

STL迭代器

牧云@^-^@ 提交于 2020-01-07 04:22:33
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 以下内容大多来自《C++标准程序库》 迭代器是一个“可遍历STL容器内全部或部分元素”的对象,一个迭代器用来指出容器中的一个特定位置。 基本操作: (1)Operator * :返回当前位置上的元素值 (2)Operator ++:将迭代器前进至下一个元素,大多数迭代器还可以使用operator -- 退回到前一个元素 (3)Operator ==和Operator != 判断两个迭代器是否指向同一个位置 (4)Operator = 为迭代器赋值 这些操作与操作数组元素时的指针接口是一致的,不同在于迭代器具有遍历复杂数据结构的能力,其下层运行机制取决于其所遍历的数据结构,因此,每一种容器型别都必须提供自己的迭代器。事实上每一种容器都将其迭代器以嵌套方式定义于内部,因此各种迭代器的接口相同,型别却不相同。 重要函数 所有容器类别都提供有一些成员函数,使我们得以获得迭代器并一直遍访所有元素。 begin() 返回一个迭代器,指向容器起始点的位置 end() 返回一个迭代器,指向容器结束点。结束点在最后一个元素之后,这样的迭代器又称作逾尾(past-the-end)迭代器(所以遍历的时候都是判断!=end()) #include <iostream> #include <list> using namespace

idiom for iterating a range of angles in a container?

此生再无相见时 提交于 2020-01-07 03:52:08
问题 Suppose I have data of 2D lines in the form struct Point { int x, y; }; struct Line { Point p1, p2; double angle() const { return atan2(p2.y-p1.y, p2.x-p1.x); } }; And I want to store these sorted by angle, which must be in the interval (-PI, PI] . My problem: I want to iterate a range in this container, but allow it to wrap around the ends of the interval. For example "all lines between angles PI*3/4 to -PI*3/4 ". To clarify, if I use standard containers like multimap , I can't simply do the

C++ Command line action abstraction using interface

元气小坏坏 提交于 2020-01-07 03:16:12
问题 I'm building an application whose usage is going to look something like this: application --command --option1=? --option2=2? Basically, there can be any number of options, but only one command per instance of the application. Similar to the way git works. Now, I thought I'd write it in C++ to get some boost and stl experience and have a go with a few of those design patterns I keep reading about. So, I implemented this: class Action { public: void AddParameter(std::string key, boost::any p);

RAII, Berkeley Sockets, and STL Containers

可紊 提交于 2020-01-07 03:12:11
问题 Let me start by stating I am using a GNU C++ compiler that does NOT support move constructors. It only supports the 98 standard. Given that, I want to write a C++ class that wraps Berkeley style sockets in a simple way that follows RAII. Here is a very simple class: class SimpleSocket { int m_sockfd; public: SimpleSocket() { m_sockfd = socket(AF_INET, ...); } SimpleSocket(const SimpleSocket& other) { m_sockfd = other.m_sockfd; } ~SimpleSocket() { closeSocket(); } void closeSocket() { if(m

Sorting a vector of custom objects

家住魔仙堡 提交于 2020-01-07 02:43:45
问题 How does one go about sorting a vector containing custom (i.e. user defined) objects. Probably, standard STL algorithm sort along with a predicate (a function or a function object) which would operate on one of the fields (as a key for sorting) in the custom object should be used. Am I on the right track? 回答1: A simple example using std::sort struct MyStruct { int key; std::string stringValue; MyStruct(int k, const std::string& s) : key(k), stringValue(s) {} }; struct less_than_key { inline

算法拾遗[4]——STL用法

Deadly 提交于 2020-01-06 23:42:58
主要bb一下优先队列和字符串吧. 哦还有 bitset . 优先队列 定义很容易: priority_queue<int> pq; 内部是一个堆. 基本操作 pq.top() 取堆顶元素; (没有 front() 方法!) pq.push(x) 插入; pq.pop() 删除(删除堆顶); pq.empty() 判断是否为空. 自定义优先级 最大堆: priority_queue<int> pq; 最小堆: priority_queue< int, vector<int>, greater<int> > pq; 事实上还有自定义优先级 cmp 的方法(优先级最大的最先出队): 12345678 struct { bool operator() (const int a, const int b) const { return a > b; }};priority_queue<int, vector<int>, cmp> pq; // 此时也是最小堆 大专栏 算法拾遗[4]——STL用法 ody> 例题 百练 4078: http://bailian.openjudge.cn/practice/4078/ 字符串 定义更容易: string s; 基本操作 s.size() 串长度(下标从0 开始); s.substr(a, n) 构造子串, a为第一个字符的下标, n为子串字符长度

STL——算法

懵懂的女人 提交于 2020-01-06 15:30:33
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 以下内容大多摘自《C++标准程序库》 算法实例 STL提供了一些标准算法,包括搜寻、排序、拷贝、重新排序、修改、数值运算等。算法并不是容器类别的成员函数,而是一种搭配迭代器使用的全局函数。 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> coll; vector<int>::iterator pos; //coll.push_back(2); coll.push_back(5); coll.push_back(4); coll.push_back(1); coll.push_back(6); coll.push_back(3); pos = min_element(coll.begin(),coll.end()); //寻找最小值 cout << "min:" << *pos << endl; pos = max_element(coll.begin(), coll.end());//寻找最大值 cout << "max:" << *pos << endl; sort(coll.begin(),coll.end()); //排序 cout <<

Inheritance in std::map with base class as value

大兔子大兔子 提交于 2020-01-06 04:03:38
问题 Here is a code: class Base { public: long index; }; class Derived : public Base { public: bool value; }; void call(map<char *, Base *> *base) { map<char *, Base *>::iterator it = base->begin(); cout << it->second->index << endl; } void test(void) { map<char *, Derived *> *d = new map<char *, Derived *>; call(d); } Compiler alerts an error: error C2664: 'call' : cannot convert parameter 1 from 'std::map<_Kty,_Ty> *' to 'std::map<_Kty,_Ty> *' 1> with 1> [ 1> _Kty=char *, 1> _Ty=Derived * 1> ] 1

Inheritance in std::map with base class as value

拈花ヽ惹草 提交于 2020-01-06 04:03:10
问题 Here is a code: class Base { public: long index; }; class Derived : public Base { public: bool value; }; void call(map<char *, Base *> *base) { map<char *, Base *>::iterator it = base->begin(); cout << it->second->index << endl; } void test(void) { map<char *, Derived *> *d = new map<char *, Derived *>; call(d); } Compiler alerts an error: error C2664: 'call' : cannot convert parameter 1 from 'std::map<_Kty,_Ty> *' to 'std::map<_Kty,_Ty> *' 1> with 1> [ 1> _Kty=char *, 1> _Ty=Derived * 1> ] 1

multimap accumulate values

家住魔仙堡 提交于 2020-01-06 02:01:08
问题 I have a multimap defined by typedef std::pair<int, int> comp_buf_pair; //pair<comp_t, dij> typedef std::pair<int, comp_buf_pair> node_buf_pair; typedef std::multimap<int, comp_buf_pair> buf_map; //key=PE, value = pair<comp_t, dij> typedef buf_map::iterator It_buf; int summ (int x, int y) {return x+y;} int total_buf_size = 0; std::cout << "\nUpdated buffer values" << std::endl; for(It_buf it = bufsz_map.begin(); it!= bufsz_map.end(); ++it) { comp_buf_pair it1 = it->second; // max buffer size