EI具有将向量作为参数指针的功能:

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


现在在此函数中,我想从该向量中过滤出数据到另一个向量,并且我想通过更改此临时向量的值来更改原始向量的数据。该死,很难理解这样的东西:

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
}


我有这样的事情:

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.
}


那不会改变原始值。

最佳答案

我认为您可能无法传达自己的意图,因此这需要一个心理答案。

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.
}

08-17 04:07