简体   繁体   中英

Vectors of pointers to other vector's elements

EI have function which takes as parameter pointer to vector:

void Function(std::vector<type>* aa)

Now inside this function I want to filter out data from that vector to another vector and I want to change data of original vector by changing values of this temporary one. Damn it's hard to understand something like:

void Function(std::vector<type>* aa)
{
    std::vector<type*> temp; //to this vector I filter out data and by changning 
    //values of this vector I want to autmatically change values of aa vector
}

I have something like that:

void Announce_Event(std::vector<Event>& foo)
{
    std::vector<Event> current;
    tm current_time = {0,0,0,0,0,0,0,0,0};
    time_t thetime;
    thetime = time(NULL);
    localtime_s(&current_time, &thetime);
    for (unsigned i = 0; i < foo.size(); ++i) {
        if (foo[i].day == current_time.tm_mday &&
            foo[i].month == current_time.tm_mon &&
            foo[i].year == current_time.tm_year+1900)
        {
            current.push_back(foo[i]);
        }
    }
    std::cout << current.size() << std::endl;
    current[0].title = "Changed"; //<-- this is suppose to change value.
}

That does not change original value.

I think you may be having trouble communicating your intentions, so this calls for a psychic answer.

void Func(std::vector<type> & aa)
{
    std::vector<type*> temp;

    // I wish <algorithm> had a 'transform_if'    
    for(int i=0; i<aa.size(); ++i)
    {
        if( some_test(aa[i]) )
            temp.push_back(&aa[i])
    }

    // This leaves temp with pointers to some of the elements of aa.
    // Only those elements which passed some_test().  Now any modifications
    // to the dereferenced pointers in temp will modify those elements
    // of aa.  However, keep in mind that if elements are added or
    // removed from aa, it may invalidate the pointers in temp.
}

Do not use a pointer to a vector , use a reference instead:

void Function(std::vector<type>& aa)

inside the function you can now access the vectors contents as usual.

void Function(std::vector<type>& aa)
{
    std::vector<type>& temp = aa;

    // if you now append something to temp, it is also appended to aa
    aa.push_back(type());
}

I don't know why you want two references to one vector, but hey, you asked :)

EDIT: removed typo, see comments. thanx

As an aside, start formatting your code better. Messy code is difficult to understand and makes it harder for you to figure out what you're trying to do.

This will do what you want:

void Oglos_Wydarzenie(std::vector<Wydarzenie>& zmienna)
{
    std::vector<Wydarzenie *> obecne;
    tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
    time_t czas;
    czas = time(NULL);
    localtime_s(&AktualnyCzas,&czas);
    for (unsigned i = 0; i < zmienna.size(); ++i) {
        if (zmienna[i].dzien == AktualnyCzas.tm_mday &&
            zmienna[i].miesiac ==  AktualnyCzas.tm_mon &&
            zmienna[i].rok == AktualnyCzas.tm_year+1900)
        {
            obecne.push_back(&zmienna[i]);
        }
    }
    std::cout << obecne.size() << std::endl;
    obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
}

You could do this with all pointers and no references at all, but then it looks much more confusing:

void Oglos_Wydarzenie(std::vector<Wydarzenie>* zmienna)
{
    std::vector<Wydarzenie *> obecne;
    tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
    time_t czas;
    czas = time(NULL);
    localtime_s(&AktualnyCzas,&czas);
    for (unsigned i = 0; i < zmienna->size(); ++i) {
        if ((*zmienna)[i].dzien == AktualnyCzas.tm_mday &&
            (*zmienna)[i].miesiac ==  AktualnyCzas.tm_mon &&
            (*zmienna)[i].rok == AktualnyCzas.tm_year+1900)
        {
            obecne.push_back(&((*zmienna)[i]));
        }
    }
    std::cout << obecne.size() << std::endl;
    obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM