I am creating one application and got stuck with vector "reallocation". What I try to do is pass tuple<string, int, vector<Log> *>
where 3rd element is pointer pointing on vector<vector<Log>>
. Doing that on this way: Vector <Log> *pointer = &arr[length];
. And when I try to print address it works as expected. Problem is happening when I try this :
(*get<2>(*it)).resize(++get<1>(*it));
Trying to reallocate like (*pointer).resize();
Works
What I am trying to make is tuple (something like relational databases) :
tuple<string, int, vector <Log> *>
is unique id, number of entries for that id, pointer to address of first element of that row
So basically I will have tuple with basic info about unique ids, number of rows for each IP and pointer to that specific row.
First time when IP comes it's added to tuple and it's data stored on first place. Each other same IP will be checked in tuple and when it's found it's data need to be stored in next free space (with resizing by 1). Hope it's not too complicated to understand.
Here is code I am currently using : (note that variable a
is type Log
)
typedef vector<tuple<string, int, vector <Log>*>> lookup_list;
lookup_list lookup;
vector<vector<Log>> arr;
vector <Log> *pointer;
int length = 0;
string ip;
for (int i = 0; i < arr_size; i++){
ip = a[i].ip_address;
auto it = find_if(lookup.begin(), lookup.end(), [ip](const tuple<string, int, vector <Log>*>& e){
return get<0>(e) == ip;
});
if (it == lookup.end()){
arr.resize(length + 1);
arr[length].resize(1);
pointer = &arr[length];
arr[length][0] = a[i];
lookup.push_back(tuple<string, int, vector <Log> *>(ip, 1, pointer));
length++;
}
else {
get<1>(*it)++;
(*get<2>(*it)).resize(get<1>(*it));
(*get<2>(*it))[(get<1>(*it))-1] = a[i];
}
Error I am getting is basically either trying to access memory that is out of bounds or _Xlength_error("vector too long");
Should note that this happens on first IP that is same size, so not sure why that error is triggered...