stl

Should I replace all calls to push_back with emplace_back?

久未见 提交于 2020-01-02 04:49:07
问题 In my C++ application I heavily use STL containers like vector . There are a lot of calls to push_back , and I have been concerned about unnecessary constructions and copy operations. My application is pretty low-level and I am very concerned about CPU and memory usage. Should I replace all calls to push_back with calls to emplace_back ? I am using Visual Studio 2013. 回答1: It is an almost always rule. You cannot rely on side effect of copy constructors so it should means that skipping it

Segmentation fault in std::map::insert(…)

我们两清 提交于 2020-01-02 03:24:14
问题 i've used search but i didn't find answer satisfying me... so.. this is chunk of code: //VoteContainer.h typedef uint32_t order_id_t; typedef int driver_id_t; class Vote { public: enum DriverVoteResponse {YES, NO, TIMEOUT}; struct DriverResponse { driver_id_t driver_id; time_t time; DriverVoteResponse response; }; Vote() : m_order_id(0), m_time_until(0) {}; Vote(order_id_t inOrderId, std::vector<driver_id_t> inPermittedDrivers, int inSeconds); Vote(const Vote & other) : m_order_id(other.m

Is it possible to remove queue element by value?

爷,独闯天下 提交于 2020-01-02 03:17:26
问题 I want to remove element from queue with specific value. How to do such thing? (I am trying to create a concurrent mixture of map and queue and currently I try to implement on this answer) So I currently have such code: #ifndef CONCURRENT_QUEUED_MAP_H #define CONCURRENT_QUEUED_MAP_H #include <map> #include <deque> #include <boost/thread.hpp> #include <boost/thread/locks.hpp> template <class map_t_1, class map_t_2> class concurrent_queued_map { private: std::map<map_t_1, map_t_2> _ds; std:

Is it possible to remove queue element by value?

时间秒杀一切 提交于 2020-01-02 03:17:06
问题 I want to remove element from queue with specific value. How to do such thing? (I am trying to create a concurrent mixture of map and queue and currently I try to implement on this answer) So I currently have such code: #ifndef CONCURRENT_QUEUED_MAP_H #define CONCURRENT_QUEUED_MAP_H #include <map> #include <deque> #include <boost/thread.hpp> #include <boost/thread/locks.hpp> template <class map_t_1, class map_t_2> class concurrent_queued_map { private: std::map<map_t_1, map_t_2> _ds; std:

Optimising C++ 2-D arrays

為{幸葍}努か 提交于 2020-01-02 02:35:11
问题 I need a way to represent a 2-D array (a dense matrix) of doubles in C++, with absolute minimum accessing overhead. I've done some timing on various linux/unix machines and gcc versions. An STL vector of vectors, declared as: vector<vector<double> > matrix(n,vector<double>(n)); and accessed through matrix[i][j] is between 5% and 100% slower to access than an array declared as: double *matrix = new double[n*n]; accessed through an inlined index function matrix[index(i,j)] , where index(i,j)

Can I detach a std::vector<char> from the data it contains?

耗尽温柔 提交于 2020-01-02 02:18:49
问题 I'm working with a function which yields some data as a std::vector<char> and another function (think of legacy APIs) which processes data and takes a const char *, size_t len . Is there any way to detach the data from the vector so that the vector can go out of scope before calling the processing function without copying the data contained in the vector (that's what I mean to imply with detaching ). Some code sketch to illustrate the scenario: // Generates data std::vector<char>

Why use rbegin() instead of end() - 1?

余生长醉 提交于 2020-01-02 01:54:07
问题 I'm wondering what the benefits of using rbegin() rather than end() - 1 are for STL containers. For example, why would you use something like: vector<int> v; v.push_back(999); vector<int>::reverse_iterator r = v.rbegin(); vector<int>::iterator i = r.base(); Rather than: vector<int> v; v.push_back(999); auto r = v.end() - 1; 回答1: rbegin() return an iterator with a reverse operator++ ; that is, with a reverse_iterator you can iterate through a container going backward. Example: #include <vector

How can i pass C++ Objects to DLLs with different _ITERATOR_DEBUG_LEVEL

孤街浪徒 提交于 2020-01-01 19:22:11
问题 My executable makes calls to a number of DLLs, that i wrote myself. According to 3rd party C++ libs used by these DLLs i can not freely choose compiler settings for all DLLs. Therefore in some DLLs _ITERATOR_DEBUG_LEVEL is set to 2 (default in the debug version), but in my executable _ITERATOR_DEBUG_LEVEL is set to 0, according to serious performance problems. When i now pass a std::string to the DLL, the application crashes, as soon as the DLL tries to copy it to a local std::string obj, as

Minimal set of nested typedefs for a custom STL sequence?

与世无争的帅哥 提交于 2020-01-01 15:35:13
问题 What is the minimal set of nested typedefs that should be defined in a custom STL class meeting the Sequence concept? The custom sequence should be compatible with: std::back_insert_iterator BOOST_FOREACH Boost range concept. 回答1: The C++ Standard says all containers must have the following typedefs (C++03 23.1/Table 65): value_type reference const_reference iterator const_iterator difference_type size_type Reversible containers must have the following typedefs (C++03 23.1/Table 66): reverse

栈 队列 优先队列 STL

余生颓废 提交于 2020-01-01 14:36:00
栈 #include<stack> //头文件 stack<char> st; //定义 st.push(str1[0]); //入栈 cur=st.top (); //取栈顶值 st.pop(); //出栈 st.empty ()//空为true 队列 #include<queue> queue<char>que; que.push(a); a=que.front(); que.pop(); que.empty ()优先队列 bool operator < (const coor &a, const coor &b) { return a.time > b.time; //从小到大排序,修改为最小堆,取最小值。 } priority_queue<coor>que; que.push(a); a=que.top();// 默认为最大堆,取最大值。 que.pop(); que.empty () 以下对vector、list、deque容器通用,这里以list容器为例,注意访问元素时vector、deque可用下标直接访问,但对list不行 list<line> dlist; vector<int> c; line in; list<line>::iterator iter;//定义一个指向元素的迭代器 插入: dlist.insert(iter,t)/