I have a function which shall initialize an empty vector of vectors from type string with certain elements from an given vector of vectors from type string. My syntax looks like this
std::vector<std::vector<std::string>> extract_data_on_userid(const std::vector<std::vector<std::string>> &array, std::vector<std::string> &user_ids, const int nr_of_events)
{
std::vector<std::vector<std::string>> data_extract;
int event_iterator = 0;
int user_id_iterator = 0;
// While loops which extracts the events based on user IDs
while (event_iterator <= nr_of_events)
{
// While loop which finds specified user id in an event
while (user_id_iterator < array[0].size())
{
if (check_id(user_ids, array[0][user_id_iterator]))
{
for (size_t i = 0; i < array.size(); i++)
{
data_extract[i].push_back(array[i][user_id_iterator]);
}
}
user_id_iterator++;
}
event_iterator++;
}
return data_extract;
}
The given vector consists on varying number of string vectors (at least 2). My method shall search for certain UserIDs in
check_id(user_ids, array[0][user_id_iterator])
an then push the relevant event (user_id_iterator) in a new 2D Vector for all 1D Vectors
vector[i:in][user_id_iterator]
Into the newly initiated vector
std::vector<std::vector<std::string>> data_extract;
over the for loop.
for (size_t i = 0; i < array.size(); i++)
{
data_extract[i].push_back(array[i][user_id_iterator]);
}
It all does work as expected until the elements of the vectors[i:in] in row [user_id_iterator] shall be pushed into the emtpy vectors.
Do I intially have to initialize all 1D vectors in the 2D Vector data_extract? What is the correct syntax to fill an empty vector of vectors which certain elements from a filled vector of vectors? I receive an exception (segmentation fault) because the emtpy vector is not initialized correctly.
Aucun commentaire:
Enregistrer un commentaire