简体   繁体   English

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

[英]Vectors of pointers to other vector's elements

EI have function which takes as parameter pointer to vector: EI具有将向量作为参数指针的功能:

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: 不要使用指向vector的指针,而应使用引用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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