指向其他向量元素的指针向量

0 c++ pointers vector

EI具有作为向量的参数指针的函数:

void Function(std::vector<type>* aa)
Run Code Online (Sandbox Code Playgroud)

现在在这个函数里面我想过滤掉从那个向量到另一个向量的数据,我想通过改变这个临时值的值来改变原始向量的数据.该死的很难理解:

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
}
Run Code Online (Sandbox Code Playgroud)

我有类似的东西:

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.
}
Run Code Online (Sandbox Code Playgroud)

这不会改变原始价值.

Ben*_*ley 6

我认为你可能无法表达你的意图,所以这需要一个心灵的答案.

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.
}
Run Code Online (Sandbox Code Playgroud)