stl

Why does std::queue use std::dequeue as underlying default container?

我是研究僧i 提交于 2021-01-21 07:31:07
问题 As read on cplusplus.com, std::queue is implemented as follows: queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front". The underlying container may be one of the standard container class template or some other specifically designed

Setting all values in a std::map

心已入冬 提交于 2021-01-20 20:18:08
问题 How to set all the values in a std::map to the same value, without using a loop iterating over each value? 回答1: Using a loop is by far the simplest method. In fact, it’s a one-liner: [C++17] for (auto& [_, v] : mymap) v = value; Unfortunately C++ algorithm support for associative containers isn’t great pre-C++20. As a consequence, we can’t directly use std::fill . To use them anyway (pre-C++20), we need to write adapters — in the case of std::fill , an iterator adapter. Here’s a minimally

Setting all values in a std::map

十年热恋 提交于 2021-01-20 20:16:27
问题 How to set all the values in a std::map to the same value, without using a loop iterating over each value? 回答1: Using a loop is by far the simplest method. In fact, it’s a one-liner: [C++17] for (auto& [_, v] : mymap) v = value; Unfortunately C++ algorithm support for associative containers isn’t great pre-C++20. As a consequence, we can’t directly use std::fill . To use them anyway (pre-C++20), we need to write adapters — in the case of std::fill , an iterator adapter. Here’s a minimally

Setting all values in a std::map

╄→尐↘猪︶ㄣ 提交于 2021-01-20 20:14:11
问题 How to set all the values in a std::map to the same value, without using a loop iterating over each value? 回答1: Using a loop is by far the simplest method. In fact, it’s a one-liner: [C++17] for (auto& [_, v] : mymap) v = value; Unfortunately C++ algorithm support for associative containers isn’t great pre-C++20. As a consequence, we can’t directly use std::fill . To use them anyway (pre-C++20), we need to write adapters — in the case of std::fill , an iterator adapter. Here’s a minimally

Difference between upper_bound and lower_bound in stl

这一生的挚爱 提交于 2021-01-20 18:09:28
问题 I was looking at how the upper_bound and lower_bound algorithms work in stl on these pages: lower_bound, upper_bound, and it's documented the same way on these pages: lower_bound, upper_bound Looking at the code from the links, they seem to do exactly the same thing to me, with only the following lines being different (looking at the code from the first 2 links): lower_bound (line 10): if (*it<val) { // or: if (comp(*it,val)), for version (2) upper_bound (line 10): if (!(val<*it)) // or: if (

How to replicate map, filter and reduce behaviors in C++ using STL?

拟墨画扇 提交于 2021-01-20 14:47:09
问题 I suppose we can use std::transform to replicate the map behavior in C++ like this : std::vector<int> in = { 1 , 2 , 3 ,4 }; std::vector<int> out(in.size()); std::transform(in.being() , in.end() , out.begin() , [](const int & val) { return val+1; }); I guess a better way would be to use the back inserter.' std::vector<int> out2; std::transform(in.begin() , in.end() , std::back_inserter(out2) , [](const int & val){ return val + 1; }); // out will be { 2 , 3 ,4 ,5 } Am I right ? How do I do the

Sort 2 dimensional c array with std::sort

青春壹個敷衍的年華 提交于 2021-01-19 06:25:39
问题 I cannot seem to sort a 2 dimensional c array with std::sort. I can however sort a one dimensional array. This is in a case where I am being handed a c array in a c++ program and am hoping to sort without copying it over to a std::array. Maybe there is some way to turn it into a std::array without copying it? That sounds doubtful to me as any std::array would then call a destructor on memory that it does not own. Sorting One dimensional c style array works just fine: int len = 5; auto one_dim

Should we explicitly write a copy constructor if the class member is a vector?

泄露秘密 提交于 2021-01-17 08:14:25
问题 struct myType { vector<char*> ls; }; Here ls is holding pointers to char . If a user-defined copy constructor for myType is not provided, will myType 's default copy constructor do a deep copy of ls ? 回答1: Here ls is holding pointer to char. If copy constructor is not provided, will default copy constructor do the deep copy? The default copy constructor will copy all members – i.e. call their respective copy constructors. 1 So yes, a std::vector (being nothing special as far as C++ is

Insert a pair key in map

我只是一个虾纸丫 提交于 2021-01-07 02:49:35
问题 The way to insert a pair in a map is that: std::map<char,int> mymap; // first insert function version (single parameter): mymap.insert ( std::pair<char,int>('a',100) ); but now I'm trying to insert this in a map: map<pair<int,int>, int> map1; //(pair is the key and int is a value) I tried this: pair<int,int> p; p.first = 5; p.second = 20; map1.insert(pair<int,int>,double> (p,0)); So, how I can do it? 回答1: There are so many possibilities for this. You may choose any of the below which suits

Errors while using templates templates parameters

梦想的初衷 提交于 2021-01-05 06:42:27
问题 I am studying CPP templates myself and got stuck while trying templates of templates parameters for a class. I am getting errors when I am trying to instantiate the class member. #pragma once #include "stdafx.h" # include <list> template<class type, template<type> class T> class stack { private: int count; int size; T<type> st; public: stack(size_t size):size(100), count(-1){ } void push(type elem); type pop(); }; template<class type, template<type> class T> void stack<type, T>::push(type