I have multiple 3 one dimensional vectors (vector<int> starts, vector<int> ends, vector<int> points)
. Each having specific number of elements.
I want to create a two dimensional vector vector<pair<int,int>>matrix
in such a sequence :
- from beginning of
matrix
to size ofstart
first element ofmatrix
is elements ofvector<int> starts
and second element is "-1" - Append now the elements of
vector<int> ends
tomatrix
such that first element ofmatrix
is elements ofvector<int> ends
and second element is "-2" -
Append now the elements of
vector<int> points
tomatrix
such that first element ofmatrix
is elements ofvector<int> points
and second element is Index of points.Visual Representation :-
Input:
starts:
{1, 2, 3}
ends:{4, 5, 6}
points:(7, 8, 9}
Output:
matrix:
{ {1, -1}, {2, -1}, {3, -1}, {4, -2}, {5, -2}, {6, -2}, {7, 0}, {8, 1}, {9, 2} }
Currently I am using a push_back with for-loop function which works perfectly fine but when the input size is big code is very slow.
Code I am using is as follows:
vector<pair<int,int>> fast_count_segments(
vector<int> starts,
vector<int> ends,
vector<int> points)
{
int i = 0;
vector<pair<int,int>>matrix;
for(i; i<starts.size(); i++) {
matrix.push_back(make_pair(starts[i],-1));
}
for(i; i<starts.size()+ends.size(); i++) {
matrix.push_back(make_pair(ends[i-starts.size()],-2));
}
for(i; i<starts.size()+ends.size()+points.size(); i++) {
matrix.push_back(make_pair(
points[i-starts.size()-ends.size()],
i-(starts.size()+ends.size())
));
}
return matrix;
}
Can you please help on how to fill the 2D vector quickly with these requirements without iterating through each element. I am using C++11. Thanks in Advance !!
Aucun commentaire:
Enregistrer un commentaire