How to add elements to a vector of tuples - c++

I just have this:
std::vector<int[2]> ints;
how can I add elements to this vector?
using either ints.insert() or ints.push_back()?
No idea how to do this, C/C++ newb.

With C++ 11:
std::vector<std::tuple<int, int, int>> vec;
vec.emplace_back(0, 1, 2);

Here is a way you can achieve the functionality you are looking for:
std::vector<std::tuple<int, int>> ints;
Then you would add a tuple to the vector like this:
ints.push_back(std::make_tuple(1, 2));
Edit/Update:
if you were looping over you vector and i is your integer index used during the loop, then to access to tuple you can do this:
int intOne, intTwo;
intOne = std::get<0>(ints[i]);
intTwo = std::get<1>(ints[i]);
here is some more info on get for a tuple

You can use extract tuple using std::get
Here is the C++ 11 link with example:
http://en.cppreference.com/w/cpp/utility/tuple/get
The full article about tuples is here:
http://en.cppreference.com/w/cpp/utility/tuple

Related

Initilize a vector of Pair<int,int> with emplace_back

I want to initialize a vector of integer pairs while specifying its size (I have to use this structure). I tried:
vector<pair<int, int>> container;
container.emplace_back(size);
And:
container.emplace_back(size, make_pair(0, 0));
But I keep having this error:
error: no matching function for call to 'std::pair<int, int>::pair(long long unsigned int&, std::pair<int, int>)'
Is there any solution or different approach?
Thank you!
emplace_back forwards its parameters to the elements constructor. std::pair<int,int> has no constructor that takes a size and a pair, hence the error. To emplace an element:
std::vector<std::pair<int, int>> container;
container.emplace_back(0,0);
However, if you want to construct a vector of certain size upfront, you need not emplace elements, because they are already there:
std::vector<std::pair<int, int>> container(size);
container[42] = make_pair(1,2); // 42 < size !
I'm guessing what you really want is something like:
vector<pair<int, int>> container(size);
This will initialize the vector constainer with size number of default-constructed elements.

Insert a pair key in map

The way to insert a pair in a map is that:
std::map<char,int> mymap;
// first insert function version (single parameter):
mymap.insert ( std::pair<char,int>('a',100) );
but now I'm trying to insert this in a map:
map<pair<int,int>, int> map1; //(pair is the key and int is a value)
I tried this:
pair<int,int> p;
p.first = 5;
p.second = 20;
map1.insert(pair<int,int>,double> (p,0));
So, how I can do it?
There are so many possibilities for this. You may choose any of the below which suits you better.
Using make_pair
map<pair<int,int>, int> m;
m.insert(make_pair(make_pair(5,20), 0));
Using curly braces
map<pair<int,int>, int> m;
m.insert({{5,20}, 0});
Declaring a C++ Pair first
pair<int,int> p(5,20);
map<pair<int,int>, int> m;
m.insert(make_pair(p, 0));
You can define and fill pairs manually if you want, but it's more common and idiomatic to use make_pair, or (since C++17) the deduction guides for pair:
map1.insert(std::make_pair(std::make_pair(5,20),0));
map1.insert(std::pair{std::pair{5,20},0}); // C++17 or later
First, if you are constructing the key while inserting, you may want to use emplace instead, since it will construct the key and value in place and is also better equipped to forward the arguments passed to it without explicit construction. So your first example would be simplified to
mymap.emplace('a', 100);
while your second example will surely work as
map1.emplace(std::piecewise_construct, std::forward_as_tuple(5, 20), std::forward_as_tuple(0));
and from C++17 onwards may also work as
map1.emplace(5, 20, 0);
See for examples here and here

C++ Is there a way to initialize unordered_map using contents of vector as keys and custom values?

Let's say I have a vector
vector<int> vect;
vect.push_back(2);
vect.push_back(3);
Now I want an unordered_map with those 2 and 3 as keys and 0 as default values for all those keys.
Is there a way to do it?
I tried doing
unordered_map<int, int> myMap(vect.begin(), vect.end());
hoping it would initialize with what's in the vector as keys and default int value.
However, it couldn't.
I mean, I can always just iterate the vector and manually insert pairs.
I just want to know if I can do it as a one liner during declaration.
Actually a simple one liner is enough, but not on declaration:
vector<int> vect = { 2, 3, 4};
unordered_map<int,int> map;
transform(vect.begin(), vect.end(), inserter(map, map.end()), [] (int v) { return make_pair(v, 0); });

c++, how to have a list of more then 1 element

I want to have a list that holds an integer a string together. I know I need to use "Pair" somewhere but I don't know how?
How I'd "Insert" into the that list that contains pairs?
(i do not need to use maps, I do not want my list contents to be organized alphabetical order.)
std::pair<int, std::string> p1(1, "abc");
std::pair<int, std::string> p2(2, "cba");
std::list<std::pair<int, std::string> > myList;
myList.push_back(p1); // Insert first pair
myList.push_back(p2); // Insert second pair (at the end of the list)
Use push_back, push_front to add elements to the rear, front of the list.
You can also use C++11 features to create new pairs "in-place".
std::list<std::pair<int, std::string>> myList;
myList.push_back(std::make_pair(1, "abc"));
myList.push_back(std::make_pair(2, "def"));
// or
std::list<std::pair<int, std::string>> myList{{1, "abc"}, {2, "cde"}};

adding elements of a vector to an unordered set

Is there an easy way to add all the elements of a vector to an unordered_set? They are of the same type. Right now, I am using a for loop and was wondering if there is a better way to do it
If you're constructing the unordered_set then:
std::vector<int> v;
std::unordered_set<int> s(v.begin(), v.end());
Forgive me if my syntax has any minor bugs, but you can try the std::copy function, its meant for this purpose.
std::vector<int> v;
std::unordered_set<int> s;
std::copy(v.begin(),v.end(),std::inserter(s,s.end()));