stl

std::regex_match with another Allocator

倖福魔咒の 提交于 2019-12-24 17:17:39
问题 I'm stuck how to use the regex_match template with my own Memory STL Allocator. This is my code: FaF::smatch stringResults; std::regex expression( "expression" ); std::regex_match( FaF::string-variable, stringResults, expression ); For std::match and std::string I was successful and therefore I use it in the above example: namespace FaF { using smatch = std::match_results<std::string::const_iterator, Allocator<std::string::const_iterator>>; using string = std::basic_string<char, std::char

why do STL containers use copying to populate in resize?

♀尐吖头ヾ 提交于 2019-12-24 16:17:27
问题 all the STL containers that implement resize use copies to populate the new elements even if the source of the copy is a default constructed object? Why is it done this way? I see no advantage and some cost. As context, I ran across this while looking for a random access container for elements that can't be copied: 回答1: It saves on complexity. We certainly need the copy-construction case, and default-construction can be modeled as replicating a default-constructed object. The performance

C++ STL非更易型算法min_element、max_element、minmax_element使用方法

被刻印的时光 ゝ 提交于 2019-12-24 15:51:32
最小值和最大值 这些算法分别返回[_First,_Last)区间中的最小元素位置、最大元素位置.或最小和最大元素位置所组成的pair 上述没有_Pred实参的各个版本将以operator<进行元素比较 _Pred用来比较两个元素,_Pred(elem1,elem2)如果第一元素小于第二元素,应当返回true 如果存在多个最小值或最大值,min_element()和max_element()返回找到的第一个目标元素.minmax_element()返回第一个最小元素和最后一个最大元素. 复杂度:线性 使用例子: bool absLess ( int elem1 , int elem2 ) { return abs ( elem1 ) < abs ( elem2 ) ; } int main ( ) { deque < int > coll ; INSERT_ELEMENTS ( coll , 2 , 6 ) ; INSERT_ELEMENTS ( coll , - 3 , 6 ) ; PRINT_ELEMENTS ( coll ) ; cout << "minimum: " << * min_element ( coll . cbegin ( ) , coll . cend ( ) ) << endl ; cout << "maximum: " << * max_element (

Efective search in set with non-key type

醉酒当歌 提交于 2019-12-24 15:40:03
问题 I have a similar data structure like this: struct Data { std::string id; Blob data; }; Now I can use a std::map to store the structure and search by ID, but I searching for a way to achieve the same thing with a std::set (since I don't really need to separate the ID and the structure). std::set::find of course takes the key type as a parameter, so I could do something like this (with the appropriate constructor): set<Data> x; x.find(Data("some_id")); But I would like to avoid this if possible

c++ singleton implementation STL thread safe

自古美人都是妖i 提交于 2019-12-24 15:33:42
问题 I've been trying to implement a singleton with some C++11 features from STL. I read about a few implementations and I found this pretty good: http://silviuardelean.ro/2012/06/05/few-singleton-approaches/ I made a few changes and got the bellow code working on VS2013, but I still would like to know: a) Is this implementation thread-safe? b) Is it ok (good practice) to return a shared_ptr from GetInstance instead of a reference? PS.: My singleton is ment to be an OpenGL interface (that's the

best c++11 way to store a vector of pointers

◇◆丶佛笑我妖孽 提交于 2019-12-24 14:40:28
问题 I want to store a vector of std::threads Currently, I implemented it as std::vector<std::thread*> However, this requires manually deleting the std::thread s What would be the most elegant c++11 way to do this? I could see std::shared_ptr , but isn't it an overkill? The pointers are unique, but std::vector needs to copy them temporarily. Maybe I do not need pointers, but std::thread being non copyable, I think I do. Thanks! 回答1: Since C++11, vector only requires that its values are movable, as

This hash only works for enumeration types

会有一股神秘感。 提交于 2019-12-24 14:25:28
问题 I'm working on a (very) simple class in C++ that has a unordered_map member: class SDLFontManager : public CPObject, public FontManagerProtocol { public: SDLFontManager() {}; // flush the cache when the manager is destroyed virtual ~SDLFontManager() { flushFontCache(); }; // load a font from the cache if it exists, or load it from file and cache it. virtual FontProtocol* fontWithTTF(const char* filename) { if(_cache.find(filename) != _cache.end()) { return _cache[filename]; } SDLFont* font =

Why does my std::wofstream write ansi?

本秂侑毒 提交于 2019-12-24 13:33:59
问题 I've got wide string and I'm writing it to a wofstream that I opened in out|binary mode. When I look in the resultant file, it's missing every other byte. I was expecting that when I opened the file in visual studio with the binary editor that I'd see every other byte as a zero, but I'm not seeing the zeros. Do you know what I'm missing? Thanks. The code is something like this: CAtlStringW data = L"some data"; wofstream stream("c:\hello.txt", ios_base:out|ios_base:binary); stream.write( data

Does const containers have only const iterator?

我只是一个虾纸丫 提交于 2019-12-24 13:15:36
问题 Why do const STL containers only return const_iterator s? For example both std::vector and std::list have the method begin overloaded as: iterator begin(); const_iterator begin() const; const_iterator cbegin() const; I thought I could still modify values of a const vector but not the vector itself. According to the standard library there is no difference between: const std::vector<int> and const std::vector<const int> 回答1: Suppose you have iterator begin() const; instead of const_iterator

How to insert unique items into vector?

偶尔善良 提交于 2019-12-24 12:43:31
问题 I have a type called Neighbors : typedef vector<pair<data,int>> Neighbors; and here's data : struct data { int par[PARAMETERS]; int cluster; bool visited; bool noise; }; I'm trying to write a function that inserts values from _NeighborPts to NeighborPts (but only ones that aren't already in NeighborPts ): void insert_unique(Neighbors* NeighborPts, const Neighbors& _NeighborPts) { Neighbors_const_it _it = _NeighborPts.begin(); while(_it != _NeighborPts.end()) { if(/* _it->first.par isn't in