C++ vector of pairs initialization
Here you go: #include <utility> vector<pair<int, int>> myVec (N, std::make_pair(-1, -1)); The second argument to that constructor is the initial value that the N pairs will take.
Here you go: #include <utility> vector<pair<int, int>> myVec (N, std::make_pair(-1, -1)); The second argument to that constructor is the initial value that the N pairs will take.
std::map::insert takes a single argument: the key-value pair, so you would need to use: mapa.insert(std::make_pair(p1, “Manzana”)); You should use std::string instead of C strings in your types. As it is now, you will likely not get the results you expect because looking up values in the map will be done by comparing pointers, not by … Read more
There’s a problem with how std::pair is defined. I’d even say it’s a minor defect in the standard. It has two constructors that could be used here: pair(const T1 &x, const T2 &y);, where T1, T2 are template parameters of the pair. template <class U1, class U2> pair(U1 &&x, U2 &&y); If you do std::pair<A, … Read more
compressed_pair uses some template trickery to save space. In C++, an object (small o) can not have the same address as a different object. So even if you have struct A { }; A‘s size will not be 0, because then: A a1; A a2; &a1 == &a2; would hold, which is not allowed. But … Read more
You are missing some crucial information: What compiler do you use? What do you use to measure the performance of the microbenchmark? What standard library implementation do you use? My system: g++ (GCC) 4.9.1 20140903 (prerelease) GLIBCXX_3.4.20 Anyhow, I ran your examples, but reserved the proper size of the vectors first to get rid of … Read more
std::map<X, Y>: is an ordered structure with respect to keys (that is, when you iterate over it, keys will be always increasing). supports unique keys (Xs) only offers fast find() method (O(log n)) which finds the Key-Value pair by Key offers an indexing operator map[key], which is also fast std::list<std::pair<X, Y> >: is a simple … Read more
You can use the preprocessor “stringify” # to do what you want: #include <stdio.h> #define PRINTER(name) printer(#name, (name)) void printer(char *name, int value) { printf(“name: %s\tvalue: %d\n”, name, value); } int main (int argc, char* argv[]) { int foo = 0; int bar = 1; PRINTER(foo); PRINTER(bar); return 0; } name: foo value: 0 name: … Read more
I think the 2009 paper “Pairs do not make good ranges” by Alisdair Meredith is at least part of the answer. Basically, many algorithms return pairs of iterators that are actually not guaranteed to be valid ranges. It seems they removed the support for pair<iterator,iterator> from the for-range loop for this reason. However, the proposed … Read more
Use std::make_pair: revenue.push_back(std::make_pair(“string”,map[i].second));
C++11 allows you to do: for (const auto& kv : myMap) { std::cout << kv.first << ” has value ” << kv.second << std::endl; } C++17 allows you to do: for (const auto& [key, value] : myMap) { std::cout << key << ” has value ” << value << std::endl; } using structured binding. UPDATE: … Read more