stl

How can I order a map by value efficiently?

梦想的初衷 提交于 2020-01-04 04:09:08
问题 Consider a std::map<K,V> . I want to re-order the map by value profiting by an appropriate container std::C<V*> or std::C<V&> , in a way that no copies of values are done to store the elements in C. Furthermore, elements in C must be sorted according to the result of int f(V&) applied to each element. Despite my efforts I could not find an appropriate C and an enough efficient way to build it. Do you have any solution? A small example would be much appreciated. 回答1: Seems simple enough. std:

Complexity of STL max_element

微笑、不失礼 提交于 2020-01-04 04:07:07
问题 So according to the link here: http://www.cplusplus.com/reference/algorithm/max_element/ , the max_element function is O(n), apparently for all STL containers. Is this correct? Shouldn't it be O(log n) for a set (implemented as a binary tree)? On a somewhat related note, I've always used cplusplus.com for questions which are easier to answer, but I would be curious what others think of the site. 回答1: It's linear because it touches every element. It's pointless to even use it on a set or other

Random integers from a function always return the same number - why?

耗尽温柔 提交于 2020-01-04 03:51:16
问题 I'm currently learning C++ and have a question about the random_device. I'm trying to simulate the monty hall problem. For this I need random integers to pick random doors. Therefore I tried making a function that returns random integers for this purpose. int guessGetRandomIndex() { std::random_device rd; // obtain a random number from hardware std::mt19937 eng(rd()); // seed the generator std::uniform_int_distribution<> distr(0, 2); return distr(eng); However when I try to get random

How do I find all the keys common in 2 maps?

雨燕双飞 提交于 2020-01-04 03:50:56
问题 I have 3 maps: map<string, vector<int> > map1 map<string, vector<int> > map2 map<string, vector<int> > map3 I need to create a third map with all the strings existing in both map1 and map2 with their corresponding vectors. Thing is, even if the strings are same, their vectors can be different and i have to append all the vectors from both the common strings into 1 vector. This is what I'm trying but I'm kind of lost: for(map<string, vector<int> >::iterator it1=map1.begin(); it1 != map1.end();

Sort and keep track of elements

♀尐吖头ヾ 提交于 2020-01-04 02:55:13
问题 Just to know, I'm talking C++ now. Suppose I have an array A = {4, 1, 5, 2, 3} and sort it in A_sorted = {1, 2, 3, 4, 5} . I would like to keep the following information: where is now element e (from array A) in the sorted array A_sorted? e.g.: element with index 2 in A ( 5 ) has now index 4 in A_sorted. The question is more like: can one use STL to achieve this? 回答1: There's no off-the-shelf functionality to achieve this, but there are work-arounds. You can, for example, keep an array of

Which version of the Dinkumware STL Lib ships with Visual Studio 2012?

穿精又带淫゛_ 提交于 2020-01-04 01:14:11
问题 In earlier versions of Visual Studio, there was a predefinied macro "_CPPLIB_VER" which reported the version of the Dinkumware STL Library shipped with this version of VS. As of 2012, I am unable to find or use this macro - it is undefinied and I am unable to find any information on this matter online. Is the Dinkumware STL Lib no longer used in VS2012? Was the macro renamed? Windows 7 x64 Microsoft Visual Studio 2012 Professional 回答1: Usually the definition of _CPPLIB_VER can be found in a

STL - Use member functions or functions in <algorithm>?

泪湿孤枕 提交于 2020-01-03 18:33:08
问题 I thought of this problem due to some of the answers I got to the following problem suggesting that I could use vector<T>::assign or copy with a back_insert_iterator here. My problem is, what are the drawbacks and advantages of using one method over another? 回答1: assign overwrites the content of the vector where as copy with back_insert_iterator does a push_back on the vector thus preseving its content. EDIT : If the question is generic (i.e. whether to use a member function defined in the

How can I get the address of the buffer allocated by vector::reserve()?

两盒软妹~` 提交于 2020-01-03 18:32:30
问题 I have a std::vector of values for which I know the maximum size, but the actual size will vary during usage: void setupBuffer(const size_t maxSize) { myVector.reserve(maxSize); } void addToBuffer(const Value& v) { myVector.push_back(v); if (myVector.size() == maxSize) { // process data... myVector.clear(); } } However, in setupBuffer, I need to obtain a pointer to the start of myVector's data. I'm using a third party library where I must cache this pointer up front for use in a call made

Passing std::string in a library API

女生的网名这么多〃 提交于 2020-01-03 15:56:15
问题 We are currently building an API for a certain library. Part of the interface requires the library to get and return to the user classes such as vector and string. When trying to simulate use of the library in a simple scenario, in debug mode the system crush when delivering a string as an input. I believe there is a different representation of the string class in debug or release mode. Then our library assumes to receive a certain representation, read a data member incorrectly and crush

Discrepencies between std::lower_bound and std::set::lower_bound

故事扮演 提交于 2020-01-03 15:55:44
问题 The C++ draft says about std::lower_bound: § 25.4.3.1 lower_bound [lower.bound] template<class ForwardIterator, class T> ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value); template<class ForwardIterator, class T, class Compare> ForwardIterator lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); Requires: The elements e of [first,last) shall be partitioned with respect to the expression e < value or comp(e, value).