Insert vector {x, y, z} into another vector? - c++

I'm looking for some pointers on inserting or pushing a vector into another vector.
The idea is I have vec1 = {1, 2, 3} for example.
Then I want to insert this into vec2 before next vec1 = {4, 5, 6} turns up.
The problem is I don't want vec 2 to read {1, 2, 3, 4, 5, 6}, I want it to read
vec2 = {1, 2, 3},
{4, 5, 6},... etc
Is this possible or I'm I completely mad. Any help will be great.
Thanks.

You can use a vector of vector of integers. Like this :
std::vector<std::vector<int>> vecofvecs = { {1,2,3}, {4,5,6} };
You can also use this :
#include <vector>
int main()
{
std::vector<std::vector<int>> vecofvecs;
std::vector<int> subvec1 = { 1,2,3 };
std::vector<int> subvec2 = { 4,5,6 };
vecofvecs.push_back(subvec1);
vecofvecs.push_back(subvec2);
return 0;
}

Related

C++ call a member function of an anonymous object instance?

I have the following piece of code:
map<int, set<int>> my_map;
int my_int1, my_int2;
// some code
set<int> temp;
temp.insert(my_int2);
my_map.insert(make_pair(my_int1, temp));
Is there some way to avoid using the temp (at least just for the code to look nicer, not necessarily for performance) and use an anonymous object? Something like:
my_map.insert(make_pair(my_int1, (set<int>{}).insert(my_int2)));
Except, the above line doesn't compile.
Not tested but try something like this:
my_map.insert({my_int1, {my_int2}});
Ok, let's sumarize. There is an important thing to know about insert: the insert doesn't insert if key already exists.
//create and initialise inline a map with int keys and string values
map<int, string> x {
{10, "hello"},
{123, "bye"},
{234, "world"}
};
x.insert({567, "alien"}); //inserts an element initialised inline
x.insert({567, "bar"}); //does nothing, according to specs
x[124] = "buzz"; //inserts an element
x[567] = "bar"; //modifies the element with key 567
//create and initialise inline a map with int keys and int values
map<int, string> y {
{10, 1},
{123, 2},
{234, 3}
};
y.insert({567, 4}); //insert an element initialised inline
z[124] = 5; //inserts
z[124] = 6; //modifies the above element
z[125]++; //inserts and modifies, now z[125] is 1
z[125]++; //modifies the above element to 2
It is very flexible. If you create a map of containers
//create and initialise inline a map with int keys and set values
map<int, set<int>> x {
{10, {1, 2, 3}},
{123, {2, 3}},
{234, {1, 2, 3}}}
};
//calling insert on map:
y.insert({567, {4, 5, 6}}); //element 567 has the value {4, 5, 6}
//now this calls insert in the set:
y[567].insert(7); //element 567 exists, so it is modified to {4, 5, 6, 7}
y[568].insert(7); //element 569 doesn't exist, so it is created first
//assign
y[235] = {4, 5, 6}; //element 235 has the value {4, 5, 6}
y[235] = {7, 8, 9}; //element 235 has the value {7, 8, 9}
y[235].insert(10); //element 235 has the value {7, 8, 9, 10}
You need none of your intermediate steps!
int main()
{
std::map<int, std::set<int>> my_map;
my_map.insert({1,{}});
}
You don't need the temporary std::set at all. Simply use std::map::operator[] to create the std::set in the std::map if it doesn't already exist, and then you can insert() into that std::set directly, eg:
map<int, set<int>> my_map;
int my_int1, my_int2;
// some code
my_map[my_int1].insert(my_int2);

order vector by value inside vector c++

std::vector<vector<float>> tmp = {{1,10,5,4},{2,5,5,1},{3,2,4,3},{4,9,7,8}};
I want to order this vector by the 4th(last) value in vector value. So the outcome will be like :
{{2,5,5,1},{3,2,4,3},{1,10,5,4},{4,9,7,8}};
Use std::sort with a suitable lambda for the comparator:
std::sort(begin(tmp), end(tmp), [](auto const& inner1, auto const& inner2)
{
// Note: No checking if the sizes are zero, should really be done
return inner1.back() < inner2.back();
});
That should do it:
#include <algorithm>
#include <vector>
int main() {
std::vector<std::vector<float>> tmp = {
{1, 10, 5, 4}, {2, 5, 5, 1}, {3, 2, 4, 3}, {4, 9, 7, 8}};
std::sort(tmp.begin(),tmp.end(), [](auto a, auto b){return a[3]<b[3];} );
}

Generate all possible ordered subset from a set

I'm aware how to generate all possible subsets from a set incorporating bit twiddling. For instance,
//Get if nth position's bit is set
bool IsBitSet(int num, int bit)
{
return 1 == ((num >> bit) & 1);
}
int subsetMaxIterCount = pow(2, someList.size());
for (int i = 0; i < subsetMaxIterCount; i++) {
vector<A> subset;
for (size_t i = 0; i < jobList.size(); i++)
{
if (IsBitSet(jobSubsetIdx, i)) {
//Add to subset here
}
}
//Here we have a subset for some i
}
However, this doesn't take into account of ordering.
For instance, if I had a set of {1, 2, 3}, the above algorithm generates subsets of:
{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1,2,3}
What I need in reality is this
{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1,2,3}, {2, 1}, {2, 1, 3}, {2, 3, 1}, {3, 1}, {3, 2}, {3, 1, 2}, {3, 2, 1}
Not sure if the above list is exhaustive. What's an effective algorithm in generating something like this? (Is this all possible subsets with permutation by the way?)
The way we generate the subsets using bit twiddling, every subset is sorted within it e.g. {1, 2, 3}, {2, 3}, {1, 3}. You can generate permutation for each subset using next_permutation
vector<vector<int>> mySubsetGenerator(vector<vector<int>>& subsets) {
vector<vector<int>> extendedSubset;
for(int i = 0; i < subsets.size(); ++i) {
do {
extendedSubset.push_back(subsets[i]);
} while(next_permutation(subsets[i].begin(), subsets[i].end()));
}
return extendedSubset;
}
Moreover, you can use only backtracking to generate all possible permutations by taking one or more elements of array.

Merge 2 sorted arrays with the same size without extra space?

Here is an example of what I am trying to do:
int size = 10;
int *data = new int[size];
int *array_1 = &data[0];
int *array_2 = &data[size/2];
//fill array_1 and array_2 with data
sort(array_1, array_1+size/2);
sort(array_2, array_2+size/2);
//now, is it possible to merge the 2 sorted arrays?
For example if array_1 = {1, 4, 7} and array_2 = {3, 5, 6}
I am trying to make data = {1, 3, 4, 5, 6, 7}
You want std::inplace_merge. See http://www.cplusplus.com/reference/algorithm/inplace_merge/

subarray of 2D array to NULL

I have the following array in C++:
int arr[3][2] = {
{1, 2},
{3, 4},
{5, 6}
};
And I want to implement data like this:
arr[1] = NULL;
But this doesn't work. Thanks a lot for your help.