stl

Inserting to std::unordered_map calls hash function twice in MSVC++'s STL, bad design or special reason?

人盡茶涼 提交于 2020-08-27 02:23:12
问题 For this code: #include<unordered_map> #include<iostream> using namespace std; struct myhash { unsigned operator()(const unsigned&v)const { cout<<"Hash function is called:"<<v<<endl; return v; } }; unordered_map<unsigned,unsigned,myhash>mp; int main() { for (unsigned i=0;i<3;++i) { cout<<"Visiting hash table:"<<i<<endl; ++mp[i]; } } When using g++, there's no surprise with the output: Visiting hash table:0 Hash function is called:0 Visiting hash table:1 Hash function is called:1 Visiting hash

ArgMin for vector<double> in C++?

南笙酒味 提交于 2020-08-24 06:00:04
问题 I'd like to find the index of the minimum value in a C++ std::vector<double> . Here's a somewhat verbose implementation of this: //find index of smallest value in the vector int argMin(std::vector<double> vec) { std::vector<double>::iterator mins = std::min_element(vec.begin(), vec.end()); //returns all mins double min = mins[0]; //select the zeroth min if multiple mins exist for(int i=0; i < vec.size(); i++) { //Note: could use fabs( (min - vec[i]) < 0.01) if worried about floating-point

ArgMin for vector<double> in C++?

别来无恙 提交于 2020-08-24 05:57:28
问题 I'd like to find the index of the minimum value in a C++ std::vector<double> . Here's a somewhat verbose implementation of this: //find index of smallest value in the vector int argMin(std::vector<double> vec) { std::vector<double>::iterator mins = std::min_element(vec.begin(), vec.end()); //returns all mins double min = mins[0]; //select the zeroth min if multiple mins exist for(int i=0; i < vec.size(); i++) { //Note: could use fabs( (min - vec[i]) < 0.01) if worried about floating-point

Performing set_difference on unordered sets

谁说胖子不能爱 提交于 2020-08-04 09:22:27
问题 The set_difference algorithm requires the following The elements in the ranges shall already be ordered according to this same criterion which is not the case for hash tables. I'm thinking of implementing a set difference A-B in terms of std::remove_copy where the removal criterion would be the existence of an element of A in the set B. Is there a standard-valid-fastest-safest way to do it? 回答1: If you have two hash tables, the most efficient way should be to iterate over one of them, looking

What is the use of value_type in STL containers?

允我心安 提交于 2020-08-01 06:29:11
问题 What's the use of value_type in STL containers? From the MSDN: // vector_value_type.cpp // compile with: /EHsc #include <vector> #include <iostream> int main( ) { using namespace std; vector<int>::value_type AnInt; AnInt = 44; cout << AnInt << endl; } I don't understand what does value_type specifically achieve here? The variable could be an int as well? Is it used because the coders are lazy to check what's the type of objects present in the vector? Once I am clear about this I think I will

Replacing std::transform, inserting into a std::vector

走远了吗. 提交于 2020-07-22 22:07:42
问题 I'm looking to insert values into a std::vector in a way like std::transform. std::transform needs a pre-sized third argument, but in my case, the size depends on transformers() and is not predictable. ... // std::vector<int> new_args(); <-- not working std::vector<int> new_args(args.size()); std::transform(args.begin(),args.end(),new_args.begin(),transformers()); Is there a std:transform-ish way to insert values into a std::vector? 回答1: You do not need to pre size the vector that is going to

Replacing std::transform, inserting into a std::vector

故事扮演 提交于 2020-07-22 22:07:06
问题 I'm looking to insert values into a std::vector in a way like std::transform. std::transform needs a pre-sized third argument, but in my case, the size depends on transformers() and is not predictable. ... // std::vector<int> new_args(); <-- not working std::vector<int> new_args(args.size()); std::transform(args.begin(),args.end(),new_args.begin(),transformers()); Is there a std:transform-ish way to insert values into a std::vector? 回答1: You do not need to pre size the vector that is going to

Advance iterator for the std::vector std::advance VS operator +?

女生的网名这么多〃 提交于 2020-07-17 09:43:06
问题 I found myself writing the following a lot: int location =2; vector<int> vec; vector<int>::iterator it=vec.begin(); /..../ std::advance(it, location); instead of it= it + 5; what is the Preferred/Recommended way ? 回答1: Adding will only work with random access iterators. std::advance will work with all sorts of iterators. As long as you're only dealing with iterators into vectors, it makes no real difference, but std::advance keeps your code more generic (e.g. you could substitute a list for