std-pair

How can I print out C++ map values?

时光怂恿深爱的人放手 提交于 2019-11-27 09:45:16
问题 I have a map like this: map<string, pair<string,string> > myMap; And I've inserted some data into my map using: myMap.insert(make_pair(first_name, make_pair(middle_name, last_name))); How can I now print out all the data in my map? 回答1: for(map<string, pair<string,string> >::const_iterator it = myMap.begin(); it != myMap.end(); ++it) { std::cout << it->first << " " << it->second.first << " " << it->second.second << "\n"; } In C++11, you don't need to spell out map<string, pair<string,string>

What is std::pair?

别来无恙 提交于 2019-11-27 09:36:26
问题 What is std::pair for, why would I use it, and what benefits does boost::compressed_pair bring? 回答1: std::pair is a data type for grouping two values together as a single object. std::map uses it for key, value pairs. While you're learning pair, you might check out tuple. It's like pair but for grouping an arbitrary number of values. tuple is part of TR1 and many compilers already include it with their Standard Library implementations. Also, checkout Chapter 1, "Tuples," of the book The C++

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

坚强是说给别人听的谎言 提交于 2019-11-27 08:37:42
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? 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 a plain T[N] array, but when you're dealing with a non-aggregate element type, you may need to be more

Preferred/idiomatic way to insert into a map

余生长醉 提交于 2019-11-27 06:02:49
I have identified four different ways of inserting 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?) icecrime First of all, operator[] and insert member functions are not functionally equivalent : The operator[] will search for the key, insert a default constructed value if not found, and return a reference to which you assign a value. Obviously

How can I make an unordered set of pairs of integers in C++?

前提是你 提交于 2019-11-27 05:35:07
问题 The following program does not compile an unordered set of pairs of integers, but it does for integers. Can unordered_set and its member functions be used on user-defined types, and how can I define it? #include <unordered_set> ... class A{ ... private: std::unordered_set< std::pair<int, int> > u_edge_; }; Compiler error: error: no matching function for call to 'std::unordered_set >::unordered_set()' 回答1: Your code compiles on VS2010 SP1 (VC10), but it fails to compile with GCC g++ 4.7.2.

What is the purpose of std::make_pair vs the constructor of std::pair?

情到浓时终转凉″ 提交于 2019-11-27 05:03:18
问题 What is the purpose of std::make_pair ? Why not just do std::pair<int, char>(0, 'a') ? Is there any difference between the two methods? 回答1: The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the type of the elements that are passed to it, without you needing to tell it. That's what I could gather from various docs anyways. See this example from http://www.cplusplus.com/reference/std/utility/make_pair/ pair <int

What is the difference between using a struct with two fields and a pair?

♀尐吖头ヾ 提交于 2019-11-27 04:29:21
问题 What is the difference regarding memory allocation and efficiency between using a struct with two fields and a pair? 回答1: std::pair provides pre-written constructors and comparison operators. This also allows them to be stored in containers like std::map without you needing to write, for example, the copy constructor or strict weak ordering via operator < (such as required by std::map ). If you don't write them you can't make a mistake (remember how strict weak ordering works?) so it's more

find_if and std::pair, but just one element

依然范特西╮ 提交于 2019-11-27 02:55:15
问题 Suppose i have the following code: std::vector< std::pair <int, char> > myVec; or std::list< std::pair <int, char> > myList; /* then ***************/ std::list< std::pair <int, char> >::iterator listIt; or std::vector< std::pair <int, char> >::iterator vectorIt; /* No difference between vector and list */ Now i need to search just for one int element in them, So: vectorIt = std::find_if(myVec.begin(),myVect.end(),make_pair(.....)); ^^^^^^^^^^^^^^^^^ how can i do it? 回答1: Write a unary

What is C# analog of C++ std::pair?

↘锁芯ラ 提交于 2019-11-27 02:37:10
I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based. Thank you! Jorge Ferreira Tuples are available since .NET4.0 and support generics: Tuple<string, int> t = new Tuple<string, int>("Hello", 4); In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following: public class Pair<T, U> { public Pair() { } public Pair(T first, U second) { this.First = first; this.Second = second; } public T First { get; set; } public U Second { get; set; } }; And use it like this: Pair

why do i need to use piecewise_construct in map::emplace for single arg constructors of noncopyable objects?

倖福魔咒の 提交于 2019-11-27 01:32:49
问题 The following code will not compile on gcc 4.8.2. The problem is that this code will attempt to copy construct an std::pair<int, A> which can't happen due to struct A missing copy and move constructors. Is gcc failing here or am I missing something? #include <map> struct A { int bla; A(int blub):bla(blub){} A(A&&) = delete; A(const A&) = delete; A& operator=(A&&) = delete; A& operator=(const A&) = delete; }; int main() { std::map<int, A> map; map.emplace(1, 2); // doesn't work map.emplace(std