algoProg algoProg - 8 days ago 5
C++ Question

Vector<tuple>, make_tuple<int, ignore, float>

I create a vector of tuples:

std::vector<std::tuple<int*, bool, int*>> *DataStucture;


Next I want to iterate over a data set to get only the first element of each tuple.

Is this legal:

DataStructure -> push_back(std::make_tuple(some_pointer_to_some_int_value, std::ignore, std::ignore));


In next round of data set scanning, I compare the value of some_int_value and upon matching I set following two elements of DataStructure:

DataStructure -> push_back(std::make_tuple(std::ignore, some_bool_value, some_pointer_to_some_int_value2);


I am not sure what exactly
std::ignore
is for. I tried checking cpp reference website but I did not get it.

Answer

Just to elaborate on my previous comment, you cannot use std::ignore here, because it can be used only as lvalue, but really you don't need to either. Just use nullptr or any "default" value instead:

DataStructure->push_back(std::make_tuple(some_pointer_to_some_int_value, false, nullptr));

std::ignore should be used if you want to unpack your tuple into different values like this:

int* p; 
bool b; 
for (auto& tuple : *DataStructure) {
    std::tie(p,b,std::ignore) = tuple;
    // p now have value of first element of tuple
    // b now have value of second element of tuple
}

On an unrelated note, your second operation does not set values of existing tuple, it adds new one. Also, why use pointer to a vector instead of vector itself or at least std::unique_ptr? It is generaly considered better to omit unnecessary "naked" pointers.

Comments