std-pair

Sorting a std::vector<std::pair<std::string,bool>> by the string?

*爱你&永不变心* 提交于 2019-11-26 23:06:06
How can I sort this vector by comparing the pair.first which is an std::string ? (without providing a static compare function, nor use boost). std::vector<std::pair<std::string, bool> > v; std::sort(v.begin(), v.end()); std::pair overloads operator< to sort first by the first element then by the second element. Thus, if you just sort the vector using the default sort ordering ( operator< ), you'll get your desired ordering. I really like James' answer, but there's one other option you might want to consider - just funnel everything into a std::map : std::map<std::string, bool> myMap(v.begin(),

converting a variable name to a string in C++

血红的双手。 提交于 2019-11-26 19:44:26
问题 I'd like to output some data to a file. For example assume I have two vectors of doubles: vector<double> data1(10); vector<double> data2(10); is there an easy way to output this to a file so that the first row contains the headings 'data1' and 'data2' followed by the actual contents. The function which outputs the data will be passed various different arrays so hardcoding the name of the heading is not possible - ideally I'd like to convert the variable name to some string and then output

pair<int,int> pair as key of unordered_map issue

冷暖自知 提交于 2019-11-26 18:19:28
问题 My code: typedef pair<int,int> Pair tr1::unordered_map<Pair,bool> h; h.insert(make_pair(Pair(0,0),true)); Erorr undefined reference to `std::tr1::hash<std::pair<int, int> >::operator()(std::pair<int, int>) const' Something I need to fix? thanks 回答1: This happens because there is no specialization for std::tr1::hash<Key> with Key = std::pair<int, int> . You must to specialize std::tr1::hash<Key> with Key = std::pair<int, int> before declaring tr1::unordered_map<Pair,bool> h; . This happens

How should I brace-initialize an std::array of std::pairs?

时间秒杀一切 提交于 2019-11-26 17:46:09
问题 std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was ambiguous` What am I doing wrong? 回答1: Add another pair of braces. std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } }; std::array<T, N> is an aggregate class containing a member of type T[N] . Usually, you can initialise that the same way you would

Why was pair range access removed from C++11?

放肆的年华 提交于 2019-11-26 15:31:00
问题 I just discovered that at one point, the C++11 draft had std::begin / std::end overloads for std::pair that allowed treating a pair of iterators as a range suitable for use in a range-based for loop (N3126, section 20.3.5.5), but this has since been removed. Does anyone know why it was removed? I find the removal very unfortunate, because it seems there is no other way to treat a pair of iterators as a range. Indeed: The lookup rules for begin/end in a range-based for loop say that begin/end

What is the preferred/idiomatic way to insert into a map?

丶灬走出姿态 提交于 2019-11-26 11:49:16
问题 I have identified four different ways of inserting elements into a std::map : std::map<int, int> function; function[0] = 42; function.insert(std::map<int, int>::value_type(0, 42)); function.insert(std::pair<int, int>(0, 42)); function.insert(std::make_pair(0, 42)); Which of those is the preferred/idiomatic way? (And is there another way I have not thought of?) 回答1: First of all, operator[] and insert member functions are not functionally equivalent : The operator[] will search for the key,

Sorting a std::vector<std::pair<std::string,bool>> by the string?

谁说胖子不能爱 提交于 2019-11-26 08:32:47
问题 How can I sort this vector by comparing the pair.first which is an std::string ? (without providing a static compare function, nor use boost). 回答1: std::vector<std::pair<std::string, bool> > v; std::sort(v.begin(), v.end()); std::pair overloads operator< to sort first by the first element then by the second element. Thus, if you just sort the vector using the default sort ordering ( operator< ), you'll get your desired ordering. 回答2: I really like James' answer, but there's one other option

What is the equivalent of the C++ Pair<L,R> in Java?

[亡魂溺海] 提交于 2019-11-26 01:19:12
问题 Is there a good reason why there is no Pair<L,R> in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own. It seems that 1.6 is providing something similar ( AbstractMap.SimpleEntry<K,V> ), but this looks quite convoluted. 回答1: In a thread on comp.lang.java.help, Hunter Gratzner gives some arguments against the presence of a Pair construct in Java. The main argument is that a class Pair doesn't convey any semantics about the relationship between