stl

Alignment and the STL in VS 2012/VC11

▼魔方 西西 提交于 2020-01-05 00:49:13
问题 I have a vague memory of the STL having trouble with aligned structs (e.g. SIMD vectors placed in a std::vector), unless you specify a custom allocator. According to this document VS 2012/VC11 has partial support for c++ alignment. Does this mean that the VS STL implementation can handle aligned structs now, without providing a custom allocator? 回答1: No. It means that the VC++ compiler supports a method for specifying the required alignment for a type (the __declspec(align(N)) syntax). VC++

no match for operator= using a std::vector

一世执手 提交于 2020-01-04 13:51:48
问题 I've got a class declared like this: class Level { private: std::vector<mapObject::MapObject> features; (...) }; and in one of its member functions I try to iterate through that vector like this: vector<mapObject::MapObject::iterator it; for(it=features.begin(); it<features.end(); it++) { /* loop code */ } This seems straightforward to me, but g++ gives me this error: src/Level.cpp:402: error: no match for ‘operator=’ in ‘it = ((const yarl::level::Level*)this)->yarl::level::Level::features

C++ vector loses data in recursive function

主宰稳场 提交于 2020-01-04 09:12:45
问题 I am very new to C++ and I am trying to implement a TriangleDynamic object that can recursively split itself using a function called splitTriangleProject. It splits itself into four smaller TriangleDynamic objects (and projects the vertices of the new triangles onto a sphere with a given radius and origin, but I believe this is beside the point), pushes the newly created triangles into a vector that is part of the member data of the original object. The vector member data is called

Inserting in a multiset: before the first occurence of that value instead of after the last occurence

不想你离开。 提交于 2020-01-04 06:34:12
问题 As the title says multiset inserts a value at the end of the range of all the same values. (Ex: Inserting 2 in a multiset 1,2,2,3 makes it 1,2,2,/*new*/ 2,3 ). How do I get the new value inserted at the start of the range of all the same values? (Ex: Inserting 2 in multiset 1,2,2,3 should make 1,/*new*/ 2,2,2,3 ) 回答1: Try this std::multiset<int> mset { 2,4,5,5,6,6 }; int val = 5; auto it = mset.equal_range ( val ).first; //Find the first occurrence of your target value. Function will return

How to use STL algorithms in Qt?

旧街凉风 提交于 2020-01-04 06:30:52
问题 While reading "c++ gui programming eith Qt 4 , second edition " I came across this topic : "The STL header provides a more complete set of generic algorithms. these algorithms can be used on Qt containers as well as STL containers. If STL implementations are available on all your platforms, there is probably no reason to avoid using the STL algorithms when Qt lacks an equivalent algorithm ." It states that the generic algorithms of STL(which is defined in "algorithm" header) can be used with

can I do this binary search using the STL?

独自空忆成欢 提交于 2020-01-04 06:24:41
问题 I am trying to refactor some code that doesn't use the STL to use the generic algorithms it provide. I have a struct like this : struct A { int i; //other stuff... }; // ... A* array; // array of A objects, sorted by A::i member int n = ...; // array size there is then a function that was coded that takes A , n and an integer k , whose purpose is to give me pointers to to the first and the last element of the array that have their i member equal to k . This is implemented by hand in terms of

how to provide a swap function for my class?

强颜欢笑 提交于 2020-01-04 06:18:33
问题 What is the proper way to enable my swap in STL algorithms? 1) Member swap . Does std::swap use SFINAE trick to use the member swap . 2) Free standing swap in the same namespace. 3) Partial specialization of std::swap . 4) All of the above. Thank you. EDIT: Looks like I didn't word my question clearly. Basically, I have a template class and I need STL algos to use the (efficient) swap method I wrote for that class. 回答1: 1) is the proper use of swap . Write it this way when you write "library"

Understanding std::swap(). What is the purpose of tr1::_Remove_reference?

蹲街弑〆低调 提交于 2020-01-04 05:36:11
问题 In the STL implementation of VS10 there is the following code for a variant of std::swap(): // TEMPLATE FUNCTION _Move template<class _Ty> inline typename tr1::_Remove_reference<_Ty>::_Type&& _Move(_Ty&& _Arg) { // forward _Arg as movable return ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg); } // TEMPLATE FUNCTION swap (from <algorithm>) template<class _Ty> inline void swap(_Ty& _Left, _Ty& _Right) { // exchange values stored at _Left and _Right _Ty _Tmp = _Move(_Left); _Left = _Move(

Circular buffer with pre-allocated buffer?

删除回忆录丶 提交于 2020-01-04 04:46:06
问题 Does any library has a Circular buffer class that can be used with pre-allocated buffer? I looked at Boost::circular_buffer, but it seems all of its constructors require an allocator. I don't want to reinvent circular buffer class, but have to use pre-allocated buffer. I want something like: char buffer[1000]; // pre-allocated buffer. circular_buffer_class cb; // a class that provides the interface as a circular buffer. cb.attach(buffer, 1000); // attaching the preallocated buffer to the

Is the following code using std::set “legal”?

青春壹個敷衍的年華 提交于 2020-01-04 04:21:11
问题 I have this code: set<int>::iterator new_end = set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), set1.begin()); set1.erase(new_end, set1.end); It compiles and runs fine in visual studio. However, in a previous question, people stated that a set 's iterators are supposed to be const . I don't see anything like that in the standard. Can someone tell me where it says that, or if this is well-defined behavior? If it's not, please provide code that does what I need. Is there a way