stl

order of elements in std::unordered_multimap

ぃ、小莉子 提交于 2019-12-23 17:30:22
问题 If I have the following piece of code std::unordered_multimap<std::string, std::vector<double>> myMap; std::vector<double> v1, v2, v3; // init v1, v2, v3.... myMap.insert(std::make_pair<std::string, std::vector<double>("vec", v1)); myMap.insert(std::make_pair<std::string, std::vector<double>("vec", v2)); myMap.insert(std::make_pair<std::string, std::vector<double>("vec", v3)); If I access the values with an iterator they will always be in this order: v1, v2, v3 So basically if I insert

Does pImpl fundamentally solve C++ DLL issue?

帅比萌擦擦* 提交于 2019-12-23 17:01:23
问题 I'm trying to export a C++ class out of a DLL with stl members. Here's my main class. class MATHFUNCSDLL_API MyMathFuncsImpl { public: std::vector<int> vi; std::string getString(); void setString(std::string s); private: std::string s; }; Using the methods works, but gives warnings on VS 2012 about std::string and std::vector not having a dll-interface. Now when I do this - class MATHFUNCSDLL_API MyMathFuncs { public: MyMathFuncs() { pImpl = new MyMathFuncsImpl(); } std::string getString() {

User-defined overloaded operator * with std::chrono::duration

£可爱£侵袭症+ 提交于 2019-12-23 15:14:21
问题 I've created a Frequency class template intended to work in conjunction with std::chrono::duration. A Frequency object stores a number of cycles per unit duration (both using template parameters for their types). The idea is that multiplying a Frequency by a duration produces an object of type Rep. Here's the class definition. I've omitted all but the relevant members. #include <ratio> #include <chrono> using namespace std::chrono; template <typename Rep, typename Period = std::ratio<1>>

C++ STL Heap算法

别说谁变了你拦得住时间么 提交于 2019-12-23 13:47:06
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() {   vector<int> vec1;   vector<int>::iterator vec_iter1;   for (int k=0;k<10;k++)   {     vec1.push_back(rand());   }   for (vec_iter1 = vec1.begin();vec_iter1 != vec1.end();++vec_iter1)   {     cout << *vec_iter1 << " ";   }   cout << endl;   cout << "-------------------------------------------------------" << endl;   make_heap(vec1.begin(), vec1.end());   for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)   {     cout << *vec_iter1 << " ";   }   cout << endl;   cout << "--------------

Is there a C++ container with reasonable random access that never calls the element type's copy constructor?

泪湿孤枕 提交于 2019-12-23 13:24:09
问题 I need a container that implements the following API (and need not implement anything else): class C<T> { C(); T& operator[](int); // must have reasonably sane time constant // expand the container by default constructing elements in place. void resize(int); // only way anything is added. void clear(); C<T>::iterator begin(); C<T>::iterator end(); } and can be used on: class I { public: I(); private: // copy and assignment explicate disallowed I(I&); I& operator=(I&); } Dose such a beast

c++ STL map::operator[] done on an entry being deleted

£可爱£侵袭症+ 提交于 2019-12-23 13:07:24
问题 std::map<int,int> bar; int foo(int key) { bar.erase(key); return 1; } int main() { bar[0] = foo(0); return 0; } This code compiled with GCC 4.8 segs fault when checking memory usage with electric fence. LD_PRELOAD=libefence.so.0.0 ./a.out The problem comes from the fact that the compiler generates a code that starts to allocate a new entry in the map, then executes foo() to get the value to put into bar[0] . While running foo() , the entry gets destroyed and the code finally ends by writing

Functor that calls a function after dereferencing?

你说的曾经没有我的故事 提交于 2019-12-23 12:55:58
问题 Is there a small functor in the C++ standard or in Boost that wraps around another functor, but dereferences a pointer before it calls that functor? I'm thinking of something like this: template<class F> struct DerefCmp { template<class T> bool operator()(T* v) const { return F(*v); } }; I'd use it in a container of pointers, for example, where I want to compare by value: std::set<int*, DerefCmp< std::equal<int> > > s; 回答1: I am not aware of any function object in the C++ Standard Library or

Functor that calls a function after dereferencing?

夙愿已清 提交于 2019-12-23 12:55:47
问题 Is there a small functor in the C++ standard or in Boost that wraps around another functor, but dereferences a pointer before it calls that functor? I'm thinking of something like this: template<class F> struct DerefCmp { template<class T> bool operator()(T* v) const { return F(*v); } }; I'd use it in a container of pointers, for example, where I want to compare by value: std::set<int*, DerefCmp< std::equal<int> > > s; 回答1: I am not aware of any function object in the C++ Standard Library or

Add std::pair with + operator

拈花ヽ惹草 提交于 2019-12-23 12:45:14
问题 Is there a simple way to make a+b work in the following example: #include <utility> #include <iostream> int main () { std::pair<int, int> a=std::make_pair(1,2); std::pair<int, int> b=std::make_pair(3,3); std::pair<int, int> c = a+b; return 0; } 回答1: template <typename T,typename U> std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second}; } int main () { std::pair<int, int> a=std::make_pair(1,2); std::pair<int, int> b=std::make

What is the reason for the entire C++ STL code to be included in the .h rather than .cpp/.c files?

偶尔善良 提交于 2019-12-23 12:43:04
问题 I just downloaded the STL source code and I noticed all the definition for the STL template classes are included in the .h file. The actual source code for the function definition is in the .h file rather than .cpp/.c file. What is the reason for this? http://www.sgi.com/tech/stl/download.html 回答1: Because very few compilers implement linking of templates. It's hard. Here's a brief but (I think) informative article about it: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=53 I