stl

Viewing data in a circular buffer in real-time

允我心安 提交于 2019-12-21 22:41:59
问题 I have an incoming stream of messages, and want a window that allows the user to scroll through the messages. This is my current thinking: Incoming messages go into single producer single consumer queue A thread reads them out and places them into a circular buffer with a sequential id This way I could have multiple incoming streams safely placed in the circular buffer and it decouples the input Mutex to coordinate circular buffer access between the UI and the thread Two notifications from

Reducing STL code bloat by wrapping containers

我怕爱的太早我们不能终老 提交于 2019-12-21 22:30:11
问题 I have a C++ library (with over 50 source files) which uses a lot of STL routines with primary containers being list and vector. This has caused a huge code bloat and I would like to reduce the code bloat by creating a wrapper over the list and vector. Shown below is my wrapper over std:: and the wrapped instances. template<typename T> class wlist { private: std::list<T> m_list; public: // new iterator set. typedef typename std::list<T>::iterator iterator; typedef typename std::list<T>::const

Approaching STL algorithms, lambda, local classes and other approaches

限于喜欢 提交于 2019-12-21 22:13:39
问题 One of the things that seems to be necessary with use of STL is a way to specify local functions. Many of the functions that I would normally provide cannot be created using STL function object creation tools ( eg bind ), I have to hand roll my function object. Since the C++ standard forbids local types to be used as arguments in template instantiations the best I was able to use was to create a small library, ( just showing relevant parts ) // library header class MyFunctionBase<R,T> {

How to implement generic method for STL containers that haven`t common interface needed for that method using template template parameter

核能气质少年 提交于 2019-12-21 21:48:44
问题 Problem statement (for an educational purpose): -Implement method printContainer which works for STL containers vector , stack , queue and deque . I made a solution, but I don`t like it due to excessive amount of code. What I did to solve the problem: 1. Designed generic function which expects uniform interface from containers for operations: get value of last element and erase that element from the container template <typename T> void printContainer(T container) { cout << " * * * * * * * * *

C++ STL基本容器的使用

僤鯓⒐⒋嵵緔 提交于 2019-12-21 21:05:52
  C++中有两种类型的容器:顺序容器和关联容器。顺序容器主要有vector、list、deque等。其中vector表示一段连续的内存,基于数组实现,list表示非连续的内存,基于链表实现,deque与vector类似,但是对首元素提供插入和删除的双向支持。关联容器主要有map和set.map是key-value形式,set是单值。map和set只能存放唯一的key,multimap和multiset可以存放多个相同的key.   容器类自动申请和释放内存,因此无需new和delete操作。   一、vector   vector基于模板实现,需包含头文件vector.   1.定义和初始化   //1.定义和初始化   vector vec1; //默认初始化,vec1为空   vector vec2(vec1); //使用vec1初始化vec2   vector vec3(vec1.begin(),vec1.end());//使用vec1初始化vec2   vector vec4(10); //10个值为的元素   vector vec5(10,4); //10个值为的元素   //2.常用操作方法   vec1.push_back(100); //添加元素   int size = vec1.size(); //元素个数   bool isEmpty = vec1

Difference between allocator supplied as template parameter and allocator supplied as constructor argument in C++ containers?

拟墨画扇 提交于 2019-12-21 20:42:36
问题 What's the difference between supplying an STL container (for example, std::vector) with an allocator as a template parameter, eg.: std::vector<int, std::allocator<int>> some_ints; and supplying an allocator as a constructor argument, eg: std::allocator<int> temp; std::vector<int> some_ints(temp); and what are the advantages of either, given that they are not the same thing (ie. one supplies a type, the other a type instance) and can be used separately from each other? 回答1: Can be used

c++ stringstream to ostream to string

倖福魔咒の 提交于 2019-12-21 20:33:14
问题 I would like to be able to do: foo(stringstream()<<"number = " << 500); EDIT: single line solution is crucial since this is for logging purposes. These will be all around the code. inside foo will print the string to screen or something of the sort. now since stringstream's operator<< returns ostream&, foo's signature must be: foo(ostream& o); but how can I convert ostream& to string? (or char*). Different approaches to achieving this use case are welcome as well. 回答1: The obvious solution is

c++ stringstream to ostream to string

你说的曾经没有我的故事 提交于 2019-12-21 20:29:38
问题 I would like to be able to do: foo(stringstream()<<"number = " << 500); EDIT: single line solution is crucial since this is for logging purposes. These will be all around the code. inside foo will print the string to screen or something of the sort. now since stringstream's operator<< returns ostream&, foo's signature must be: foo(ostream& o); but how can I convert ostream& to string? (or char*). Different approaches to achieving this use case are welcome as well. 回答1: The obvious solution is

Specializing STL algorithms so they automatically call efficient container member functions when available

◇◆丶佛笑我妖孽 提交于 2019-12-21 20:15:30
问题 The STL has global algorithms that can operate on arbitrary containers as long as they support the basic requirements of that algorithm. For example, some algorithms may require a container to have random access iterators like a vector rather than a list. When a container has a faster way of doing something than the generic algorithm would, it provides a member function with the same name to achieve the same goal - like a list providing its own remove_if() since it can remove elements by just

how do I dynamically cast between vectors of pointers?

丶灬走出姿态 提交于 2019-12-21 20:00:48
问题 I have: class T {}; class S: public T {}; vector<T*> v; vector<S*> w; transform(v.begin(), v.end(), dynamic_cast_iterator<S*>(w.begin())); But, of course, dynamic_cast_iterator doesn't exist. 回答1: Here is one solution (using boost lambda): #include <boost/lambda/lambda.hpp> #include <boost/lambda/casts.hpp> #include <algorithm> #include <iterator> #include <iostream> namespace bll = boost::lambda; struct A { virtual ~A() { } }; struct B : A { void f() { std::cout << "hello, world" << std: