Hello everyone!
I would like your help regarding this:
I have a tuple vector which i must iterate through, and accessing the vector data to decide wether to erase or not the current vector element.
My code :

typedef tuple <int, CString, int, int> pc_data;
vector <pc_data> pc_vec;

//Assume that pc_vec is filled with elements somewhere here...


//This code is not working. 
//Exception : vector subject out of range because of the size dynamically changing i suppose
for (int i=0; i<num_of_pcs; i++)
{
    if(std::get<2>(pc_vec[i])==0)
        pc_vec.erase(pc_vec.begin()+i);
}

//second try
//No exception here but the dynamic size of the vector again won't let me access all elements sequentially
for (size_t i = 0; i < pc_vec.size(); i++)
{
    if(std::get<2>(pc_vec[i])==0)
        pc_vec.erase(pc_vec.begin()+i);
}

//third try
//use iterator
vector<pc_data>::iterator it = pc_vec.begin();
//I don't know how to access the current element data in this situation..(?)
for ( ; it != pc_vec.end(); ) 
{
    if(std::get<2>(/*WHAT SHOULD I USE TO ACCESS THE CURRENT ELEMENT HERE??*/)==0)
    {
        it = pc_vec.erase(it);
    } 
    else 
    {
        ++it;
    }
}

Could you please suggest me a way to do it?
Thanks in advance and hope my question is clear enough.

Recommended Answers

All 6 Replies

Do you mean something like this?

#include <iostream>
#include <tuple>
#include <vector>
#include <string>

using namespace std;

int main()
{
    typedef tuple <int, string, int, int> pc_data;

    vector <pc_data> pc_vec;

    // Add some test data
    pc_vec.push_back(make_tuple(0, "never gonna ", 0, 0));
    pc_vec.push_back(make_tuple(1, "give you ", 1, 0));
    pc_vec.push_back(make_tuple(2, "up, never ", 2, 0));
    pc_vec.push_back(make_tuple(3, "stop. HAMMER TIME.", 3, 0));
    pc_vec.push_back(make_tuple(4, "gonna let you ", 4, 0));
    pc_vec.push_back(make_tuple(5, "down.\n", 5, 0));

    //Assume that pc_vec is filled with elements somewhere here...
    //This code is not working.
    //Exception : vector subject out of range because of the size dynamically changing i suppose
    for (int i=0; i < pc_vec.size(); i++)
    {
        // Test: we're going to delete the tuple of which the 3rd element (index 2) = 3.
        if(get<2>(pc_vec[i]) == 3)
        {
            pc_vec.erase(pc_vec.begin() + i);
            i--;
        }
    }

    // Result:
    for (int i = 0; i < pc_vec.size(); i++)
    {
        cout << get<1>(pc_vec[i]);
    }

    return 0;
}

Awww! Why didn't i think of that???!!
:)
Thanks a lot that seems to work!

By the way is there any reason that someone should use an iterator for this? Would it be more secure or fast?

Thanks again!

By the way is there any reason that someone should use an iterator for this? Would it be more secure or fast?

The main benefit (as far as I know) is that the use of iterators makes your code generic because most (if not all) containers in the STL support it. Performance differences seem to vary, never really tested it myself.

Thank you Gonbe!

Just as an FYI. If you call erase on a vector and you are using iterators any iterator that is after the begining will be invalidated.

Nice to know. I should read about iterator invalidation rules i suppose... my STL knowledge needs to be refreshed as it seems like i forgot everything...
:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.