stl

list of polymorphic objects

帅比萌擦擦* 提交于 2019-12-28 18:44:21
问题 I have a particular scenario below. The code below should print 'say()' function of B and C class and print 'B says..' and 'C says...' but it doesn't .Any ideas.. I am learning polymorphism so also have commented few questions related to it on the lines of code below. class A { public: // A() {} virtual void say() { std::cout << "Said IT ! " << std::endl; } virtual ~A(); //why virtual destructor ? }; void methodCall() // does it matters if the inherited class from A is in this method { class

C++ std::find with a custom comparator

我是研究僧i 提交于 2019-12-28 16:30:45
问题 This is basically what I want to do: bool special_compare(const string& s1, const string& s2) { // match with wild card } std::vector<string> strings; strings.push_back("Hello"); strings.push_back("World"); // I want this to find "Hello" find(strings.begin(), strings.end(), "hell*", special_compare); // And I want this to find "World" find(strings.begin(), strings.end(), "**rld", special_compare); But std::find doesn't work like that unfortunately. So using only the STL, how can I do

Move list element to the end in STL

送分小仙女□ 提交于 2019-12-28 16:30:21
问题 I have already the list pointer of CDrawObject* std::list<CDrawObject*> elements; How I can move some element to the end of list. I see STL Algorithms Reference but i don't find this operations. How i can do it? 回答1: Use the list method splice() void list::splice ( iterator position, list<T,Allocator>& x, iterator i ); Move iterator i from list x into current list at position "position" Thus to move it to the end put x.splice( x.end(), x, iter ); (they can both be the same list or different

Pick a unique random subset from a set of unique values

时间秒杀一切 提交于 2019-12-28 14:55:27
问题 C++. Visual Studio 2010. I have a std::vector V of N unique elements ( heavy structs). How can efficiently pick M random, unique, elements from it? E.g. V contains 10 elements: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } and I pick three... 4, 0, 9 0, 7, 8 But NOT this: 0, 5, 5 <--- not unique! STL is preferred. So, something like this? std::minstd_rand gen; // linear congruential engine?? std::uniform_int<int> unif(0, v.size() - 1); gen.seed((unsigned int)time(NULL)); // ...? // Or is there a good

Pick a unique random subset from a set of unique values

半腔热情 提交于 2019-12-28 14:54:00
问题 C++. Visual Studio 2010. I have a std::vector V of N unique elements ( heavy structs). How can efficiently pick M random, unique, elements from it? E.g. V contains 10 elements: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } and I pick three... 4, 0, 9 0, 7, 8 But NOT this: 0, 5, 5 <--- not unique! STL is preferred. So, something like this? std::minstd_rand gen; // linear congruential engine?? std::uniform_int<int> unif(0, v.size() - 1); gen.seed((unsigned int)time(NULL)); // ...? // Or is there a good

small string optimization for vector?

倾然丶 夕夏残阳落幕 提交于 2019-12-28 06:18:16
问题 I know several (all?) STL implementations implement a "small string" optimization where instead of storing the usual 3 pointers for begin, end and capacity a string will store the actual character data in the memory used for the pointers if sizeof(characters) <= sizeof(pointers). I am in a situation where I have lots of small vectors with an element size <= sizeof(pointer). I cannot use fixed size arrays, since the vectors need to be able to resize dynamically and may potentially grow quite

Remove duplicates from a list<int>

喜欢而已 提交于 2019-12-28 04:20:09
问题 Using STL algorithms (as much as possible) such as remove_if() and list::erase , is there a nice way to remove duplicates from a list defined as following: list<int> l; Please note that list::unique() only works if duplication occurs in consecutive elements. In my case, all duplicates have to be eliminated regardless of their position in the list. Moreover, removing duplicates mean preserving only one copy of each element in the final result. EDIT: The option to l.sort() followed by l.unique(

Can I use const in vectors to allow adding elements, but not modifications to the already added?

孤者浪人 提交于 2019-12-28 02:40:34
问题 My comments on this answer got me thinking about the issues of constness and sorting. I played around a bit and reduced my issues to the fact that this code: #include <vector> int main() { std::vector <const int> v; } will not compile - you can't create a vector of const ints. Obviously, I should have known this (and intellectually I did), but I've never needed to create such a thing before. However, it seems like a useful construct to me, and I wonder if there is any way round this problem -

Can I use const in vectors to allow adding elements, but not modifications to the already added?

和自甴很熟 提交于 2019-12-28 02:40:13
问题 My comments on this answer got me thinking about the issues of constness and sorting. I played around a bit and reduced my issues to the fact that this code: #include <vector> int main() { std::vector <const int> v; } will not compile - you can't create a vector of const ints. Obviously, I should have known this (and intellectually I did), but I've never needed to create such a thing before. However, it seems like a useful construct to me, and I wonder if there is any way round this problem -

Can we use a user defined class for the key in a STL map?

本秂侑毒 提交于 2019-12-28 02:13:08
问题 I need a key in the map, however, I found it should be multiple data. Can I put these data in one user defined class and put the whole class as a key in the map? Will it impact the time efficiency? What other concerns should be applied here? 回答1: Any type can be used as a key as long as it is Copyable Assignable Comparable, since the map is sorted by key If your class is just a simple structure, then it's already copyable and assignable. For a class to be comparable, you must either implement